SectionQuery::normalizeFilter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels\Queries;
4
5
use Arrilot\BitrixModels\Helpers;
6
use Illuminate\Support\Collection;
7
use Arrilot\BitrixModels\Models\SectionModel;
8
9
/**
10
 * @method SectionQuery active()
11
 */
12
class SectionQuery extends OldCoreQuery
13
{
14
    /**
15
     * Query sort.
16
     *
17
     * @var array
18
     */
19
    public $sort = ['SORT' => 'ASC'];
20
21
    /**
22
     * Query bIncCnt.
23
     * This is sent to getList directly.
24
     *
25
     * @var array|false
26
     */
27
    public $countElements = false;
28
29
    /**
30
     * Iblock id.
31
     *
32
     * @var int
33
     */
34
    protected $iblockId;
35
36
    /**
37
     * List of standard entity fields.
38
     *
39
     * @var array
40
     */
41
    protected $standardFields = [
42
        'ID',
43
        'CODE',
44
        'EXTERNAL_ID',
45
        'IBLOCK_ID',
46
        'IBLOCK_SECTION_ID',
47
        'TIMESTAMP_X',
48
        'SORT',
49
        'NAME',
50
        'ACTIVE',
51
        'GLOBAL_ACTIVE',
52
        'PICTURE',
53
        'DESCRIPTION',
54
        'DESCRIPTION_TYPE',
55
        'LEFT_MARGIN',
56
        'RIGHT_MARGIN',
57
        'DEPTH_LEVEL',
58
        'SEARCHABLE_CONTENT',
59
        'SECTION_PAGE_URL',
60
        'MODIFIED_BY',
61
        'DATE_CREATE',
62
        'CREATED_BY',
63
        'DETAIL_PICTURE',
64
    ];
65
66
    /**
67
     * Constructor.
68
     *
69
     * @param object $bxObject
70
     * @param string $modelName
71
     */
72
    public function __construct($bxObject, $modelName)
73
    {
74
        parent::__construct($bxObject, $modelName);
75
76
        $this->iblockId = $modelName::iblockId();
77
    }
78
79
    /**
80
     * CIBlockSection::getList substitution.
81
     *
82
     * @return Collection
83
     */
84
    protected function loadModels()
85
    {
86
        $queryType = 'SectionQuery::getList';
87
        $sort = $this->sort;
88
        $filter = $this->normalizeFilter();
89
        $countElements = $this->countElements;
90
        $select = $this->normalizeSelect();
91
        $navigation = $this->navigation;
92
        $keyBy = $this->keyBy;
93
94
        $callback = function() use ($sort, $filter, $countElements, $select, $navigation) {
95
            $sections = [];
96
            $rsSections = $this->bxObject->getList($sort, $filter, $countElements, $select, $navigation);
97
            while ($arSection = $this->performFetchUsingSelectedMethod($rsSections)) {
98
99
                // Если передать nPageSize, то Битрикс почему-то перестает десериализовать множественные свойсвта...
100
                // Проверим это еще раз, и если есть проблемы то пофиксим.
101
                foreach ($arSection as $field => $value) {
102
                    if (
103
                    is_string($value)
104
                    && Helpers::startsWith($value, 'a:')
105
                    && (Helpers::startsWith($field, 'UF_') || Helpers::startsWith($field, '~UF_'))
106
                    ) {
107
                        $unserializedValue = @unserialize($value);
108
                        $arSection[$field] = $unserializedValue === false ? $value : $unserializedValue;
109
                    }
110
                }
111
112
                $this->addItemToResultsUsingKeyBy($sections, new $this->modelName($arSection['ID'], $arSection));
113
            }
114
115
            return new Collection($sections);
116
        };
117
118
        $cacheParams = compact('queryType', 'sort', 'filter', 'countElements', 'select', 'navigation', 'keyBy');
119
120
        return $this->handleCacheIfNeeded($cacheParams, $callback);
121
    }
122
123
    /**
124
     * Get the first section with a given code.
125
     *
126
     * @param string $code
127
     *
128
     * @return SectionModel
129
     */
130
    public function getByCode($code)
131
    {
132
        $this->filter['=CODE'] = $code;
133
134
        return $this->first();
135
    }
136
137
    /**
138
     * Get the first section with a given external id.
139
     *
140
     * @param string $id
141
     *
142
     * @return SectionModel
143
     */
144
    public function getByExternalId($id)
145
    {
146
        $this->filter['EXTERNAL_ID'] = $id;
147
148
        return $this->first();
149
    }
150
151
    /**
152
     * Get count of sections that match filter.
153
     *
154
     * @return int
155
     */
156
    public function count()
157
    {
158
        if ($this->queryShouldBeStopped) {
159
            return 0;
160
        }
161
162
        $queryType = 'SectionQuery::count';
163
        $filter = $this->normalizeFilter();
164
        $callback = function() use ($filter) {
165
            return (int) $this->bxObject->getCount($filter);
166
        };
167
168
        return $this->handleCacheIfNeeded(compact('queryType', 'filter'), $callback);
169
    }
170
171
    /**
172
     * Setter for countElements.
173
     *
174
     * @param $value
175
     *
176
     * @return $this
177
     */
178
    public function countElements($value)
179
    {
180
        $this->countElements = $value;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Normalize filter before sending it to getList.
187
     * This prevents some inconsistency.
188
     *
189
     * @return array
190
     */
191
    protected function normalizeFilter()
192
    {
193
        $this->filter['IBLOCK_ID'] = $this->iblockId;
194
195
        return $this->filter;
196
    }
197
198
    /**
199
     * Normalize select before sending it to getList.
200
     * This prevents some inconsistency.
201
     *
202
     * @return array
203
     */
204
    protected function normalizeSelect()
205
    {
206
        if ($this->fieldsMustBeSelected()) {
207
            $this->select = array_merge($this->standardFields, $this->select);
208
        }
209
210
        if ($this->propsMustBeSelected()) {
211
            $this->select[] = 'IBLOCK_ID';
212
            $this->select[] = 'UF_*';
213
        }
214
215
        $this->select[] = 'ID';
216
217
        return $this->clearSelectArray();
218
    }
219
}
220