Skip to content

Commit

Permalink
Add optional type (#17)
Browse files Browse the repository at this point in the history
* Add optional type

Resolve #16

* Use php5 compatible version
  • Loading branch information
Christian Blank authored May 28, 2017
1 parent 437f897 commit 79bd1ff
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ install:
- composer install

script:
- vendor/bin/phpspec run --no-interaction
- vendor/bin/phpspec run --no-interaction
- vendor/bin/phpunit tests
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Currently the following types are supported:
* List
* Datetime
* Regex
* Optional

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

Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
}
],
"require-dev": {
"phpspec/phpspec": "^3.2"
"phpspec/phpspec": "^3.2",
"phpunit/phpunit": "^5.6"
},
"autoload": {
"psr-4": {
"StructureCheck\\": [
"src"
],
"StructureCheck\\Test\\": [
"tests"
]
}
}
Expand Down
23 changes: 23 additions & 0 deletions spec/Type/OptionalTypeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace spec\StructureCheck\Type;

use StructureCheck\Result;
use StructureCheck\Type\OptionalType;
use PhpSpec\ObjectBehavior;
use StructureCheck\Type\TypeInterface;

class OptionalTypeSpec extends ObjectBehavior
{
function it_is_initializable(TypeInterface $childType)
{
$this->beConstructedWith($childType);
$this->shouldHaveType(OptionalType::class);
}

function it_should_return_the_value_from_the_child(TypeInterface $childType) {
$this->beConstructedWith($childType);
$childType->check(false)->willReturn(new Result(false, []));
$this->check(false)->isValid()->shouldBe(false);
}
}
4 changes: 2 additions & 2 deletions src/Check/CountCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function check($value)
return $result;
}

if ($value instanceof Countable) {
if (!$value instanceof Countable) {
return new Result(
false,
[sprintf(self::$countableErrorMessage, json_encode($value))]
);
}

if (count($value) === $this->count) {
if (count($value) !== $this->count) {
return new Result(
false,
[sprintf(self::$countErrorMessage, json_encode($value), $this->count)]
Expand Down
6 changes: 4 additions & 2 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public function check($value)

foreach ($this->children as $key => $child) {
if (!array_key_exists($key, $value)) {
$valid = false;
$errors[] = sprintf(self::$missingKeyErrorMessage, $key);
if (!$child instanceof OptionalType) {
$valid = false;
$errors[] = sprintf(self::$missingKeyErrorMessage, $key);
}

continue;
}
Expand Down
37 changes: 37 additions & 0 deletions src/Type/OptionalType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace StructureCheck\Type;

use StructureCheck\ResultInterface;

/**
* Class OptionalType
* @package StructureCheck\Type
* @author Christian Blank <[email protected]>
*/
class OptionalType implements TypeInterface
{
/**
* @var TypeInterface
*/
private $child;

/**
* OptionalType constructor.
* @param TypeInterface $child
*/
public function __construct(TypeInterface $child)
{
$this->child = $child;
}

/**
* @param mixed $value
*
* @return ResultInterface
*/
public function check($value)
{
return $this->child->check($value);
}
}
46 changes: 46 additions & 0 deletions tests/Integration/Type/ObjectTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace StructureCheck\Test\Integration\Type;

use PHPUnit\Framework\TestCase;
use StructureCheck\Checker;
use StructureCheck\CheckerInterface;
use StructureCheck\Type\AnyType;
use StructureCheck\Type\ObjectType;
use StructureCheck\Type\OptionalType;

/**
* Class ObjectTypeTest
* @package StructureCheck\Test\Integration\Type
* @author Christian Blank <[email protected]>
*/
class ObjectTypeTest extends TestCase
{
/**
* @var CheckerInterface
*/
private $checker;

/**
*
*/
protected function setUp()
{
parent::setUp();
$this->checker = new Checker();
}

/**
* @test
*/
public function itShouldHandleAbsenceOfOptionalDeclaredType()
{
$structure = new ObjectType([
'opt' => new OptionalType(new AnyType())
]);

$actual = $structure->check([]);

$this->assertSame(true, $actual->isValid());
}
}

0 comments on commit 79bd1ff

Please sign in to comment.