AbstractModel.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /**
  13. * @param array $definition Service description
  14. * @param ShapeMap $shapeMap Shapemap used for creating shapes
  15. */
  16. public function __construct(array $definition, ShapeMap $shapeMap)
  17. {
  18. $this->definition = $definition;
  19. $this->shapeMap = $shapeMap;
  20. }
  21. public function toArray()
  22. {
  23. return $this->definition;
  24. }
  25. public function offsetGet($offset)
  26. {
  27. return isset($this->definition[$offset])
  28. ? $this->definition[$offset] : null;
  29. }
  30. public function offsetSet($offset, $value)
  31. {
  32. $this->definition[$offset] = $value;
  33. }
  34. public function offsetExists($offset)
  35. {
  36. return isset($this->definition[$offset]);
  37. }
  38. public function offsetUnset($offset)
  39. {
  40. unset($this->definition[$offset]);
  41. }
  42. protected function shapeAt($key)
  43. {
  44. if (!isset($this->definition[$key])) {
  45. throw new \InvalidArgumentException('Expected shape definition at '
  46. . $key);
  47. }
  48. return $this->shapeFor($this->definition[$key]);
  49. }
  50. protected function shapeFor(array $definition)
  51. {
  52. return isset($definition['shape'])
  53. ? $this->shapeMap->resolve($definition)
  54. : Shape::create($definition, $this->shapeMap);
  55. }
  56. }