|
| 1 | + |
| 2 | + |
| 3 | +# PHP Structured Arrays |
| 4 | + |
| 5 | +Make PHP array data validation easier and clearer by defining array structures. |
| 6 | + |
| 7 | +## Why |
| 8 | + |
| 9 | +We often need to receive, validate, and process data objects. Sometimes, an array might be too |
| 10 | +dumb for the task at hand, requiring several unpleasant control structures to validate and setup. |
| 11 | +Moreover, creating a dedicated DTO class (or more) might be overkill, requiring a lot of boilerplate |
| 12 | +code, whilst making things like error messaging and future refactoring more difficult. |
| 13 | + |
| 14 | +In cases like this I find myself itching for a Go/Rust like `Struct` instead. |
| 15 | + |
| 16 | +See [doc](docs/use-case.md) for a more in depth discussion. |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Here's an example written using the provided convenient helper functions: |
| 21 | + |
| 22 | +```php |
| 23 | +<?php |
| 24 | + |
| 25 | +use SK\StructArray\Exception\StructValidationException; |
| 26 | + |
| 27 | +use function SK\StructArray\{ |
| 28 | + arrayOf, optional, not, struct, validate |
| 29 | +}; |
| 30 | + |
| 31 | +$directory = [ |
| 32 | + 'path' => __DIR__, |
| 33 | + 'content' => [ |
| 34 | + [ |
| 35 | + 'header' => 'Greeting', |
| 36 | + 'line' => 'Hello', |
| 37 | + ], |
| 38 | + ] |
| 39 | +]; |
| 40 | + |
| 41 | +try { |
| 42 | + validate($directory, struct('Directory', [ |
| 43 | + 'path' => 'is_dir', |
| 44 | + 'file' => optional('is_file', __DIR__ . '/directory-validation.php'), |
| 45 | + 'content' => arrayOf(struct('Paragraph', [ |
| 46 | + 'header' => 'is_string', |
| 47 | + 'line' => not('is_null'), |
| 48 | + ])), |
| 49 | + ])); |
| 50 | +} catch (StructValidationException $e) { |
| 51 | + echo $e->getMessage() . PHP_EOL; |
| 52 | + return; |
| 53 | +} |
| 54 | + |
| 55 | +echo "Path: {$directory['path']}" . PHP_EOL; |
| 56 | +echo "File: {$directory['file']}" . PHP_EOL; |
| 57 | +// Prints: |
| 58 | +// Path: /Users/seifkamal/src/struct-array/examples |
| 59 | +// File: /Users/seifkamal/src/struct-array/examples/directory-validation.php |
| 60 | +``` |
| 61 | + |
| 62 | +Here's the same one using static class methods: |
| 63 | + |
| 64 | +```php |
| 65 | +<?php |
| 66 | + |
| 67 | +use SK\StructArray\Exception\StructValidationException; |
| 68 | +use SK\StructArray\Property\Type; |
| 69 | +use SK\StructArray\Struct; |
| 70 | +use SK\StructArray\Validator; |
| 71 | + |
| 72 | +$directory = [ |
| 73 | + 'path' => __DIR__, |
| 74 | + 'content' => [ |
| 75 | + [ |
| 76 | + 'header' => 'Greeting', |
| 77 | + 'line' => 'Hello', |
| 78 | + ], |
| 79 | + ] |
| 80 | +]; |
| 81 | + |
| 82 | +$paragraphStruct = Struct::of('Paragraph', [ |
| 83 | + 'header' => 'is_string', |
| 84 | + 'line' => Type::not('is_null'), |
| 85 | +]); |
| 86 | +$directoryStruct = Struct::of('Directory', [ |
| 87 | + 'path' => 'is_dir', |
| 88 | + 'file' => Type::optional('is_file', __DIR__ . '/directory-validation.php'), |
| 89 | + 'content' => Type::arrayOf($paragraphStruct), |
| 90 | +]); |
| 91 | + |
| 92 | +try { |
| 93 | + Validator::validate($directory, $directoryStruct); |
| 94 | +} catch (StructValidationException $e) { |
| 95 | + echo $e->getMessage() . PHP_EOL; |
| 96 | + return; |
| 97 | +} |
| 98 | + |
| 99 | +echo "Path: {$directory['path']}" . PHP_EOL; |
| 100 | +echo "File: {$directory['file']}" . PHP_EOL; |
| 101 | +// Prints: |
| 102 | +// Path: /Users/seifkamal/src/struct-array/examples |
| 103 | +// File: /Users/seifkamal/src/struct-array/examples/directory-validation.php |
| 104 | +``` |
| 105 | + |
| 106 | +For more detailed ones, see the [examples directory](examples). |
0 commit comments