File tree 8 files changed +147
-17
lines changed
8 files changed +147
-17
lines changed Original file line number Diff line number Diff line change @@ -19,16 +19,10 @@ Create a requirement:
19
19
$requirement = new ListType(
20
20
new ObjectType([
21
21
'foo' => new StringType(),
22
- 'bar' => new StringType()
23
- ]),
24
- new ObjectType([
25
- 'foo' => new StringType(),
26
- 'bar' => new IntType()
27
- ]),
28
- new ObjectType([
29
- 'foo' => new AnyType(),
30
- 'bar' => new IntType()
31
- ]),
22
+ 'bar' => new IntType(),
23
+ 'buzz' => new AnyType(),
24
+ 'foobar' => new NullableType(new StringType())
25
+ ])
32
26
);
33
27
```
34
28
Have some sort of external data you want to check.
@@ -37,12 +31,17 @@ $data = [
37
31
[
38
32
'foo' => 'foe',
39
33
'bar' => 'baz',
34
+ 'buzz' => 'foe',
35
+ 'foobar' => null
40
36
], [
41
37
'foo' => 'foe',
42
38
'bar' => 7,
39
+ 'buzz' => 'foe',
40
+ 'foobar' => 'baz',
43
41
], [
44
42
'foo' => [],
45
- 'bar' => 'baz',
43
+ 'bar' => 9.1,
44
+ 'foobar' => 'baz',
46
45
],
47
46
];
48
47
```
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace spec \StructureCheck \Type ;
4
+
5
+ use StructureCheck \Type \AnyType ;
6
+ use PhpSpec \ObjectBehavior ;
7
+ use Prophecy \Argument ;
8
+
9
+ class AnyTypeSpec extends ObjectBehavior
10
+ {
11
+ function it_is_initializable ()
12
+ {
13
+ $ this ->shouldHaveType (AnyType::class);
14
+ }
15
+ }
Original file line number Diff line number Diff line change 5
5
use StructureCheck \Type \ListType ;
6
6
use PhpSpec \ObjectBehavior ;
7
7
use Prophecy \Argument ;
8
+ use StructureCheck \Type \TypeInterface ;
8
9
9
10
class ListTypeSpec extends ObjectBehavior
10
11
{
11
- function it_is_initializable ()
12
+ function it_is_initializable (TypeInterface $ childType )
12
13
{
14
+ $ this ->beConstructedWith ($ childType );
13
15
$ this ->shouldHaveType (ListType::class);
14
16
}
15
17
}
Original file line number Diff line number Diff line change 5
5
use StructureCheck \Type \ObjectType ;
6
6
use PhpSpec \ObjectBehavior ;
7
7
use Prophecy \Argument ;
8
+ use StructureCheck \Type \TypeInterface ;
8
9
9
10
class ObjectTypeSpec extends ObjectBehavior
10
11
{
11
- function it_is_initializable ()
12
+ function it_is_initializable (TypeInterface $ childType )
12
13
{
14
+ $ this ->beConstructedWith ([$ childType ]);
13
15
$ this ->shouldHaveType (ObjectType::class);
14
16
}
15
17
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace StructureCheck \Type ;
4
+
5
+ class AnyType
6
+ {
7
+ }
Original file line number Diff line number Diff line change 2
2
3
3
namespace StructureCheck \Type ;
4
4
5
- class FloatType
5
+ class FloatType implements TypeInterface
6
6
{
7
- }
7
+ private static $ errorMessage = 'The value %s is not a float. ' ;
8
+
9
+ /**
10
+ * @param mixed $value
11
+ *
12
+ * @return ResultInterface
13
+ */
14
+ public function check ($ value )
15
+ {
16
+ $ checkResult = is_float ($ value );
17
+
18
+ return new Result (
19
+ $ checkResult ,
20
+ !$ checkResult ? [sprintf (self ::$ errorMessage , json_encode ($ value ))] : []
21
+ );
22
+ }
23
+ }
Original file line number Diff line number Diff line change 2
2
3
3
namespace StructureCheck \Type ;
4
4
5
- class ListType
5
+ use StructureCheck \Result ;
6
+
7
+ /**
8
+ * Class ListType
9
+ * @package StructureCheck\Type
10
+ */
11
+ class ListType implements TypeInterface
6
12
{
13
+ private static $ isNotAnArrayMessage = 'The given value %s is not an array. ' ;
14
+
15
+ /**
16
+ * @var TypeInterface[]
17
+ */
18
+ private $ child ;
19
+
20
+ /**
21
+ * ListType constructor.
22
+ *
23
+ * @param TypeInterface $child
24
+ */
25
+ public function __construct (TypeInterface $ child )
26
+ {
27
+ $ this ->child = $ child ;
28
+ }
29
+
30
+ /**
31
+ * @inheritdoc
32
+ */
33
+ public function check ($ value )
34
+ {
35
+ if (!is_array ($ value )) {
36
+ return new Result (
37
+ false ,
38
+ [sprintf (self ::$ isNotAnArrayMessage , json_encode ($ value ))]
39
+ );
40
+ }
41
+
42
+ $ errors = [];
43
+ $ valid = true ;
44
+
45
+ foreach ($ value as $ item ) {
46
+ $ result = $ this ->child ->check ($ item );
47
+
48
+ $ valid &= $ result ->isValid ();
49
+ $ errors += $ result ->getErrors ();
50
+ }
51
+
52
+ return new Result ($ valid , $ errors );
53
+ }
7
54
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace StructureCheck \Type ;
4
4
5
- class ObjectType
5
+ use StructureCheck \Result ;
6
+
7
+ class ObjectType implements TypeInterface
6
8
{
9
+ private static $ missingKeyErrorMessage = 'The key "%s" does not exists ' ;
10
+
11
+ /**
12
+ * @var TypeInterface[]
13
+ */
14
+ private $ children ;
15
+
16
+ /**
17
+ * ObjectType constructor.
18
+ *
19
+ * @param TypeInterface[] $children
20
+ */
21
+ public function __construct (array $ children )
22
+ {
23
+ $ this ->children = $ children ;
24
+ }
25
+
26
+ /**
27
+ * @inheritdoc
28
+ */
29
+ public function check ($ value )
30
+ {
31
+ $ errors = [];
32
+ $ valid = true ;
33
+
34
+ foreach ($ this ->children as $ key => $ child ) {
35
+ if (!array_key_exists ($ key , $ value )) {
36
+ $ valid = false ;
37
+ $ errors [] = sprintf (self ::$ missingKeyErrorMessage , $ key );
38
+
39
+ continue ;
40
+ }
41
+
42
+ $ result = $ child ->check ($ value [$ key ]);
43
+ $ valid &= $ result ->isValid ();
44
+ $ errors += $ result ->getErrors ();
45
+ }
46
+
47
+ return new Result ($ valid , $ errors );
48
+ }
7
49
}
You can’t perform that action at this time.
0 commit comments