Completed
Push — master ( af05a7...eda6ec )
by Nekrasov
02:11
created

SectionQuery::getList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
rs 9.4286
cc 3
eloc 13
nc 3
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels\Queries;
4
5
use Arrilot\BitrixModels\Collection;
6
use Arrilot\BitrixModels\Models\SectionModel;
7
8
class SectionQuery extends BaseQuery
9
{
10
    /**
11
     * Query sort.
12
     *
13
     * @var array
14
     */
15
    public $sort = ['SORT' => 'ASC'];
16
17
    /**
18
     * Query bIncCnt.
19
     * This is sent to getList directly.
20
     *
21
     * @var array|false
22
     */
23
    public $countElements = false;
24
25
    /**
26
     * Iblock id.
27
     *
28
     * @var int
29
     */
30
    protected $iblockId;
31
32
    /**
33
     * List of standard entity fields.
34
     *
35
     * @var array
36
     */
37
    protected $standardFields = [
38
        'ID',
39
        'CODE',
40
        'EXTERNAL_ID',
41
        'IBLOCK_ID',
42
        'IBLOCK_SECTION_ID',
43
        'TIMESTAMP_X',
44
        'SORT',
45
        'NAME',
46
        'ACTIVE',
47
        'GLOBAL_ACTIVE',
48
        'PICTURE',
49
        'DESCRIPTION',
50
        'DESCRIPTION_TYPE',
51
        'LEFT_MARGIN',
52
        'RIGHT_MARGIN',
53
        'DEPTH_LEVEL',
54
        'SEARCHABLE_CONTENT',
55
        'SECTION_PAGE_URL',
56
        'MODIFIED_BY',
57
        'DATE_CREATE',
58
        'CREATED_BY',
59
        'DETAIL_PICTURE',
60
    ];
61
62
    /**
63
     * Constructor.
64
     *
65
     * @param object $bxObject
66
     * @param string $modelName
67
     */
68
    public function __construct($bxObject, $modelName)
69
    {
70
        parent::__construct($bxObject, $modelName);
71
72
        $this->iblockId = $modelName::iblockId();
73
    }
74
75
    /**
76
     * CIBlockSection::getList substitution.
77
     *
78
     * @return Collection
79
     */
80
    public function getList()
81
    {
82
        if ($this->queryShouldBeStopped) {
83
            return new Collection();
84
        }
85
86
        $sections = [];
87
        $rsSections = $this->bxObject->getList(
88
            $this->sort,
89
            $this->normalizeFilter(),
90
            $this->countElements,
91
            $this->normalizeSelect(),
92
            $this->navigation
93
        );
94
        while ($arSection = $rsSections->fetch()) {
95
            $this->addItemToResultsUsingKeyBy($sections, new $this->modelName($arSection['ID'], $arSection));
96
        }
97
98
        return new Collection($sections);
99
    }
100
101
    /**
102
     * Get the first section with a given code.
103
     *
104
     * @param string $code
105
     *
106
     * @return SectionModel
107
     */
108
    public function getByCode($code)
109
    {
110
        $this->filter['CODE'] = $code;
111
112
        return $this->first();
113
    }
114
115
    /**
116
     * Get the first section with a given external id.
117
     *
118
     * @param string $id
119
     *
120
     * @return SectionModel
121
     */
122
    public function getByExternalId($id)
123
    {
124
        $this->filter['EXTERNAL_ID'] = $id;
125
126
        return $this->first();
127
    }
128
129
    /**
130
     * Get count of sections that match filter.
131
     *
132
     * @return int
133
     */
134
    public function count()
135
    {
136
        if ($this->queryShouldBeStopped) {
137
            return 0;
138
        }
139
140
        return (int) $this->bxObject->getCount($this->normalizeFilter());
141
    }
142
143
    /**
144
     * Setter for countElements.
145
     *
146
     * @param $value
147
     *
148
     * @return $this
149
     */
150
    public function countElements($value)
151
    {
152
        $this->countElements = $value;
153
154
        return $this;
155
    }
156
157
    /**
158
     * Normalize filter before sending it to getList.
159
     * This prevents some inconsistency.
160
     *
161
     * @return array
162
     */
163
    protected function normalizeFilter()
164
    {
165
        $this->filter['IBLOCK_ID'] = $this->iblockId;
166
167
        return $this->filter;
168
    }
169
170
    /**
171
     * Normalize select before sending it to getList.
172
     * This prevents some inconsistency.
173
     *
174
     * @return array
175
     */
176 View Code Duplication
    protected function normalizeSelect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        if ($this->fieldsMustBeSelected()) {
179
            $this->select = array_merge($this->standardFields, $this->select);
180
        }
181
182
        if ($this->propsMustBeSelected()) {
183
            $this->select[] = 'IBLOCK_ID';
184
            $this->select[] = 'UF_*';
185
        }
186
187
        return $this->clearSelectArray();
188
    }
189
}
190