1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Shopify; |
4
|
|
|
|
5
|
|
|
use Helix\Shopify\Base\AbstractEntity; |
6
|
|
|
use Helix\Shopify\Base\AbstractEntity\CrudTrait; |
7
|
|
|
use Helix\Shopify\Base\AbstractEntity\MetafieldTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A metafield. |
11
|
|
|
* |
12
|
|
|
* @see https://shopify.dev/docs/admin-api/rest/reference/metafield |
13
|
|
|
* |
14
|
|
|
* @see MetafieldTrait::newMetafield() |
15
|
|
|
* |
16
|
|
|
* @method $this setNamespace (string $ns) @depends create-only |
17
|
|
|
* @method $this setKey (string $key) @depends create-only |
18
|
|
|
* |
19
|
|
|
* @method string getCreatedAt () |
20
|
|
|
* @method string getDescription () |
21
|
|
|
* @method string getKey () |
22
|
|
|
* @method string getNamespace () |
23
|
|
|
* @method string getOwnerId () injected |
24
|
|
|
* @method string getOwnerResource() injected |
25
|
|
|
* @method string getUpdatedAt () |
26
|
|
|
* @method string getValueType () |
27
|
|
|
* |
28
|
|
|
* @method $this setDescription (string $description) |
29
|
|
|
*/ |
30
|
|
|
class Metafield extends AbstractEntity |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
use CrudTrait; |
34
|
|
|
|
35
|
|
|
const TYPE = 'metafield'; |
36
|
|
|
const DIR = 'metafields'; |
37
|
|
|
|
38
|
|
|
const TYPE_INT = 'integer'; |
39
|
|
|
const TYPE_STRING = 'string'; |
40
|
|
|
const TYPE_JSON = 'json_string'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var AbstractEntity |
44
|
|
|
*/ |
45
|
|
|
protected $container; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param AbstractEntity $container |
49
|
|
|
* @param array $data |
50
|
|
|
*/ |
51
|
|
|
public function __construct(AbstractEntity $container, array $data = []) |
52
|
|
|
{ |
53
|
|
|
$this->container = $container; |
54
|
|
|
parent::__construct($container, $data); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
final public function __toString(): string |
58
|
|
|
{ |
59
|
|
|
return "metafields/{$this->getId()}"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function _container() |
63
|
|
|
{ |
64
|
|
|
return $this->container; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function _dir(): string |
68
|
|
|
{ |
69
|
|
|
if ($this->container instanceof Shop) { |
70
|
|
|
return 'metafields'; |
71
|
|
|
} |
72
|
|
|
return "{$this->container}/metafields"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return AbstractEntity |
77
|
|
|
*/ |
78
|
|
|
public function getResource() |
79
|
|
|
{ |
80
|
|
|
return $this->container; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function getValue() |
87
|
|
|
{ |
88
|
|
|
if ($this->isJson()) { |
89
|
|
|
return json_decode($this->data['value'] ?? 'null', true, JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR); |
90
|
|
|
} |
91
|
|
|
return $this->data['value'] ?? null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
final public function isInt(): bool |
98
|
|
|
{ |
99
|
|
|
return $this->getValueType() === self::TYPE_INT; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
final public function isJson(): bool |
106
|
|
|
{ |
107
|
|
|
return $this->getValueType() === self::TYPE_JSON; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
final public function isString(): bool |
114
|
|
|
{ |
115
|
|
|
return $this->getValueType() === self::TYPE_STRING; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param mixed $value |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function setValue($value) |
123
|
|
|
{ |
124
|
|
|
if (is_int($value)) { |
125
|
|
|
$this->_set('value_type', self::TYPE_INT); |
126
|
|
|
return $this->_set('value', $value); |
127
|
|
|
} |
128
|
|
|
if (is_string($value)) { |
129
|
|
|
$this->_set('value_type', self::TYPE_STRING); |
130
|
|
|
return $this->_set('value', $value); |
131
|
|
|
} |
132
|
|
|
$this->_set('value_type', self::TYPE_JSON); |
133
|
|
|
return $this->_set('value', json_encode($value, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)); |
134
|
|
|
} |
135
|
|
|
} |