@@ -61,7 +61,7 @@ $directory = [
61
61
try {
62
62
validate($directory, struct('Directory', [
63
63
'path' => 'is_dir',
64
- 'file' => optional('is_file', __DIR__ . '/directory-validation.php '),
64
+ 'file' => optional('is_file', __DIR__ . '/README.md '),
65
65
'content' => arrayOf(struct('Paragraph', [
66
66
'header' => 'is_string',
67
67
'line' => not('is_null'),
@@ -75,11 +75,40 @@ try {
75
75
echo "Path: {$directory['path']}" . PHP_EOL;
76
76
echo "File: {$directory['file']}" . PHP_EOL;
77
77
// Prints:
78
- // Path: /Users/seifkamal/src/struct-array/examples
79
- // File: /Users/seifkamal/src/struct-array/examples/directory-validation.php
78
+ // Path: /Users/seifkamal/src/struct-array
79
+ // File: /Users/seifkamal/src/struct-array/README.md
80
80
```
81
81
82
- Here's the same one using static class methods:
82
+ You can also just use an array directly, without creating a ` Struct ` :
83
+
84
+ ``` php
85
+ <?php
86
+
87
+ use function SK\StructArray\{
88
+ arrayOf, optional, not, validate
89
+ };
90
+
91
+ $directory = [...];
92
+
93
+ validate($directory, [
94
+ 'path' => 'is_dir',
95
+ 'file' => optional('is_file', __DIR__ . '/README.md'),
96
+ 'content' => arrayOf([
97
+ 'header' => 'is_string',
98
+ 'line' => not('is_null'),
99
+ ]),
100
+ ]);
101
+ ```
102
+
103
+ This is tailored for quick usage, and therefore assumes the defined interface is non-exhaustive
104
+ (ie. the array submitted for validation is allowed to have keys that aren't defined here). It also
105
+ means error messages will be more generic, ie you'll see:
106
+ > Struct failed validation. ...
107
+
108
+ instead of:
109
+ > Directory failed validation. ...
110
+
111
+ Here's another example directly using the static class methods:
83
112
84
113
``` php
85
114
<?php
@@ -105,7 +134,7 @@ $paragraphStruct = Struct::of('Paragraph', [
105
134
]);
106
135
$directoryStruct = Struct::of('Directory', [
107
136
'path' => 'is_dir',
108
- 'file' => Type::optional('is_file', __DIR__ . '/directory-validation.php '),
137
+ 'file' => Type::optional('is_file', __DIR__ . '/README.md '),
109
138
'content' => Type::arrayOf($paragraphStruct),
110
139
]);
111
140
@@ -119,8 +148,8 @@ try {
119
148
echo "Path: {$directory['path']}" . PHP_EOL;
120
149
echo "File: {$directory['file']}" . PHP_EOL;
121
150
// Prints:
122
- // Path: /Users/seifkamal/src/struct-array/examples
123
- // File: /Users/seifkamal/src/struct-array/examples/directory-validation.php
151
+ // Path: /Users/seifkamal/src/struct-array
152
+ // File: /Users/seifkamal/src/struct-array/README.md
124
153
```
125
154
126
155
For more, see the [ examples directory] ( examples ) .
0 commit comments