Passed
Push — master ( 1f9a54...993a87 )
by y
02:21
created

ExternalData::setDataJsonEncoded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Helix\Asana\Task;
4
5
use Helix\Asana\Base\Data;
6
use Helix\Asana\Task;
7
8
/**
9
 * Custom task data.
10
 *
11
 * @see https://developers.asana.com/docs/custom-external-data
12
 *
13
 * @method null|string  getGid  ()
14
 * @method null|string  getData ()
15
 *
16
 * @method bool         hasGid  ()
17
 * @method bool         hasData ()
18
 *
19
 * @method $this        setGid  (?string $gid) 1024 chars max.
20
 * @method $this        setData (?string $data) 32768 chars max.
21
 */
22
class ExternalData extends Data {
23
24
    /**
25
     * @var Task
26
     */
27
    protected $task;
28
29
    /**
30
     * @param Task $task
31
     * @param array $data
32
     */
33
    public function __construct (Task $task, array $data = []) {
34
        $this->task = $task;
35
        parent::__construct($task, $data);
36
    }
37
38
    /**
39
     * Marks the task's `external` diff.
40
     *
41
     * @param string $field
42
     * @param mixed $value
43
     * @return $this
44
     */
45
    protected function _set (string $field, $value) {
46
        $this->task->diff['external'] = true;
47
        return parent::_set($field, $value);
48
    }
49
50
    /**
51
     * The JSON decoded data, or `null`.
52
     *
53
     * @return mixed
54
     */
55
    public function getDataJsonDecoded () {
56
        if (strlen($data = $this->getData())) {
57
            return json_decode($data, true, 512, JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR);
58
        }
59
        return null;
60
    }
61
62
    /**
63
     * @return Task
64
     */
65
    public function getTask () {
66
        return $this->task;
67
    }
68
69
    /**
70
     * JSON encodes and sets.
71
     * This field is nullable, so `null` is not encoded.
72
     *
73
     * @param mixed $data
74
     * @return $this
75
     */
76
    public function setDataJsonEncoded ($data) {
77
        if (isset($data)) {
78
            return $this->setData(json_encode($data, JSON_THROW_ON_ERROR));
79
        }
80
        return $this->setData(null);
81
    }
82
}