@@ 16-65 (lines=50) @@ | ||
13 | * @method array getCoordinates() |
|
14 | * @method GeoLocation setCoordinates(array $coordinates = null) |
|
15 | */ |
|
16 | class GeoLocation extends JsonObject |
|
17 | { |
|
18 | const TYPE_NAME = 'Point'; |
|
19 | ||
20 | /** |
|
21 | * @param array $data |
|
22 | * @param Context|callable $context |
|
23 | */ |
|
24 | public function __construct(array $data = [], $context = null) |
|
25 | { |
|
26 | parent::__construct($data, $context); |
|
27 | $name = static::TYPE_NAME; |
|
28 | if (!empty($name)) { |
|
29 | $this->setType(static::TYPE_NAME); |
|
30 | } |
|
31 | } |
|
32 | ||
33 | /** |
|
34 | * @return array |
|
35 | */ |
|
36 | public function fieldDefinitions() |
|
37 | { |
|
38 | return [ |
|
39 | 'type' => [static::TYPE => 'string'], |
|
40 | 'coordinates' => [static::TYPE => 'array'] |
|
41 | ]; |
|
42 | } |
|
43 | ||
44 | /** |
|
45 | * @param array $data |
|
46 | * @param Context|callable $context |
|
47 | * @return static |
|
48 | */ |
|
49 | public static function fromArray(array $data, $context = null) |
|
50 | { |
|
51 | if (isset($data['type'])) { |
|
52 | $className = static::getGeoTypeByTypeName($data['type']); |
|
53 | if (class_exists($className)) { |
|
54 | return new $className($data, $context); |
|
55 | } |
|
56 | } |
|
57 | return new static($data, $context); |
|
58 | } |
|
59 | ||
60 | protected static function getGeoTypeByTypeName($apiType) |
|
61 | { |
|
62 | $className = '\Commercetools\Core\Model\Common\\Geo' . ucfirst($apiType); |
|
63 | return $className; |
|
64 | } |
|
65 | } |
|
66 |
@@ 17-67 (lines=51) @@ | ||
14 | * @method string getName() |
|
15 | * @method FieldType setName(string $name = null) |
|
16 | */ |
|
17 | class FieldType extends JsonObject |
|
18 | { |
|
19 | const NAME = ''; |
|
20 | ||
21 | /** |
|
22 | * @param array $data |
|
23 | * @param Context|callable $context |
|
24 | */ |
|
25 | public function __construct(array $data = [], $context = null) |
|
26 | { |
|
27 | parent::__construct($data, $context); |
|
28 | $name = static::NAME; |
|
29 | if (!empty($name)) { |
|
30 | $this->setName(static::NAME); |
|
31 | } |
|
32 | } |
|
33 | ||
34 | public function fieldDefinitions() |
|
35 | { |
|
36 | return [ |
|
37 | 'name' => [static::TYPE => 'string'], |
|
38 | ]; |
|
39 | } |
|
40 | ||
41 | public function fieldTypeDefinition() |
|
42 | { |
|
43 | return []; |
|
44 | } |
|
45 | ||
46 | /** |
|
47 | * @param array $data |
|
48 | * @param Context|callable $context |
|
49 | * @return static |
|
50 | */ |
|
51 | public static function fromArray(array $data, $context = null) |
|
52 | { |
|
53 | if (isset($data['name'])) { |
|
54 | $className = static::getTypeByApiType($data['name']); |
|
55 | if (class_exists($className)) { |
|
56 | return new $className($data, $context); |
|
57 | } |
|
58 | } |
|
59 | return new static($data, $context); |
|
60 | } |
|
61 | ||
62 | protected static function getTypeByApiType($apiType) |
|
63 | { |
|
64 | $className = '\Commercetools\Core\Model\Type\\' . ucfirst($apiType) . 'Type'; |
|
65 | return $className; |
|
66 | } |
|
67 | } |
|
68 |