1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Common; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @package Commercetools\Core\Model\Common |
10
|
|
|
* |
11
|
|
|
* @method string getType() |
12
|
|
|
* @method GeoLocation setType(string $type = null) |
13
|
|
|
* @method array getCoordinates() |
14
|
|
|
* @method GeoLocation setCoordinates(array $coordinates = null) |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
class GeoLocation extends JsonObject |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
const TYPE_NAME = 'Point'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $data |
22
|
|
|
* @param Context|callable $context |
23
|
|
|
*/ |
24
|
3 |
|
public function __construct(array $data = [], $context = null) |
25
|
|
|
{ |
26
|
3 |
|
parent::__construct($data, $context); |
27
|
3 |
|
$name = static::TYPE_NAME; |
28
|
3 |
|
if (!empty($name)) { |
29
|
3 |
|
$this->setType(static::TYPE_NAME); |
30
|
|
|
} |
31
|
3 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
3 |
|
public function fieldDefinitions() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
3 |
|
'type' => [static::TYPE => 'string'], |
40
|
3 |
|
'coordinates' => [static::TYPE => 'array'] |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $data |
46
|
|
|
* @param Context|callable $context |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
1 |
|
public static function fromArray(array $data, $context = null) |
50
|
|
|
{ |
51
|
1 |
|
if (isset($data['type'])) { |
52
|
1 |
|
$className = static::getGeoTypeByTypeName($data['type']); |
53
|
1 |
|
if (class_exists($className)) { |
54
|
1 |
|
return new $className($data, $context); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
return new static($data, $context); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
protected static function getGeoTypeByTypeName($apiType) |
61
|
|
|
{ |
62
|
1 |
|
$className = '\Commercetools\Core\Model\Common\\Geo' . ucfirst($apiType); |
63
|
1 |
|
return $className; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.