Skip to content
This repository was archived by the owner on Dec 19, 2022. It is now read-only.

Commit 8a74487

Browse files
author
Seif Kamal
committed
Initial commit
0 parents  commit 8a74487

23 files changed

+1295
-0
lines changed

.gitattributes

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# .gitattributes
2+
docs/ export-ignore
3+
examples/ export-ignore
4+
tests/ export-ignore
5+
6+
# Auto detect text files and perform LF normalization
7+
* text=auto

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
composer.lock

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Seif Kamal
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
![GitHub license](https://img.shields.io/github/license/safe-k/struct-array)
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).

composer.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "safe-k/struct-array",
3+
"type": "library",
4+
"description": "Array structure validation",
5+
"keywords": [
6+
"data",
7+
"array",
8+
"structure",
9+
"validation",
10+
"dto",
11+
"ddd"
12+
],
13+
"license": "MIT",
14+
"authors": [
15+
{
16+
"name": "Seif Kamal",
17+
"email": "[email protected]"
18+
}
19+
],
20+
"require": {
21+
"php": ">=7.0.0"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^9.1"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"SK\\StructArray\\": "src"
29+
},
30+
"files": [
31+
"src/functions.php"
32+
]
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"SK\\StructArray\\Test\\": "tests"
37+
}
38+
},
39+
"scripts": {
40+
"test": "phpunit tests"
41+
}
42+
}

0 commit comments

Comments
 (0)