Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
resourcecollection.php
1<?php
2
4
5
13{
14 protected const KEY_PATH = 'path';
15 protected const KEY_TYPE = 'type';
16 protected const KEY_LOCATION = 'location';
17 protected const KEY_ORDER = 'order';
18
23 protected $resources;
28 protected $order;
33 protected $strings = [];
34
38 public function __construct()
39 {
40 $this->resources = [];
41 $this->order = 0;
42 }
43
49 public function add(string $path, string $type, int $location): void
50 {
51 // overwrite only if new location more
52 if (
53 $this->isResourceAdded($path)
54 && !$this->isNeedRaiseLocation($path, $location)
55 )
56 {
57 return;
58 }
59
60 $this->resources[$path] = [
61 self::KEY_PATH => $path,
62 self::KEY_TYPE => $type,
63 self::KEY_LOCATION => $location,
64 self::KEY_ORDER => $this->order++,
65 ];
66 }
67
68 protected function isResourceAdded(string $path): bool
69 {
70 return array_key_exists($path, $this->resources);
71 }
72
73 protected function isNeedRaiseLocation(string $path, int $location): bool
74 {
75 return $location < $this->resources[$path][self::KEY_LOCATION];
76 }
77
82 public function addString(string $string): void
83 {
84 if ($string && !in_array($string, $this->strings, true))
85 {
86 $this->strings[] = $string;
87 }
88 }
89
94 public function getStrings(): array
95 {
96 return $this->strings;
97 }
98
102 public function remove($pathes): void
103 {
104 if (!is_array($pathes))
105 {
106 $pathes = [$pathes];
107 }
108
109 foreach ($pathes as $path)
110 {
111 $this->removeOnce($path);
112 }
113 }
114
115 protected function removeOnce(string $path): void
116 {
117 if ($this->isResourceAdded($path))
118 {
119 unset($this->resources[$path]);
120 }
121 }
122
128 public function getSliceByLocation(int $location): ResourceCollection
129 {
130 return $this->getSliceByFilter(self::KEY_LOCATION, $location);
131 }
132
140 protected function getSliceByFilter($field, $value): ResourceCollection
141 {
142 $resourcesByFilter = new self();
143
144 foreach ($this->resources as $resource)
145 {
146 if (array_key_exists($field, $resource) && $resource[$field] === $value)
147 {
148 $resourcesByFilter->add(
149 $resource[self::KEY_PATH],
150 $resource[self::KEY_TYPE],
151 $resource[self::KEY_LOCATION]
152 );
153 }
154 }
155
156 return $resourcesByFilter;
157 }
158
163 public function getPathes(): array
164 {
165 return array_keys($this->resources);
166 }
167
172 public function getNormalized(): array
173 {
174 $this->sortByLocation();
175 $normalizedResources = [];
176
177 foreach ($this->resources as $resource)
178 {
179 if (!array_key_exists($resource[self::KEY_TYPE], $normalizedResources))
180 {
181 $normalizedResources[$resource[self::KEY_TYPE]] = [];
182 }
183 $normalizedResources[$resource[self::KEY_TYPE]][] = $resource[self::KEY_PATH];
184 }
185
186 return $normalizedResources;
187 }
188
189 protected function sortByLocation(): void
190 {
191 $columnLocation = array_column($this->resources, self::KEY_LOCATION);
192 $columnOrder = array_column($this->resources, self::KEY_ORDER);
193 array_multisort($columnLocation, $columnOrder, $this->resources);
194 }
195
200 public function isEmpty(): bool
201 {
202 return (count($this->resources) === 0) && (count($this->strings) === 0);
203 }
204}
add(string $path, string $type, int $location)
isNeedRaiseLocation(string $path, int $location)