@@ -61,7 +61,7 @@ $directory = [
6161try {
6262 validate($directory, struct('Directory', [
6363 'path' => 'is_dir',
64- 'file' => optional('is_file', __DIR__ . '/directory-validation.php '),
64+ 'file' => optional('is_file', __DIR__ . '/README.md '),
6565 'content' => arrayOf(struct('Paragraph', [
6666 'header' => 'is_string',
6767 'line' => not('is_null'),
@@ -75,11 +75,40 @@ try {
7575echo "Path: {$directory['path']}" . PHP_EOL;
7676echo "File: {$directory['file']}" . PHP_EOL;
7777// 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
8080```
8181
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:
83112
84113``` php
85114<?php
@@ -105,7 +134,7 @@ $paragraphStruct = Struct::of('Paragraph', [
105134]);
106135$directoryStruct = Struct::of('Directory', [
107136 'path' => 'is_dir',
108- 'file' => Type::optional('is_file', __DIR__ . '/directory-validation.php '),
137+ 'file' => Type::optional('is_file', __DIR__ . '/README.md '),
109138 'content' => Type::arrayOf($paragraphStruct),
110139]);
111140
@@ -119,8 +148,8 @@ try {
119148echo "Path: {$directory['path']}" . PHP_EOL;
120149echo "File: {$directory['file']}" . PHP_EOL;
121150// 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
124153```
125154
126155For more, see the [ examples directory] ( examples ) .
0 commit comments