Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mapping: added response entity, split into trait
- Loading branch information
Showing
9 changed files
with
160 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Apitte\Core\Mapping\Response; | ||
|
||
use ArrayIterator; | ||
use IteratorAggregate; | ||
use Traversable; | ||
|
||
abstract class AbstractEntity implements IResponseEntity, IteratorAggregate | ||
{ | ||
|
||
/** | ||
* @return array | ||
*/ | ||
abstract public function toArray(); | ||
|
||
/** | ||
* @return ArrayIterator|Traversable | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new ArrayIterator($this->toArray()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Apitte\Core\Mapping\Response; | ||
|
||
use Apitte\Core\Mapping\TReflectionProperties; | ||
|
||
abstract class BasicEntity extends AbstractEntity | ||
{ | ||
|
||
use TReflectionProperties; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getResponseProperties() | ||
{ | ||
return $this->getProperties(); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function toResponse() | ||
{ | ||
return $this->toArray(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Apitte\Core\Mapping\Response; | ||
|
||
interface IResponseEntity | ||
{ | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getResponseProperties(); | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function toResponse(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Apitte\Core\Mapping; | ||
|
||
use ReflectionObject; | ||
|
||
trait TReflectionProperties | ||
{ | ||
|
||
/** @var array */ | ||
protected $properties = []; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getProperties() | ||
{ | ||
if (!$this->properties) { | ||
$properties = []; | ||
$rf = new ReflectionObject($this); | ||
$class = get_class($this); | ||
|
||
$defaultProperties = $rf->getDefaultProperties(); | ||
foreach ($rf->getProperties() as $property) { | ||
// If property is not from the latest child, then skip it. | ||
if ($property->getDeclaringClass()->getName() !== $class) continue; | ||
|
||
// If property is not public, then skip it. | ||
if (!$property->isPublic()) continue; | ||
|
||
$properties[$property->getName()] = [ | ||
'name' => $property->getName(), | ||
'type' => $property->getValue($this), | ||
'defaultValue' => isset($defaultProperties[$property->getName()]) ? $defaultProperties[$property->getName()] : NULL, | ||
]; | ||
} | ||
|
||
$this->properties = $properties; | ||
} | ||
|
||
return $this->properties; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function toArray() | ||
{ | ||
$data = []; | ||
$properties = $this->getProperties(); | ||
|
||
foreach ($properties as $property) { | ||
if (!isset($this->{$property['name']})) continue; | ||
|
||
$data[$property['name']] = $this->{$property['name']}; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
} |