Skip to content

Commit 79bd1ff

Browse files
author
Christian Blank
authored
Add optional type (#17)
* Add optional type Resolve #16 * Use php5 compatible version
1 parent 437f897 commit 79bd1ff

File tree

8 files changed

+120
-6
lines changed

8 files changed

+120
-6
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ install:
1717
- composer install
1818

1919
script:
20-
- vendor/bin/phpspec run --no-interaction
20+
- vendor/bin/phpspec run --no-interaction
21+
- vendor/bin/phpunit tests

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Currently the following types are supported:
7171
* List
7272
* Datetime
7373
* Regex
74+
* Optional
7475

7576
There are some open issues with ideas for more types. Feel free to send pull requests.
7677

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
}
1414
],
1515
"require-dev": {
16-
"phpspec/phpspec": "^3.2"
16+
"phpspec/phpspec": "^3.2",
17+
"phpunit/phpunit": "^5.6"
1718
},
1819
"autoload": {
1920
"psr-4": {
2021
"StructureCheck\\": [
2122
"src"
23+
],
24+
"StructureCheck\\Test\\": [
25+
"tests"
2226
]
2327
}
2428
}

spec/Type/OptionalTypeSpec.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace spec\StructureCheck\Type;
4+
5+
use StructureCheck\Result;
6+
use StructureCheck\Type\OptionalType;
7+
use PhpSpec\ObjectBehavior;
8+
use StructureCheck\Type\TypeInterface;
9+
10+
class OptionalTypeSpec extends ObjectBehavior
11+
{
12+
function it_is_initializable(TypeInterface $childType)
13+
{
14+
$this->beConstructedWith($childType);
15+
$this->shouldHaveType(OptionalType::class);
16+
}
17+
18+
function it_should_return_the_value_from_the_child(TypeInterface $childType) {
19+
$this->beConstructedWith($childType);
20+
$childType->check(false)->willReturn(new Result(false, []));
21+
$this->check(false)->isValid()->shouldBe(false);
22+
}
23+
}

src/Check/CountCheck.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public function check($value)
5555
return $result;
5656
}
5757

58-
if ($value instanceof Countable) {
58+
if (!$value instanceof Countable) {
5959
return new Result(
6060
false,
6161
[sprintf(self::$countableErrorMessage, json_encode($value))]
6262
);
6363
}
6464

65-
if (count($value) === $this->count) {
65+
if (count($value) !== $this->count) {
6666
return new Result(
6767
false,
6868
[sprintf(self::$countErrorMessage, json_encode($value), $this->count)]

src/Type/ObjectType.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public function check($value)
3333

3434
foreach ($this->children as $key => $child) {
3535
if (!array_key_exists($key, $value)) {
36-
$valid = false;
37-
$errors[] = sprintf(self::$missingKeyErrorMessage, $key);
36+
if (!$child instanceof OptionalType) {
37+
$valid = false;
38+
$errors[] = sprintf(self::$missingKeyErrorMessage, $key);
39+
}
3840

3941
continue;
4042
}

src/Type/OptionalType.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace StructureCheck\Type;
4+
5+
use StructureCheck\ResultInterface;
6+
7+
/**
8+
* Class OptionalType
9+
* @package StructureCheck\Type
10+
* @author Christian Blank <[email protected]>
11+
*/
12+
class OptionalType implements TypeInterface
13+
{
14+
/**
15+
* @var TypeInterface
16+
*/
17+
private $child;
18+
19+
/**
20+
* OptionalType constructor.
21+
* @param TypeInterface $child
22+
*/
23+
public function __construct(TypeInterface $child)
24+
{
25+
$this->child = $child;
26+
}
27+
28+
/**
29+
* @param mixed $value
30+
*
31+
* @return ResultInterface
32+
*/
33+
public function check($value)
34+
{
35+
return $this->child->check($value);
36+
}
37+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace StructureCheck\Test\Integration\Type;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use StructureCheck\Checker;
7+
use StructureCheck\CheckerInterface;
8+
use StructureCheck\Type\AnyType;
9+
use StructureCheck\Type\ObjectType;
10+
use StructureCheck\Type\OptionalType;
11+
12+
/**
13+
* Class ObjectTypeTest
14+
* @package StructureCheck\Test\Integration\Type
15+
* @author Christian Blank <[email protected]>
16+
*/
17+
class ObjectTypeTest extends TestCase
18+
{
19+
/**
20+
* @var CheckerInterface
21+
*/
22+
private $checker;
23+
24+
/**
25+
*
26+
*/
27+
protected function setUp()
28+
{
29+
parent::setUp();
30+
$this->checker = new Checker();
31+
}
32+
33+
/**
34+
* @test
35+
*/
36+
public function itShouldHandleAbsenceOfOptionalDeclaredType()
37+
{
38+
$structure = new ObjectType([
39+
'opt' => new OptionalType(new AnyType())
40+
]);
41+
42+
$actual = $structure->check([]);
43+
44+
$this->assertSame(true, $actual->isValid());
45+
}
46+
}

0 commit comments

Comments
 (0)