Passed
Branch master (1a758e)
by y
01:58
created

Section::_setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helix\Asana\Project;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\AbstractEntity\CrudTrait;
7
use Helix\Asana\Project;
8
use Helix\Asana\Task;
9
use IteratorAggregate;
10
use Traversable;
11
12
/**
13
 * A project section.
14
 *
15
 * @see https://developers.asana.com/docs/asana-sections
16
 * @see https://developers.asana.com/docs/section
17
 *
18
 * @method string   getCreatedAt    () RFC3339x
19
 * @method string   getName         ()
20
 * @method Project  getProject      ()
21
 *
22
 * @method $this    setName         (string $name)
23
 */
24
class Section extends AbstractEntity implements IteratorAggregate {
25
26
    use CrudTrait;
27
28
    const DIR = 'sections';
29
    const TYPE = 'section';
30
31
    protected const MAP = [
32
        'project' => Project::class
33
    ];
34
35
    /**
36
     * @var Project
37
     */
38
    protected $parent;
39
40
    public function __construct ($caller, array $data = []) {
41
        parent::__construct($caller, $data);
42
        $this->parent = $this->getProject();
43
    }
44
45
    protected function _setData (array $data): void {
46
        // deprecated for the singular project field.
47
        unset($data['projects']);
48
49
        parent::_setData($data);
50
    }
51
52
    /**
53
     * @param array $filter
54
     * @return Traversable|Task[]
55
     */
56
    public function getIterator (array $filter = Task::GET_INCOMPLETE) {
57
        $filter['section'] = $this->getGid();
58
        return $this->api->loadEach($this, Task::class, 'tasks', $filter);
59
    }
60
61
    /**
62
     * @param array $filter
63
     * @return Task[]
64
     */
65
    public function getTasks (array $filter = Task::GET_INCOMPLETE) {
66
        return iterator_to_array($this->getIterator($filter));
67
    }
68
69
    /**
70
     * Factory.
71
     *
72
     * @return Task
73
     */
74
    public function newTask () {
75
        /** @var Task $task */
76
        $task = $this->api->factory($this, Task::class);
77
        return $task->addToProject($this);
78
    }
79
}