Passed
Push — master ( fbd180...23ab98 )
by y
02:18
created

Section::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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    ()
19
 * @method string   getName         ()
20
 * @method $this    setName         (string $name)
21
 * @method Project  getProject      ()
22
 */
23
class Section extends AbstractEntity implements IteratorAggregate {
24
25
    use CrudTrait;
26
27
    const TYPE = 'section';
28
29
    protected static $map = [
30
        'project' => Project::class
31
    ];
32
33
    final public function __toString (): string {
34
        return "sections/{$this->getGid()}";
35
    }
36
37
    final protected function _getDir (): string {
38
        return "{$this->getProject()}/sections";
39
    }
40
41
    /**
42
     * @param array $filter
43
     * @return Traversable|Task[]
44
     */
45
    public function getIterator (array $filter = []) {
46
        $filter['section'] = $this->getGid();
47
        return $this->_loadEach(Task::class, 'tasks', $filter);
48
    }
49
50
    /**
51
     * @param array $filter
52
     * @return Task[]
53
     */
54
    public function getTasks (array $filter = []) {
55
        return iterator_to_array($this->getIterator($filter));
56
    }
57
58
    /**
59
     * Instantiates and returns a new task for the section.
60
     *
61
     * @depends after-create
62
     * @return Task
63
     */
64
    public function newTask () {
65
        /** @var Task $task */
66
        $task = $this->_factory(Task::class);
67
        return $task->addToProject($this->getProject(), $this);
68
    }
69
}