AbstractModel.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Aws\Api;
  3. /**
  4. * Base class that is used by most API shapes
  5. */
  6. abstract class AbstractModel implements \ArrayAccess
  7. {
  8. /** @var array */
  9. protected $definition;
  10. /** @var ShapeMap */
  11. protected $shapeMap;
  12. /** @var array */
  13. protected $contextParam;
  14. /**
  15. * @param array $definition Service description
  16. * @param ShapeMap $shapeMap Shapemap used for creating shapes
  17. */
  18. public function __construct(array $definition, ShapeMap $shapeMap)
  19. {
  20. $this->definition = $definition;
  21. $this->shapeMap = $shapeMap;
  22. if (isset($definition['contextParam'])) {
  23. $this->contextParam = $definition['contextParam'];
  24. }
  25. }
  26. public function toArray()
  27. {
  28. return $this->definition;
  29. }
  30. /**
  31. * @return mixed|null
  32. */
  33. #[\ReturnTypeWillChange]
  34. public function offsetGet($offset)
  35. {
  36. return isset($this->definition[$offset])
  37. ? $this->definition[$offset] : null;
  38. }
  39. /**
  40. * @return void
  41. */
  42. #[\ReturnTypeWillChange]
  43. public function offsetSet($offset, $value)
  44. {
  45. $this->definition[$offset] = $value;
  46. }
  47. /**
  48. * @return bool
  49. */
  50. #[\ReturnTypeWillChange]
  51. public function offsetExists($offset)
  52. {
  53. return isset($this->definition[$offset]);
  54. }
  55. /**
  56. * @return void
  57. */
  58. #[\ReturnTypeWillChange]
  59. public function offsetUnset($offset)
  60. {
  61. unset($this->definition[$offset]);
  62. }
  63. protected function shapeAt($key)
  64. {
  65. if (!isset($this->definition[$key])) {
  66. throw new \InvalidArgumentException('Expected shape definition at '
  67. . $key);
  68. }
  69. return $this->shapeFor($this->definition[$key]);
  70. }
  71. protected function shapeFor(array $definition)
  72. {
  73. return isset($definition['shape'])
  74. ? $this->shapeMap->resolve($definition)
  75. : Shape::create($definition, $this->shapeMap);
  76. }
  77. }