Passed
Push — master ( 199ece...6bc7da )
by y
02:13
created

Metafield::_container()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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
    use CrudTrait;
33
34
    const TYPE = 'metafield';
35
    const DIR = 'metafields';
36
37
    const TYPE_INT = 'integer';
38
    const TYPE_STRING = 'string';
39
    const TYPE_JSON = 'json_string';
40
41
    /**
42
     * @var AbstractEntity
43
     */
44
    protected $container;
45
46
    /**
47
     * @param AbstractEntity $container
48
     * @param array $data
49
     */
50
    public function __construct (AbstractEntity $container, array $data = []) {
51
        $this->container = $container;
52
        parent::__construct($container, $data);
53
    }
54
55
    final public function __toString (): string {
56
        return "metafields/{$this->getId()}";
57
    }
58
59
    protected function _container () {
60
        return $this->container;
61
    }
62
63
    protected function _dir (): string {
64
        if ($this->container instanceof Shop) {
65
            return 'metafields';
66
        }
67
        return "{$this->container}/metafields";
68
    }
69
70
    /**
71
     * @return AbstractEntity
72
     */
73
    public function getResource () {
74
        return $this->container;
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    public function getValue () {
81
        if ($this->isJson()) {
82
            return json_decode($this->data['value'] ?? 'null', true, JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR);
83
        }
84
        return $this->data['value'] ?? null;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    final public function isInt (): bool {
91
        return $this->getValueType() === self::TYPE_INT;
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    final public function isJson (): bool {
98
        return $this->getValueType() === self::TYPE_JSON;
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    final public function isString (): bool {
105
        return $this->getValueType() === self::TYPE_STRING;
106
    }
107
108
    /**
109
     * @param mixed $value
110
     * @return $this
111
     */
112
    public function setValue ($value) {
113
        if (is_int($value)) {
114
            $this->_set('value_type', self::TYPE_INT);
115
            return $this->_set('value', $value);
116
        }
117
        if (is_string($value)) {
118
            $this->_set('value_type', self::TYPE_STRING);
119
            return $this->_set('value', $value);
120
        }
121
        $this->_set('value_type', self::TYPE_JSON);
122
        return $this->_set('value', json_encode($value, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
123
    }
124
}