| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Api; |
| 4 |
|
| 5 |
/** |
| 6 |
* Builds shape based on shape references. |
| 7 |
*/ |
| 8 |
class ShapeMap |
| 9 |
{ |
| 10 |
/** @var array */ |
| 11 |
private $definitions; |
| 12 |
/** @var Shape[] */ |
| 13 |
private $simple; |
| 14 |
/** |
| 15 |
* @param array $shapeModels Associative array of shape definitions. |
| 16 |
*/ |
| 17 |
public function __construct(array $shapeModels) |
| 18 |
{ |
| 19 |
$this->definitions = $shapeModels; |
| 20 |
} |
| 21 |
/** |
| 22 |
* Get an array of shape names. |
| 23 |
* |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public function getShapeNames() |
| 27 |
{ |
| 28 |
return \array_keys($this->definitions); |
| 29 |
} |
| 30 |
/** |
| 31 |
* Resolve a shape reference |
| 32 |
* |
| 33 |
* @param array $shapeRef Shape reference shape |
| 34 |
* |
| 35 |
* @return Shape |
| 36 |
* @throws \InvalidArgumentException |
| 37 |
*/ |
| 38 |
public function resolve(array $shapeRef) |
| 39 |
{ |
| 40 |
$shape = $shapeRef['shape']; |
| 41 |
if (!isset($this->definitions[$shape])) { |
| 42 |
throw new \InvalidArgumentException('Shape not found: ' . $shape); |
| 43 |
} |
| 44 |
$isSimple = \count($shapeRef) == 1; |
| 45 |
if ($isSimple && isset($this->simple[$shape])) { |
| 46 |
return $this->simple[$shape]; |
| 47 |
} |
| 48 |
$definition = $shapeRef + $this->definitions[$shape]; |
| 49 |
$definition['name'] = $definition['shape']; |
| 50 |
if (isset($definition['shape'])) { |
| 51 |
unset($definition['shape']); |
| 52 |
} |
| 53 |
$result = Shape::create($definition, $this); |
| 54 |
if ($isSimple) { |
| 55 |
$this->simple[$shape] = $result; |
| 56 |
} |
| 57 |
return $result; |
| 58 |
} |
| 59 |
} |
| 60 |
|