Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}
],
"require": {
"php": ">=5.5"
"php": ">=5.5",
"beberlei/assert": "^2.6"
},
"require-dev": {
"giggsey/libphonenumber-for-php": "^7.6"
Expand Down
24 changes: 14 additions & 10 deletions src/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,41 @@

namespace Equip\ValueObject;

use InvalidArgumentException;
use function Assert\that;

class Boolean
{
/**
* @var bool|null
*/
private $value;

/**
* @param string|boolean|null $value
* @param string|boolean|null $default
*/
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();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally I think it reads better as Assertion::that($value)->nullOr()->scalar();

"that" by itself seems to lack some context. If you want to keep the use maybe alias it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree, there is a clear use function at the top of the file.


$options = [
'flags' => \FILTER_NULL_ON_FAILURE,
];

$value = filter_var($value, \FILTER_VALIDATE_BOOLEAN, $options);

if ($value === null) {
throw new InvalidArgumentException('Value must be boolean');
}
that($value)->boolean();

$this->value = $value;
}

/**
* @return boolean
*/
public function value()
{
return $this->value;
Expand Down
19 changes: 13 additions & 6 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Equip\ValueObject;

use InvalidArgumentException;
use function Assert\that;

class EmailAddress
{
Expand All @@ -11,19 +11,26 @@ class EmailAddress
*/
private $email;

/**
* @param string|null $email
* @param boolean $is_required
*/
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;
}

/**
* @return string|null
*/
public function value()
{
return $this->email;
Expand Down
32 changes: 14 additions & 18 deletions src/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Equip\ValueObject;

use InvalidArgumentException;
use function Assert\that;

class Identifier
{
Expand All @@ -11,30 +11,26 @@ class Identifier
*/
private $id;

/**
* @param int|null $id
* @param boolean $is_required
*/
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;
}

/**
* @return int|null
*/
public function value()
{
return $this->id;
Expand Down
16 changes: 16 additions & 0 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, ''];
Expand All @@ -35,6 +39,9 @@ public function __construct($number, $is_required = true)
$this->number = $number;
}

/**
* @return string|null
*/
public function value()
{
if ($this->number) {
Expand All @@ -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;
Expand Down
24 changes: 14 additions & 10 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@

namespace Equip\ValueObject;

use InvalidArgumentException;
use function Assert\that;

class Text
{
/**
* @var string|null
*/
private $value;

/**
* @param string|null $value
* @param string|null $regex
*/
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;
}

/**
* @return string|null
*/
public function value()
{
return $this->value;
Expand Down
3 changes: 1 addition & 2 deletions tests/EmailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testValid()

public function testNotRequired()
{
$email = '';
$email = null;
$vo = new EmailAddress($email, false);

$this->assertNull($vo->value());
Expand All @@ -28,7 +28,6 @@ public function dataInvalid()
'invalid string' => ['not an email address'],
'array' => [[]],
'object' => [new \stdClass],
'null' => [null],
];
}

Expand Down
17 changes: 2 additions & 15 deletions tests/IdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function dataValid()
{
return [
'integer' => [1],
'string' => ['2'],
'octal' => [0x1a8], // 424
];
}
Expand All @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down