Skip to content

Commit

Permalink
Merge pull request #3 from VladimirBerdnik/master
Browse files Browse the repository at this point in the history
Add __isset() method to DTO class
  • Loading branch information
populov committed Dec 26, 2018
2 parents 5e105e7 + bd7630d commit a75bdcf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Changes History

1.1.1
-----
Add **__isset** method to DTO to have proper properties check on object
Improve DTO class description

1.1.0
_____
-----
Add PartialUpdateDto

1.0.9
Expand Down
41 changes: 35 additions & 6 deletions src/Dto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

namespace Saritasa;

use JsonSerializable;
use ReflectionClass;
use Saritasa\Exceptions\NotImplementedException;
use Saritasa\Traits\SimpleJsonSerialize;

/**
* Inherit this class and define protected fields to get read-only DTO
* Parent class of data transfer object. Can be used to transfer data between application layers.
* Inherit class and define protected properties to have read-only DTO.
*/
abstract class Dto implements \JsonSerializable
abstract class Dto implements JsonSerializable
{
use SimpleJsonSerialize;

protected static $propertiesCache;

/**
* Parent class of data transfer object. Can be used to transfer data between application layers.
* Inherit class and define protected properties to have read-only DTO.
*
* @param mixed[] $data DTO properties values
*/
public function __construct(array $data)
{
foreach (static::getInstanceProperties() as $key) {
Expand All @@ -23,22 +32,29 @@ public function __construct(array $data)
}
}

public function toArray()
/**
* Returns array representation of DTO properties.
*
* @return mixed[]
*/
public function toArray(): array
{
$result = [];
foreach (static::getInstanceProperties() as $key) {
$result[$key] = $this->$key;
}

return $result;
}


/**
* All instance fields are available as read-only properties
*
* @param string $name Name of private field to return
*
* @return mixed
* @throws NotImplementedException
*
* @throws NotImplementedException When requested property is not defined in DTO
*/
public function __get($name)
{
Expand All @@ -49,19 +65,32 @@ public function __get($name)
}
}

/**
* Returns whether given property is set in DTO.
*
* @param string $name Property name to check
*
* @return boolean
*/
public function __isset(string $name): bool
{
return in_array($name, static::getInstanceProperties()) && $this->$name !== null;
}

protected static function getInstanceProperties(): array
{
$class = static::class;
if (!isset(static::$propertiesCache[$class])) {
$cache = [];
$reflect = new \ReflectionClass($class);
$reflect = new ReflectionClass($class);
foreach ($reflect->getProperties() as $property) {
if (!$property->isStatic()) {
$cache[] = $property->getName();
}
}
static::$propertiesCache[$class] = $cache;
}

return static::$propertiesCache[$class];
}
}
2 changes: 1 addition & 1 deletion src/PartialUpdateDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getUpdatedFields()
return $this->updatedFields;
}

public function toArray()
public function toArray(): array
{
$result = [];
foreach ($this->updatedFields as $key) {
Expand Down
14 changes: 13 additions & 1 deletion tests/DtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testToArray()
$this->assertEquals('value1', $array['field1']);
$this->assertEquals(2, $array['field2']);
}

public function testToJson()
{
$dto = new ExampleDto([
Expand All @@ -86,6 +86,18 @@ public function testFieldsAreReadonly()
$this->expectException(\Error::class);
$dto->field1 = "test";
}

public function testIsSet()
{
$dto = new ExampleDto([
'field1' => 'value1',
'field2' => null
]);

$this->assertTrue(isset($dto->field1));
$this->assertFalse(isset($dto->field2));
$this->assertFalse(isset($dto->field3));
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/PartialUpdateDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public function testRememberSourceFields()
Assert::assertEquals(1, count(array_keys($arr)));
Assert::assertEquals('data2', $arr['field2']);
}

public function testGetUpdatedFields()
{
$data = new ExamplePartialDto(['field2' => 'data2']);

Assert::assertEquals(['field2'], $data->getUpdatedFields());
}
}

class ExamplePartialDto extends PartialUpdateDto {
Expand Down

0 comments on commit a75bdcf

Please sign in to comment.