AbstractParser.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\SchemaMapper;
  6. use Temando\Shipping\Rest\SchemaMapper\Reflection\PropertyHandlerInterface;
  7. use Temando\Shipping\Rest\SchemaMapper\Reflection\TypeHandlerInterface;
  8. /**
  9. * Temando Data Parser
  10. *
  11. * @package Temando\Shipping\Rest
  12. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  13. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. * @link http://www.temando.com/
  15. */
  16. abstract class AbstractParser implements ParserInterface
  17. {
  18. /**
  19. * @var PropertyHandlerInterface
  20. */
  21. private $propertyHandler;
  22. /**
  23. * @var TypeHandlerInterface
  24. */
  25. private $typeHandler;
  26. /**
  27. * AbstractParser constructor.
  28. * @param PropertyHandlerInterface $propertyHandler
  29. * @param TypeHandlerInterface $typeHandler
  30. */
  31. public function __construct(
  32. PropertyHandlerInterface $propertyHandler,
  33. TypeHandlerInterface $typeHandler
  34. ) {
  35. $this->propertyHandler = $propertyHandler;
  36. $this->typeHandler = $typeHandler;
  37. }
  38. /**
  39. * Convert the string representation of a given type. The input data format
  40. * (xml, json, etc.) is handled by the concrete parser class.
  41. *
  42. * @param string $data The data to be parsed
  43. * @param string $type The type (interface) to map the data to
  44. * @return object The object with populated properties
  45. */
  46. abstract public function parse($data, $type);
  47. /**
  48. * Copy the properties to an object of the given type.
  49. *
  50. * @param mixed[] $properties Associated array of property keys and values.
  51. * @param string $type The type of the target object.
  52. * @return object The target object with all properties set.
  53. */
  54. public function parseProperties(array $properties, $type)
  55. {
  56. $dataObj = $this->typeHandler->create($type);
  57. // named type
  58. foreach ($properties as $key => $value) {
  59. $subType = $this->typeHandler->getPropertyType($this->propertyHandler, $dataObj, $key);
  60. if (!$subType) {
  61. continue;
  62. }
  63. $subMetaType = $this->typeHandler->getPropertyMetaType($this->propertyHandler, $dataObj, $key);
  64. if ($subMetaType == TypeHandlerInterface::META_TYPE_OBJECT) {
  65. $value = $this->parseProperties($value, $subType);
  66. } elseif ($subMetaType == TypeHandlerInterface::META_TYPE_OBJECT_ARRAY) {
  67. $subType = rtrim($subType, '[]');
  68. $types = [];
  69. foreach ($value as $item) {
  70. $types[]= $this->parseProperties($item, $subType);
  71. }
  72. $value = $types;
  73. }
  74. // set value
  75. $setter = $this->propertyHandler->setter($key);
  76. call_user_func([$dataObj, $setter], $value);
  77. }
  78. return $dataObj;
  79. }
  80. }