CustomField::isNumber()   A
last analyzed

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\Asana;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\AbstractEntity\CrudTrait;
7
use Helix\Asana\Base\AbstractEntity\PostMutatorTrait;
8
use Helix\Asana\CustomField\EnumOption;
9
use Helix\Asana\Task\FieldEntries;
10
11
/**
12
 * A custom field.
13
 *
14
 * See {@link FieldEntries} for getting {@link Task} values.
15
 *
16
 * @see https://developers.asana.com/docs/asana-custom-fields
17
 * @see https://developers.asana.com/docs/custom-field
18
 *
19
 * @see Workspace::newCustomField()
20
 *
21
 * @method $this        setResourceSubtype      (string $type)          @depends create-only, see the subtype constants
22
 * @method $this        setWorkspace            (Workspace $workspace)  @depends create-only, no getter
23
 *
24
 * @method string       getCurrencyCode         () ISO 4217
25
 * @method string       getCustomLabel          ()
26
 * @method string       getCustomLabelPosition  () See the position constants.
27
 * @method string       getDescription          ()
28
 * @method EnumOption[] getEnumOptions          ()
29
 * @method string       getFormat               () See the format constants.
30
 * @method string       getName                 ()
31
 * @method int          getPrecision            ()
32
 * @method string       getResourceSubtype      () See the subtype constants.
33
 *
34
 * @method $this        setCurrencyCode         (string $iso4217) Requires `subtype=number`, `format=currency`
35
 * @method $this        setCustomLabel          (string $label) Requires `format=custom`
36
 * @method $this        setCustomLabelPosition  (string $position) See the position constants.
37
 * @method $this        setDescription          (string $text)
38
 * @method $this        setFormat               (string $format) See the format constants.
39
 * @method $this        setName                 (string $name)
40
 * @method $this        setPrecision            (int $precision)
41
 */
42
class CustomField extends AbstractEntity {
43
44
    use CrudTrait;
45
    use PostMutatorTrait;
46
47
    const DIR = 'custom_fields';
48
    const TYPE = 'custom_field';
49
    const TYPE_ENUM = 'enum';
50
    const TYPE_NUMBER = 'number';
51
    const TYPE_TEXT = 'text';
52
53
    const FORMAT_CURRENCY = 'currency';
54
    const FORMAT_CUSTOM = 'custom';
55
    const FORMAT_IDENTIFIER = 'identifier';
56
    const FORMAT_NONE = 'none';
57
    const FORMAT_PERCENTAGE = 'percentage';
58
59
    const POSITION_PREFIX = 'prefix';
60
    const POSITION_SUFFIX = 'suffix';
61
62
    protected const MAP = [
63
        'enum_options' => [EnumOption::class],
64
    ];
65
66
    protected function _setData (array $data): void {
67
        // strip down, removing task values if present
68
        $data = array_intersect_key($data, array_flip([
69
            'gid',
70
            'currency_code',
71
            'custom_label',
72
            'custom_label_position',
73
            'description',
74
            'enum_options',
75
            'format',
76
            'name',
77
            'precision',
78
            'resource_subtype'
79
        ]));
80
        parent::_setData($data);
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    final public function hasNotificationsEnabled (): bool {
87
        return $this->_is('has_notifications_enabled');
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    final public function isCurrency (): bool {
94
        return $this->getFormat() === self::FORMAT_CURRENCY;
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    final public function isEnum (): bool {
101
        return $this->getResourceSubtype() === self::TYPE_ENUM;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    final public function isGlobalToWorkspace (): bool {
108
        return $this->_is('is_global_to_workspace');
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    final public function isIdentifier (): bool {
115
        return $this->getFormat() === self::FORMAT_IDENTIFIER;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    final public function isNumber (): bool {
122
        return $this->getResourceSubtype() === self::TYPE_NUMBER;
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    final public function isPercentage (): bool {
129
        return $this->getFormat() === self::FORMAT_PERCENTAGE;
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    final public function isText (): bool {
136
        return $this->getResourceSubtype() === self::TYPE_TEXT;
137
    }
138
139
    /**
140
     * @return EnumOption
141
     */
142
    public function newEnumOption () {
143
        return $this->api->factory($this, EnumOption::class);
144
    }
145
146
    /**
147
     * @param bool $global
148
     * @return $this
149
     */
150
    final public function setGlobalToWorkspace (bool $global) {
151
        return $this->_set('is_global_to_workspace', $global);
152
    }
153
154
    /**
155
     * @param bool $enabled
156
     * @return $this
157
     */
158
    final public function setNotificationsEnabled (bool $enabled) {
159
        return $this->_set('has_notifications_enabled', $enabled);
160
    }
161
162
    /**
163
     * @param callable $cmp `fn( EnumOption $a, EnumOption $b ): int`
164
     * @return $this
165
     */
166
    public function sortEnumOptions (callable $cmp) {
167
        if ($options = $this->getEnumOptions()) {
168
            $prev = $options[0]; // first option on remote
169
            usort($options, $cmp);
170
            if ($this->hasGid()) {
171
                foreach ($options as $option) {
172
                    if ($option !== $prev) {
173
                        $this->api->put($option, [
174
                            'insert_after' => $prev->getGid()
175
                        ]);
176
                    }
177
                    $prev = $option;
178
                }
179
            }
180
            $this->data['enum_options'] = $options;
181
            // no diff
182
        }
183
        return $this;
184
    }
185
186
}