Completed
Push — master ( 990805...73b24b )
by Nekrasov
02:53
created

SectionModel::refresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels\Models;
4
5
use Arrilot\BitrixModels\Queries\SectionQuery;
6
use Exception;
7
8
class SectionModel extends BaseModel
9
{
10
    /**
11
     * Corresponding IBLOCK_ID
12
     *
13
     * @var int
14
     */
15
    const IBLOCK_ID = null;
16
17
    /**
18
     * Corresponding object class name.
19
     *
20
     * @var string
21
     */
22
    protected static $objectClass = 'CIBlockSection';
23
24
    /**
25
     * Getter for corresponding iblock id.
26
     *
27
     * @throws Exception
28
     *
29
     * @return int
30
     */
31
    public static function iblockId()
32
    {
33
        $id = static::IBLOCK_ID;
34
        if (!$id) {
35
            throw new Exception('You must set $iblockId property or override iblockId() method');
36
        }
37
        
38
        return $id;
39
    }
40
41
    /**
42
     * Instantiate a query object for the model.
43
     *
44
     * @return SectionQuery
45
     */
46
    public static function query()
47
    {
48
        return new SectionQuery(static::instantiateObject(), get_called_class());
49
    }
50
51
    /**
52
     * Get IDs of direct children of the section.
53
     * Additional filter can be specified.
54
     *
55
     * @param array $filter
56
     *
57
     * @return array
58
     */
59
    public function getDirectChildren(array $filter = [])
60
    {
61
        return static::query()
62
            ->filter($filter)
63
            ->filter(['SECTION_ID' => $this->id])
64
            ->select('ID')
65
            ->getList()
66
            ->transform(function ($section) {
67
                return (int) $section['ID'];
68
            })
69
            ->all();
70
    }
71
72
    /**
73
     * Get IDs of all children of the section (direct or not).
74
     * Additional filter can be specified.
75
     *
76
     * @param array $filter
77
     * @param array|string $sort
78
     *
79
     * @return array
80
     */
81
    public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC'])
82
    {
83
        if (!isset($this->fields['LEFT_MARGIN']) || !isset($this->fields['RIGHT_MARGIN'])) {
84
            $this->refresh();
85
        }
86
87
        return static::query()
88
            ->sort($sort)
89
            ->filter($filter)
90
            ->filter([
91
                '!ID' => $this->id,
92
                '>LEFT_MARGIN' => $this->fields['LEFT_MARGIN'],
93
                '<RIGHT_MARGIN' => $this->fields['RIGHT_MARGIN'],
94
            ])
95
            ->select('ID')
96
            ->getList()
97
            ->transform(function ($section) {
98
                return (int) $section['ID'];
99
            })
100
            ->all();
101
    }
102
}
103