Skip to content

Commit

Permalink
Mapping: added response entity, split into trait
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 18, 2018
1 parent 5a2f554 commit f5efcdc
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 62 deletions.
5 changes: 5 additions & 0 deletions src/Dispatcher/DecoratedDispatcher.php
Expand Up @@ -11,8 +11,10 @@
use Apitte\Core\Http\ApiRequest;
use Apitte\Core\Http\ApiResponse;
use Apitte\Core\Http\RequestAttributes;
use Apitte\Core\Mapping\Response\IResponseEntity;
use Apitte\Core\Router\IRouter;
use Apitte\Negotiation\Http\ArrayEntity;
use Apitte\Negotiation\Http\MappingEntity;
use Apitte\Negotiation\Http\ScalarEntity;
use Exception;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -97,11 +99,14 @@ protected function handle(ServerRequestInterface $request, ResponseInterface $re

// If result is array convert it manually to ArrayEntity,
// if result is scalar convert it manually to ScalarEntity,
// if result is IResponseEntity convert it manually to MappingEntity,
// otherwise use result as response
if (is_array($result)) {
$response = $response->withEntity(ArrayEntity::from($result));
} else if (is_scalar($result)) {
$response = $response->withEntity(ScalarEntity::from($result));
} else if ($result instanceof IResponseEntity) {
$response = $response->withEntity(MappingEntity::from($result));
} else {
$response = $result;
}
Expand Down
13 changes: 0 additions & 13 deletions src/Mapping/Arrayable.php

This file was deleted.

16 changes: 13 additions & 3 deletions src/Mapping/Request/AbstractEntity.php
Expand Up @@ -2,14 +2,24 @@

namespace Apitte\Core\Mapping\Request;

use Apitte\Core\Mapping\Arrayable;
use ArrayIterator;
use IteratorAggregate;
use Traversable;

abstract class AbstractEntity implements IRequestEntity, Arrayable
abstract class AbstractEntity implements IRequestEntity, IteratorAggregate
{

/**
* @return array
*/
abstract public function getProperties();
abstract public function toArray();

/**
* @return ArrayIterator|Traversable
*/
public function getIterator()
{
return new ArrayIterator($this->toArray());
}

}
51 changes: 5 additions & 46 deletions src/Mapping/Request/BasicEntity.php
Expand Up @@ -3,44 +3,20 @@
namespace Apitte\Core\Mapping\Request;

use Apitte\Core\Http\ApiRequest;
use Apitte\Core\Mapping\TReflectionProperties;
use Apitte\Core\Schema\Endpoint;
use ReflectionObject;

abstract class BasicEntity extends AbstractEntity
{

/** @var array */
protected $properties = [];
use TReflectionProperties;

/**
* @return array
*/
public function getProperties()
public function getRequestProperties()
{
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 $this->getProperties();
}

/**
Expand Down Expand Up @@ -69,7 +45,7 @@ public function factory(array $data)
$inst = new static();

// Fill properties with real data
$properties = $inst->getProperties();
$properties = $inst->getRequestProperties();
foreach ($properties as $property) {
if (!array_key_exists($property['name'], $data)) continue;

Expand All @@ -87,23 +63,6 @@ public function factory(array $data)
return $inst;
}

/**
* @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;
}

/**
* HELPERS *****************************************************************
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Mapping/Request/IRequestEntity.php
Expand Up @@ -7,6 +7,11 @@
interface IRequestEntity
{

/**
* @return array
*/
public function getRequestProperties();

/**
* @param ApiRequest $request
* @return static
Expand Down
25 changes: 25 additions & 0 deletions src/Mapping/Response/AbstractEntity.php
@@ -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());
}

}
28 changes: 28 additions & 0 deletions src/Mapping/Response/BasicEntity.php
@@ -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();
}

}
18 changes: 18 additions & 0 deletions src/Mapping/Response/IResponseEntity.php
@@ -0,0 +1,18 @@
<?php

namespace Apitte\Core\Mapping\Response;

interface IResponseEntity
{

/**
* @return array
*/
public function getResponseProperties();

/**
* @return array
*/
public function toResponse();

}
61 changes: 61 additions & 0 deletions src/Mapping/TReflectionProperties.php
@@ -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;
}

}

0 comments on commit f5efcdc

Please sign in to comment.