RelationshipHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\JsonApi;
  6. use Temando\Shipping\Rest\Response\DataObject\AbstractResource;
  7. use Temando\Shipping\Rest\SchemaMapper\Reflection\PropertyHandlerInterface;
  8. /**
  9. * Temando REST API JSON API Relationship Handler
  10. *
  11. * @package Temando\Shipping\Rest
  12. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  13. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. * @link https://www.temando.com/
  15. */
  16. class RelationshipHandler
  17. {
  18. /**
  19. * @var PropertyHandlerInterface
  20. */
  21. private $propertyHandler;
  22. /**
  23. * RelationshipHandler constructor.
  24. * @param PropertyHandlerInterface $propertyHandler
  25. */
  26. public function __construct(PropertyHandlerInterface $propertyHandler)
  27. {
  28. $this->propertyHandler = $propertyHandler;
  29. }
  30. /**
  31. * Add related data to a resource.
  32. *
  33. * If the related resource is contained in the "included" section, add the
  34. * full resource. Otherwise add the resource ID only.
  35. *
  36. * @param AbstractResource $resource
  37. * @param ResourceContainerInterface $relatedResources
  38. * @return void
  39. */
  40. public function addRelationships(AbstractResource $resource, ResourceContainerInterface $relatedResources)
  41. {
  42. // iterate over each resource's relationships
  43. foreach ($resource->getRelationships() as $relationship) {
  44. // replace each relationship by its actual resource representation
  45. $related = [];
  46. $relatedIds = [];
  47. // collect related resources by resource type
  48. foreach ($relationship->getData() as $relationshipIdentifier) {
  49. $resourceType = $relationshipIdentifier->getType();
  50. $relatedResource = $relatedResources->getResource(
  51. $relationshipIdentifier->getType(),
  52. $relationshipIdentifier->getId()
  53. );
  54. if (!$relatedResource) {
  55. // resource not included, add resource ID
  56. if (!isset($relatedIds[$resourceType])) {
  57. $relatedIds[$resourceType] = [];
  58. }
  59. $relatedIds[$resourceType][]= $relationshipIdentifier->getId();
  60. } else {
  61. // resource included, add full resource
  62. if (!isset($related[$resourceType])) {
  63. $related[$resourceType] = [];
  64. }
  65. $related[$resourceType][]= $relatedResource;
  66. }
  67. }
  68. // set related resources by resource type
  69. foreach ($related as $resourceType => $relatedResources) {
  70. $setter = $this->propertyHandler->setter($resourceType);
  71. $method = "{$setter}s";
  72. if (method_exists($resource, $method)) {
  73. call_user_func([$resource, $method], $relatedResources);
  74. }
  75. }
  76. // set related resource IDs by resource type
  77. foreach ($relatedIds as $resourceType => $relatedResourceIds) {
  78. $setter = $this->propertyHandler->setter($resourceType);
  79. $method = "{$setter}Ids";
  80. if (method_exists($resource, $method)) {
  81. call_user_func([$resource, $method], $relatedResourceIds);
  82. }
  83. }
  84. }
  85. }
  86. }