-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement serializer, use serializer to deserialize param conve…
…rter objects
- Loading branch information
Showing
18 changed files
with
377 additions
and
59 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 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
70 changes: 70 additions & 0 deletions
70
models/classes/ParamConverter/Context/ObjectFactoryContext.php
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\tao\model\ParamConverter\Context; | ||
|
||
use InvalidArgumentException; | ||
use oat\tao\model\Context\AbstractContext; | ||
|
||
class ObjectFactoryContext extends AbstractContext implements ObjectFactoryContextInterface | ||
{ | ||
public const PARAM_CLASS = 'class'; | ||
public const PARAM_DATA = 'data'; | ||
public const PARAM_FORMAT = 'format'; | ||
public const PARAM_CONTEXT = 'context'; | ||
|
||
public function getClass(): string | ||
{ | ||
return $this->getParameter(self::PARAM_CLASS); | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return $this->getParameter(self::PARAM_DATA); | ||
} | ||
|
||
public function getFormat(): string | ||
{ | ||
return $this->getParameter(self::PARAM_FORMAT, 'json'); | ||
} | ||
|
||
public function getContext(): array | ||
{ | ||
return $this->getParameter(self::PARAM_CONTEXT, []); | ||
} | ||
|
||
protected function getSupportedParameters(): array | ||
{ | ||
return [ | ||
self::PARAM_CLASS, | ||
self::PARAM_DATA, | ||
self::PARAM_FORMAT, | ||
self::PARAM_CONTEXT, | ||
]; | ||
} | ||
|
||
protected function validateParameter(string $parameter, $parameterValue): void | ||
{ | ||
if ( | ||
in_array($parameter, [self::PARAM_CLASS, self::PARAM_FORMAT], true) | ||
&& is_string($parameterValue) | ||
) { | ||
return; | ||
} | ||
|
||
if ( | ||
in_array($parameter, [self::PARAM_DATA, self::PARAM_CONTEXT], true) | ||
&& is_array($parameterValue) | ||
) { | ||
return; | ||
} | ||
|
||
throw new InvalidArgumentException( | ||
sprintf( | ||
'Context parameter %s is not valid.', | ||
$parameter | ||
) | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
models/classes/ParamConverter/Context/ObjectFactoryContextInterface.php
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\tao\model\ParamConverter\Context; | ||
|
||
interface ObjectFactoryContextInterface | ||
{ | ||
public function getClass(): string; | ||
|
||
public function getData(): array; | ||
|
||
public function getFormat(): string; | ||
|
||
public function getContext(): array; | ||
} |
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
16 changes: 16 additions & 0 deletions
16
models/classes/ParamConverter/Context/ParamConverterListenerContextInterface.php
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\tao\model\ParamConverter\Context; | ||
|
||
use oat\tao\model\HttpFoundation\Request\RequestInterface; | ||
|
||
interface ParamConverterListenerContextInterface | ||
{ | ||
public function getRequest(): RequestInterface; | ||
|
||
public function getController(): string; | ||
|
||
public function getMethod(): string; | ||
} |
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\tao\model\ParamConverter\Factory; | ||
|
||
use ReflectionClass; | ||
use InvalidArgumentException; | ||
use oat\tao\model\Serializer\SerializerInterface; | ||
use oat\tao\model\ParamConverter\Context\ObjectFactoryContextInterface; | ||
|
||
class ObjectFactory implements ObjectFactoryInterface | ||
{ | ||
/** @var SerializerInterface */ | ||
private $serializer; | ||
|
||
public function __construct(SerializerInterface $serializer) | ||
{ | ||
$this->serializer = $serializer; | ||
} | ||
|
||
public function create(ObjectFactoryContextInterface $context): object | ||
{ | ||
$constructorArgs = []; | ||
$data = $context->getData(); | ||
|
||
$reflectionClass = new ReflectionClass($context->getClass()); | ||
$constructor = $reflectionClass->getConstructor(); | ||
|
||
if ($constructor) { | ||
foreach ($constructor->getParameters() as $constructorParameter) { | ||
$constructorParameterName = $constructorParameter->getName(); | ||
|
||
if (array_key_exists($constructorParameterName, $data)) { | ||
$constructorArgs[$constructorParameterName] = $data[$constructorParameterName]; | ||
unset($data[$constructorParameterName]); | ||
} | ||
} | ||
} | ||
|
||
$instance = $reflectionClass->newInstanceArgs($constructorArgs); | ||
|
||
foreach ($data as $queryParameter => $value) { | ||
if ($reflectionClass->hasMethod('set' . $queryParameter)) { | ||
$reflectionClass | ||
->getMethod('set' . $queryParameter) | ||
->invoke($instance, $value); | ||
} elseif ($reflectionClass->hasProperty($queryParameter)) { | ||
$reflectionClass->getProperty($queryParameter)->setValue($instance, $value); | ||
} | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
public function deserialize(ObjectFactoryContextInterface $context): object | ||
{ | ||
$format = $context->getFormat(); | ||
|
||
if ($format !== 'json') { | ||
throw new InvalidArgumentException('Currently, only the "json" format is supported.'); | ||
} | ||
|
||
return $this->serializer->deserialize( | ||
json_encode($context->getData()), | ||
$context->getClass(), | ||
$format, | ||
$context->getContext() | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
models/classes/ParamConverter/Factory/ObjectFactoryInterface.php
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\tao\model\ParamConverter\Factory; | ||
|
||
use oat\tao\model\ParamConverter\Context\ObjectFactoryContextInterface; | ||
|
||
interface ObjectFactoryInterface | ||
{ | ||
public function create(ObjectFactoryContextInterface $context): object; | ||
|
||
public function deserialize(ObjectFactoryContextInterface $context): object; | ||
} |
Oops, something went wrong.