Skip to content

Commit f3a18df

Browse files
author
Christian Blank
committed
Write types implementation
1 parent 544409e commit f3a18df

File tree

8 files changed

+147
-17
lines changed

8 files changed

+147
-17
lines changed

README.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,10 @@ Create a requirement:
1919
$requirement = new ListType(
2020
new ObjectType([
2121
'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+
])
3226
);
3327
```
3428
Have some sort of external data you want to check.
@@ -37,12 +31,17 @@ $data = [
3731
[
3832
'foo' => 'foe',
3933
'bar' => 'baz',
34+
'buzz' => 'foe',
35+
'foobar' => null
4036
], [
4137
'foo' => 'foe',
4238
'bar' => 7,
39+
'buzz' => 'foe',
40+
'foobar' => 'baz',
4341
], [
4442
'foo' => [],
45-
'bar' => 'baz',
43+
'bar' => 9.1,
44+
'foobar' => 'baz',
4645
],
4746
];
4847
```

spec/Type/AnyTypeSpec.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

spec/Type/ListTypeSpec.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use StructureCheck\Type\ListType;
66
use PhpSpec\ObjectBehavior;
77
use Prophecy\Argument;
8+
use StructureCheck\Type\TypeInterface;
89

910
class ListTypeSpec extends ObjectBehavior
1011
{
11-
function it_is_initializable()
12+
function it_is_initializable(TypeInterface $childType)
1213
{
14+
$this->beConstructedWith($childType);
1315
$this->shouldHaveType(ListType::class);
1416
}
1517
}

spec/Type/ObjectTypeSpec.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use StructureCheck\Type\ObjectType;
66
use PhpSpec\ObjectBehavior;
77
use Prophecy\Argument;
8+
use StructureCheck\Type\TypeInterface;
89

910
class ObjectTypeSpec extends ObjectBehavior
1011
{
11-
function it_is_initializable()
12+
function it_is_initializable(TypeInterface $childType)
1213
{
14+
$this->beConstructedWith([$childType]);
1315
$this->shouldHaveType(ObjectType::class);
1416
}
1517
}

src/Type/AnyType.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace StructureCheck\Type;
4+
5+
class AnyType
6+
{
7+
}

src/Type/FloatType.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22

33
namespace StructureCheck\Type;
44

5-
class FloatType
5+
class FloatType implements TypeInterface
66
{
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+
}

src/Type/ListType.php

+48-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,53 @@
22

33
namespace StructureCheck\Type;
44

5-
class ListType
5+
use StructureCheck\Result;
6+
7+
/**
8+
* Class ListType
9+
* @package StructureCheck\Type
10+
*/
11+
class ListType implements TypeInterface
612
{
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+
}
754
}

src/Type/ObjectType.php

+43-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22

33
namespace StructureCheck\Type;
44

5-
class ObjectType
5+
use StructureCheck\Result;
6+
7+
class ObjectType implements TypeInterface
68
{
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+
}
749
}

0 commit comments

Comments
 (0)