From 2c7934e81be9d2df52a56edd854588a0313c1bf4 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Wed, 31 Aug 2016 15:35:59 -0500 Subject: [PATCH 1/3] Use assertions Using `beberlei/assert` to simplify validation. This does not help with boolean type casting. --- composer.json | 3 ++- src/Boolean.php | 17 ++++++++--------- src/EmailAddress.php | 12 +++++++----- src/Identifier.php | 25 ++++++++----------------- src/Text.php | 17 ++++++++--------- tests/EmailAddressTest.php | 3 +-- tests/IdentifierTest.php | 17 ++--------------- tests/TextTest.php | 2 +- 8 files changed, 37 insertions(+), 59 deletions(-) diff --git a/composer.json b/composer.json index 7930b58..bf5c89f 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ } ], "require": { - "php": ">=5.5" + "php": ">=5.5", + "beberlei/assert": "^2.6" }, "require-dev": { "giggsey/libphonenumber-for-php": "^7.6" diff --git a/src/Boolean.php b/src/Boolean.php index 3263acf..37243d1 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -4,21 +4,22 @@ use InvalidArgumentException; +use function Assert\that; + class Boolean { + /** + * @var bool|null + */ private $value; public function __construct($value, $default = null) { - static $empty_values = [null, '']; - - if (is_bool($default) && in_array($value, $empty_values, true)) { + if ($value === null) { $value = $default; } - if (!is_scalar($value)) { - throw new InvalidArgumentException('Value must a possible boolean'); - } + that($value)->nullOr()->scalar(); $options = [ 'flags' => \FILTER_NULL_ON_FAILURE, @@ -26,9 +27,7 @@ public function __construct($value, $default = null) $value = filter_var($value, \FILTER_VALIDATE_BOOLEAN, $options); - if ($value === null) { - throw new InvalidArgumentException('Value must be boolean'); - } + that($value)->boolean(); $this->value = $value; } diff --git a/src/EmailAddress.php b/src/EmailAddress.php index 521f1da..0733803 100644 --- a/src/EmailAddress.php +++ b/src/EmailAddress.php @@ -4,6 +4,8 @@ use InvalidArgumentException; +use function Assert\that; + class EmailAddress { /** @@ -13,14 +15,14 @@ class EmailAddress public function __construct($email, $is_required = true) { - static $empty_values = [null, '']; + $assert = that($email); - if (!$is_required && in_array($email, $empty_values, true)) { - $email = null; - } elseif (filter_var($email, \FILTER_VALIDATE_EMAIL) === false) { - throw new InvalidArgumentException('Value must be an email address'); + if (!$is_required) { + $assert->nullOr(); } + $assert->scalar()->email(); + $this->email = $email; } diff --git a/src/Identifier.php b/src/Identifier.php index 68f80a2..fc89421 100644 --- a/src/Identifier.php +++ b/src/Identifier.php @@ -4,6 +4,8 @@ use InvalidArgumentException; +use function Assert\that; + class Identifier { /** @@ -13,25 +15,14 @@ class Identifier public function __construct($id, $is_required = true) { - static $empty_values = [null, false, 0, '0', '']; - - if (in_array($id, $empty_values, true) && !$is_required) { - $id = null; - } else { - $options = [ - 'flags' => \FILTER_REQUIRE_SCALAR, - 'options' => [ - 'min_range' => 1, - ], - ]; - - if (filter_var($id, \FILTER_VALIDATE_INT, $options) === false) { - throw new InvalidArgumentException('Value must be a valid identifier'); - } - - $id = (int) $id; + $assert = that($id); + + if (!$is_required) { + $assert->nullOr(); } + $assert->integer()->greaterThan(0); + $this->id = $id; } diff --git a/src/Text.php b/src/Text.php index a63d44d..59447e8 100644 --- a/src/Text.php +++ b/src/Text.php @@ -4,22 +4,21 @@ use InvalidArgumentException; +use function Assert\that; + class Text { + /** + * @var string|null + */ private $value; public function __construct($value, $regex = null) { - if ($value === null) { - $value = ''; - } - - if (!is_string($value)) { - throw new InvalidArgumentException('Value must be a string'); - } + $assert = that($value)->nullOr()->string(); - if ($regex && !preg_match($regex, $value)) { - throw new InvalidArgumentException('Value must match the expected format'); + if ($regex) { + $assert->regex($regex); } $this->value = $value; diff --git a/tests/EmailAddressTest.php b/tests/EmailAddressTest.php index 476f6f3..bc5e45b 100644 --- a/tests/EmailAddressTest.php +++ b/tests/EmailAddressTest.php @@ -16,7 +16,7 @@ public function testValid() public function testNotRequired() { - $email = ''; + $email = null; $vo = new EmailAddress($email, false); $this->assertNull($vo->value()); @@ -28,7 +28,6 @@ public function dataInvalid() 'invalid string' => ['not an email address'], 'array' => [[]], 'object' => [new \stdClass], - 'null' => [null], ]; } diff --git a/tests/IdentifierTest.php b/tests/IdentifierTest.php index 217dca3..fbb2c88 100644 --- a/tests/IdentifierTest.php +++ b/tests/IdentifierTest.php @@ -11,7 +11,6 @@ public function dataValid() { return [ 'integer' => [1], - 'string' => ['2'], 'octal' => [0x1a8], // 424 ]; } @@ -25,21 +24,9 @@ public function testValid($id) $this->assertEquals($id, $vo->value()); } - public function dataNullable() + public function testNullable() { - return [ - 'null' => [null], - 'integer' => [0], - 'string' => ['0'], - ]; - } - - /** - * @dataProvider dataNullable - */ - public function testNullable($id) - { - $vo = new Identifier($id, false); + $vo = new Identifier(null, false); $this->assertNull($vo->value()); } diff --git a/tests/TextTest.php b/tests/TextTest.php index b3aefeb..c93ead3 100644 --- a/tests/TextTest.php +++ b/tests/TextTest.php @@ -25,7 +25,7 @@ public function dataValid() public function testValid($text, $regex) { $vo = new Text($text, $regex); - $this->assertSame((string) $text, $vo->value()); + $this->assertSame($text, $vo->value()); } public function dataInvalidFormat() From 87c6ad127b5041cba9cd7c511473a2d8c734b786 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Thu, 1 Sep 2016 08:37:55 -0500 Subject: [PATCH 2/3] Remove unused exception import --- src/Boolean.php | 2 -- src/EmailAddress.php | 2 -- src/Identifier.php | 2 -- src/Text.php | 2 -- 4 files changed, 8 deletions(-) diff --git a/src/Boolean.php b/src/Boolean.php index 37243d1..1b1a0d1 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -2,8 +2,6 @@ namespace Equip\ValueObject; -use InvalidArgumentException; - use function Assert\that; class Boolean diff --git a/src/EmailAddress.php b/src/EmailAddress.php index 0733803..15fad9b 100644 --- a/src/EmailAddress.php +++ b/src/EmailAddress.php @@ -2,8 +2,6 @@ namespace Equip\ValueObject; -use InvalidArgumentException; - use function Assert\that; class EmailAddress diff --git a/src/Identifier.php b/src/Identifier.php index fc89421..af212aa 100644 --- a/src/Identifier.php +++ b/src/Identifier.php @@ -2,8 +2,6 @@ namespace Equip\ValueObject; -use InvalidArgumentException; - use function Assert\that; class Identifier diff --git a/src/Text.php b/src/Text.php index 59447e8..122a606 100644 --- a/src/Text.php +++ b/src/Text.php @@ -2,8 +2,6 @@ namespace Equip\ValueObject; -use InvalidArgumentException; - use function Assert\that; class Text From 6f8c29d76ba87bee61efe63d718e4c8a440013fc Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Thu, 1 Sep 2016 08:38:18 -0500 Subject: [PATCH 3/3] Add docblocks --- src/Boolean.php | 7 +++++++ src/EmailAddress.php | 7 +++++++ src/Identifier.php | 7 +++++++ src/PhoneNumber.php | 16 ++++++++++++++++ src/Text.php | 7 +++++++ 5 files changed, 44 insertions(+) diff --git a/src/Boolean.php b/src/Boolean.php index 1b1a0d1..f997e97 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -11,6 +11,10 @@ class Boolean */ private $value; + /** + * @param string|boolean|null $value + * @param string|boolean|null $default + */ public function __construct($value, $default = null) { if ($value === null) { @@ -30,6 +34,9 @@ public function __construct($value, $default = null) $this->value = $value; } + /** + * @return boolean + */ public function value() { return $this->value; diff --git a/src/EmailAddress.php b/src/EmailAddress.php index 15fad9b..b72353d 100644 --- a/src/EmailAddress.php +++ b/src/EmailAddress.php @@ -11,6 +11,10 @@ class EmailAddress */ private $email; + /** + * @param string|null $email + * @param boolean $is_required + */ public function __construct($email, $is_required = true) { $assert = that($email); @@ -24,6 +28,9 @@ public function __construct($email, $is_required = true) $this->email = $email; } + /** + * @return string|null + */ public function value() { return $this->email; diff --git a/src/Identifier.php b/src/Identifier.php index af212aa..c6df712 100644 --- a/src/Identifier.php +++ b/src/Identifier.php @@ -11,6 +11,10 @@ class Identifier */ private $id; + /** + * @param int|null $id + * @param boolean $is_required + */ public function __construct($id, $is_required = true) { $assert = that($id); @@ -24,6 +28,9 @@ public function __construct($id, $is_required = true) $this->id = $id; } + /** + * @return int|null + */ public function value() { return $this->id; diff --git a/src/PhoneNumber.php b/src/PhoneNumber.php index be3f5b7..93c8e8a 100644 --- a/src/PhoneNumber.php +++ b/src/PhoneNumber.php @@ -13,6 +13,10 @@ class PhoneNumber */ private $number; + /** + * @param string|null $number + * @param boolean $is_required + */ public function __construct($number, $is_required = true) { static $empty_values = [null, '']; @@ -35,6 +39,9 @@ public function __construct($number, $is_required = true) $this->number = $number; } + /** + * @return string|null + */ public function value() { if ($this->number) { @@ -44,16 +51,25 @@ public function value() return null; } + /** + * @return PhoneNumberUtil + */ protected function util() { return PhoneNumberUtil::getInstance(); } + /** + * @return string + */ protected function preferredRegion() { return PhoneNumberUtil::UNKNOWN_REGION; } + /** + * @return int + */ protected function preferredFormat() { return PhoneNumberFormat::E164; diff --git a/src/Text.php b/src/Text.php index 122a606..30ca42b 100644 --- a/src/Text.php +++ b/src/Text.php @@ -11,6 +11,10 @@ class Text */ private $value; + /** + * @param string|null $value + * @param string|null $regex + */ public function __construct($value, $regex = null) { $assert = that($value)->nullOr()->string(); @@ -22,6 +26,9 @@ public function __construct($value, $regex = null) $this->value = $value; } + /** + * @return string|null + */ public function value() { return $this->value;