Skip to content

Commit

Permalink
Allow empty state for all value objects biberlabs#11 biberlabs#12
Browse files Browse the repository at this point in the history
  • Loading branch information
edigu committed Oct 14, 2016
1 parent 5d5a5e6 commit 1108752
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 39 deletions.
30 changes: 26 additions & 4 deletions src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Color implements JsonSerializable
*
* @ORM\Column(type="string", length=6, nullable=true)
*
* @var array
* @var string
*/
private $color;

Expand All @@ -43,8 +43,12 @@ class Color implements JsonSerializable
*
* @param string $hex
*/
public function __construct($hex)
public function __construct($hex = null)
{
if ($hex === null) {
return;
}

$hex = $this->normalize($hex);

if (!$this->isValidHex($hex)) {
Expand All @@ -61,16 +65,20 @@ public function __construct($hex)
*/
public function toHex()
{
return '#'.$this->color;
return ($this->color) ? '#'.$this->color : '';
}

/**
* Returns the color in RGB format
* Returns the color in RGB format, empty array if color is not set.
*
* @return array
*/
public function toRGB()
{
if ($this->isEmpty()) {
return [];
}

list($r, $g, $b) = sscanf($this->color, "%02x%02x%02x");

return [ $r, $g, $b ];
Expand Down Expand Up @@ -115,6 +123,10 @@ public function getName()
*/
public function toArray()
{
if ($this->isEmpty()) {
return [];
}

$rgb = $this->toRGB();

return [
Expand Down Expand Up @@ -160,6 +172,16 @@ public function __toString()
return $this->toHex();
}

/**
* Returns a boolean TRUE if color is literally empty
*
* @return boolean
*/
public function isEmpty()
{
return empty($this->color);
}

/**
* Returns distance between two colors.
*
Expand Down
37 changes: 31 additions & 6 deletions src/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ class DateRange implements JsonSerializable
* @param \DateTime $start
* @param \DateTime $end
*/
public function __construct(\DateTime $start, \DateTime $end)
public function __construct(\DateTime $start = null, \DateTime $end = null)
{
if ($start >= $end) {
if ($start === null || $end === null) {
return;
} elseif ($start >= $end) {
throw new \InvalidArgumentException('Start date is greater or equal to end date');
}

Expand Down Expand Up @@ -80,6 +82,10 @@ public function getDateTo()
*/
public function format($f = 'c')
{
if ($this->isEmpty()) {
return '';
}

return $this->getDateFrom()->format($f).' - '.$this->getDateTo()->format($f);
}

Expand All @@ -90,7 +96,7 @@ public function format($f = 'c')
*/
public function __toString()
{
return $this->getDateFrom()->format('c').' - '.$this->getDateTo()->format('c');
return $this->format('c');
}

/**
Expand All @@ -100,6 +106,10 @@ public function __toString()
*/
public function getDurationInSeconds()
{
if (!$this->dateFrom) {
return 0;
}

$interval = $this->dateFrom->diff($this->dateTo);

return ($interval->y * 365 * 24 * 60 * 60) +
Expand All @@ -115,11 +125,15 @@ public function getDurationInSeconds()
*
* @return array
*/
public function toArray()
public function toArray($format = 'c')
{
if ($this->isEmpty()) {
return [];
}

return [
'start' => $this->dateFrom,
'end' => $this->dateTo,
'start' => $this->getDateFrom()->format($format),
'end' => $this->getDateTo()->format($format),
];
}

Expand All @@ -132,4 +146,15 @@ public function jsonSerialize()
{
return $this->toArray();
}

/**
* Returns a boolean TRUE if the range instance is
* literally empty, FALSE otherwise.
*
* @return boolean
*/
public function isEmpty()
{
return !$this->dateFrom || !$this->dateTo;
}
}
10 changes: 5 additions & 5 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class EmailAddress
*
* @param string $email E-mail address
*/
public function __construct($email)
public function __construct($email = null)
{
// This is only a soft validation.
// This is only a soft validation to reduce headaches in
// You SHOULD sanitize & validate email before using it as a value object!
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
if ($email && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Given e-mail address '.$email.' is not a valid');
}

Expand All @@ -58,7 +58,7 @@ public function __toString()
*/
public function getDomain()
{
return explode('@', $this->address)[1];
return $this->address ? explode('@', $this->address)[1] : null;
}

/**
Expand All @@ -68,6 +68,6 @@ public function getDomain()
*/
public function getLocalPart()
{
return explode('@', $this->address)[0];
return $this->address ? explode('@', $this->address)[0] : null;
}
}
6 changes: 4 additions & 2 deletions src/Fullname.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ public function __toString()
* @param string $fullname Space separated full name. Ex: Steven Paul Jobs
* @param string $title
*/
public function __construct($fullname, $title = null)
public function __construct($fullname = null, $title = null)
{
$this->buildFromString($fullname);
if($fullname) {
$this->buildFromString($fullname);
}

if ($title) {
$this->setTitle($title);
Expand Down
12 changes: 6 additions & 6 deletions src/GeoPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class GeoPoint implements JsonSerializable
* @param float $lat Latitude
* @param float $lng Longitude
*/
public function __construct($lat = 0, $lng = 0)
public function __construct($lat = null, $lng = null)
{
if ($lat < -90.0 || $lat > 90.0 || $lng < -180.0 || $lng > 180.0) {
throw new \InvalidArgumentException('Given latitude longitude pair is invalid.');
}

if ($lat && $lng) {
if ($lat < -90.0 || $lat > 90.0 || $lng < -180.0 || $lng > 180.0) {
throw new \InvalidArgumentException('Given latitude longitude pair is invalid.');
}

$this->point = [
'lat' => (float) $lat,
'lng' => (float) $lng,
Expand All @@ -56,7 +56,7 @@ public function __construct($lat = 0, $lng = 0)
public function toElastic()
{
if (empty($this->point)) {
return null;
return [];
}

return [
Expand Down
8 changes: 6 additions & 2 deletions src/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ class IpAddress
*
* @param string $addr IP address.
*/
public function __construct($addr)
public function __construct($addr = null)
{
if (!$addr) {
return; // early..
}

$filtered = filter_var($addr, FILTER_VALIDATE_IP);
if ($filtered === false) {
throw new \InvalidArgumentException('Given IP '.$addr.' is not a valid IP address');
Expand All @@ -53,6 +57,6 @@ public function __construct($addr)
*/
public function __toString()
{
return $this->address;
return $this->address ?: '';
}
}
26 changes: 23 additions & 3 deletions src/IpRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class IpRange implements JsonSerializable
* @param IpAddress $startIp
* @param IpAddress $endIp
*/
public function __construct(IpAddress $startIp, IpAddress $endIp)
public function __construct(IpAddress $startIp = null, IpAddress $endIp = null)
{
$this->startIp = $startIp;
$this->endIp = $endIp;
Expand Down Expand Up @@ -90,10 +90,16 @@ public static function createFromCidrNotation($cidr)
return new IpRange(new IpAddress($start), new IpAddress($end));
}

// Cast the range to a CIDR notation
/**
* String representation of a range.
*
* Example output: "192.168.0.10 - 192.168.0.255"
*
* @return string
*/
public function __toString()
{
// TBD
return $this->isEmpty() ? '' : (string) $this->startIp.' - '. $this->endIp;
}

/**
Expand All @@ -103,12 +109,26 @@ public function __toString()
*/
public function toArray()
{
if ($this->isEmpty()) {
return [];
}

return [
'startIp' => (string) $this->getStartIp(),
'endIp' => (string) $this->getEndIp(),
];
}

/**
* Returns boolean TRUE if the range is empty, false otherwise.
*
* @return boolean
*/
public function isEmpty()
{
return $this->startIp === null || $this->endIp === null;
}

/**
* Implement json serializable interface.
*
Expand Down
19 changes: 14 additions & 5 deletions tests/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,34 @@ public function testAlwaysNormalizeHexValues($val, $normalized)
public function testRGBConversionWorks()
{
$color = new Color('FFF');
$this->assertEquals([255,255,255], $color->toRGB());
$this->assertEquals([255, 255, 255], $color->toRGB());
$this->assertEquals('rgb(255,255,255)', $color->toRGBString());
$this->assertInstanceof(\JsonSerializable::class, $color);
$this->assertInstanceOf(\JsonSerializable::class, $color);
$this->assertEquals($color->jsonSerialize(), $color->toArray());
}

public function testFactory()
public function testFactory()
{
$color = Color::fromRGB(255,255,255);
$color = Color::fromRGB(255, 255, 255);
$this->assertEquals('#FFFFFF', $color->toHex());
}

public function testEmptyState()
{
$color = new Color();
$this->assertInstanceOf(Color::class, $color);
$this->assertEquals([], $color->toArray());
$this->assertEquals([], $color->toRGB());
$this->assertEquals('', (string) $color);
}

/**
* @dataProvider sampleColorNames
*/
public function testSerializedColorHasAName($hex, $name)
{
$color = new Color($hex);
$arr = $color->toArray();
$arr = $color->toArray();
$this->assertEquals($arr['name'], $name);
}

Expand Down
23 changes: 17 additions & 6 deletions tests/DateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,32 @@

class DateRangeTest extends \PHPUnit_Framework_TestCase
{
public function testDateRages()
public function testDateRanges()
{
$start = new \DateTime('31 December 2016');
$end = new \DateTime('1 January 2017');
$start = new \DateTime('31 December 2016');
$end = new \DateTime('1 January 2017');
$startStr = '2016-12-31T00:00:00+00:00';
$endStr = '2017-01-01T00:00:00+00:00';
$obj = new DateRange($start, $end);

$obj = new DateRange($start, $end);
$this->assertEquals('2016 - 2017', $obj->format('Y'));
$this->assertEquals('2016-12 - 2017-01', $obj->format('Y-m'));
$this->assertStringStartsWith('2016-12-31', $obj->format());
$this->assertEquals(86400, $obj->getDurationInSeconds());
$this->assertEquals($obj->toArray(), ['start' => $start, 'end' => $end]);
$this->assertEquals($obj->jsonSerialize(), ['start' => $start, 'end' => $end]);
$this->assertEquals($obj->toArray(), ['start' => $startStr, 'end' => $endStr]);
$this->assertEquals($obj->jsonSerialize(), ['start' => $startStr, 'end' => $endStr]);
$this->assertEquals((string) $obj, $start->format('c') . ' - ' . $end->format('c'));
}

public function testEmptyState()
{
$obj = new DateRange();
$this->assertInstanceOf(DateRange::class, $obj);
$this->assertEquals([], $obj->toArray());
$this->assertEquals('', (string) $obj);
$this->assertEquals(0, $obj->getDurationInSeconds());
}

public function testOlderDateRages()
{
$start = new \DateTime('23 April 1923');
Expand Down
6 changes: 6 additions & 0 deletions tests/EmailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function testValidAddressesAreAccepted($email, $domain, $localPart)
$this->assertEquals($localPart, $obj->getLocalPart());
}

public function testEmptyState()
{
$addr = new EmailAddress();
$this->assertInstanceOf(EmailAddress::class, $addr);
}

public function invalidEmailProvider()
{
return [
Expand Down
Loading

0 comments on commit 1108752

Please sign in to comment.