File tree 3 files changed +125
-0
lines changed
3 files changed +125
-0
lines changed Original file line number Diff line number Diff line change @@ -76,3 +76,8 @@ There are some open issues with ideas for more types. Feel free to send pull req
76
76
77
77
Additionally you can implement the ` TypeInterface ` and use your own type implementations.
78
78
79
+ ## Checks
80
+
81
+ Checks are special types which can be used to add more rules to a field. So you can check
82
+ the length of a string, the count of elements in an array or determine if
83
+ a numeric value is in a given range.
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace spec \StructureCheck \Check ;
4
+
5
+ use StructureCheck \Check \CountCheck ;
6
+ use PhpSpec \ObjectBehavior ;
7
+ use Prophecy \Argument ;
8
+ use StructureCheck \ResultInterface ;
9
+ use StructureCheck \Type \TypeInterface ;
10
+
11
+ class CountCheckSpec extends ObjectBehavior
12
+ {
13
+ function it_is_initializable (TypeInterface $ child )
14
+ {
15
+ $ this ->beConstructedWith ($ child , 1 );
16
+
17
+ $ this ->shouldHaveType (CountCheck::class);
18
+ }
19
+
20
+ function it_implements_type_interface (TypeInterface $ child )
21
+ {
22
+ $ this ->beConstructedWith ($ child , 1 );
23
+
24
+ $ this ->shouldImplement (TypeInterface::class);
25
+ }
26
+
27
+ function it_returns_a_result_on_invalid_child_check (TypeInterface $ child , ResultInterface $ result )
28
+ {
29
+ $ result ->isValid ()->willReturn (false );
30
+ $ child ->check (Argument::any ())->willReturn ($ result );
31
+
32
+ $ this ->beConstructedWith ($ child , 1 );
33
+
34
+ $ this ->check ([])->shouldHaveType (ResultInterface::class);
35
+ }
36
+
37
+ function it_returns_a_result_on_check (TypeInterface $ child , ResultInterface $ result )
38
+ {
39
+ $ result ->isValid ()->willReturn (true );
40
+ $ child ->check (Argument::any ())->willReturn ($ result );
41
+
42
+ $ this ->beConstructedWith ($ child , 1 );
43
+
44
+ $ this ->check ([3 ])->shouldHaveType (ResultInterface::class);
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace StructureCheck \Check ;
4
+
5
+ use Countable ;
6
+ use StructureCheck \Result ;
7
+ use StructureCheck \Type \TypeInterface ;
8
+
9
+ /**
10
+ * Class CountCheck
11
+ * @package StructureCheck\Check
12
+ */
13
+ class CountCheck implements TypeInterface
14
+ {
15
+ /**
16
+ * @var string
17
+ */
18
+ private static $ countErrorMessage = 'The given countable %s has not the expected count %d. ' ;
19
+
20
+ /**
21
+ * @var string
22
+ */
23
+ private static $ countableErrorMessage = 'The given value %s is not a countable ' ;
24
+
25
+ /**
26
+ * @var TypeInterface
27
+ */
28
+ private $ child ;
29
+
30
+ /**
31
+ * @var int
32
+ */
33
+ private $ count ;
34
+
35
+ /**
36
+ * CountCheck constructor.
37
+ *
38
+ * @param TypeInterface $child
39
+ * @param int $count
40
+ */
41
+ public function __construct (TypeInterface $ child , $ count )
42
+ {
43
+ $ this ->child = $ child ;
44
+ $ this ->count = $ count ;
45
+ }
46
+
47
+ /**
48
+ * @inheritdoc
49
+ */
50
+ public function check ($ value )
51
+ {
52
+ $ result = $ this ->child ->check ($ value );
53
+
54
+ if (!$ result ->isValid ()) {
55
+ return $ result ;
56
+ }
57
+
58
+ if ($ value instanceof Countable) {
59
+ return new Result (
60
+ false ,
61
+ [sprintf (self ::$ countableErrorMessage , json_encode ($ value ))]
62
+ );
63
+ }
64
+
65
+ if (count ($ value ) === $ this ->count ) {
66
+ return new Result (
67
+ false ,
68
+ [sprintf (self ::$ countErrorMessage , json_encode ($ value ), $ this ->count )]
69
+ );
70
+ }
71
+
72
+ return new Result (true );
73
+ }
74
+ }
You can’t perform that action at this time.
0 commit comments