ResourceContainer.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /**
  8. * Temando REST API JSON API Container
  9. *
  10. * Register deserialized resources
  11. *
  12. * @package Temando\Shipping\Rest
  13. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  14. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link http://www.temando.com/
  16. */
  17. class ResourceContainer implements ResourceContainerInterface
  18. {
  19. /**
  20. * @var AbstractResource[]
  21. */
  22. private $resources = [];
  23. /**
  24. * @param string $type
  25. * @param string $id
  26. * @return string
  27. */
  28. private function getResourceKey($type, $id)
  29. {
  30. return sprintf('%s--%s', $type, $id);
  31. }
  32. /**
  33. * Register a resource
  34. *
  35. * @param AbstractResource $resource
  36. * @return void
  37. */
  38. public function addResource(AbstractResource $resource)
  39. {
  40. $resourceKey = $this->getResourceKey($resource->getType(), $resource->getId());
  41. $this->resources[$resourceKey] = $resource;
  42. }
  43. /**
  44. * Obtain a registered resource identified by type and id properties.
  45. *
  46. * @param string $type
  47. * @param string $id
  48. * @return AbstractResource
  49. */
  50. public function getResource($type, $id)
  51. {
  52. $resourceKey = $this->getResourceKey($type, $id);
  53. if (!isset($this->resources[$resourceKey])) {
  54. return null;
  55. }
  56. return $this->resources[$resourceKey];
  57. }
  58. /**
  59. * Obtain all registered resources.
  60. *
  61. * @return AbstractResource[]
  62. */
  63. public function getResources()
  64. {
  65. return $this->resources;
  66. }
  67. }