Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chartstoreinfo.php
1<?php
2
4
7
8use function Symfony\Component\String\s;
9
10final class ChartStoreInfo
11{
12 private array $seriesList = [];
13 private array $columns;
14
15 private StoreInfoCombiner $combiner;
16
17 public function __construct(StoreInfoCombiner $combiner)
18 {
19 $this->combiner = $combiner;
20 }
21
28 public function accumulate(string $seriesId, StoreInfo ...$stores): void
29 {
30 if (!isset($this->seriesList[$seriesId]))
31 {
32 $this->seriesList[$seriesId] = $stores;
33 }
34 else
35 {
36 $this->seriesList[$seriesId] = $this->summarizeStores(...$this->seriesList[$seriesId], ...$stores);
37 }
38 }
39
40 private function summarizeStores(StoreInfo ...$stores): array
41 {
42 $outputStores = [];
43
45 foreach ($stores as $store)
46 {
47 if (isset($outputStores[$store->getStoreId()]))
48 {
49 $outputStores[$store->getStoreId()] = $this->combiner->summarizeStores($outputStores[$store->getStoreId()], $store);
50 }
51 else
52 {
53 $outputStores[$store->getStoreId()] = $store;
54 }
55 }
56
57 return array_values($outputStores);
58 }
59
65 public function getCombinedCalculatedColumn(int $maxLabelLength): array
66 {
67 $combinedTitle = '';
68
69 $combinedColumn = array_reduce($this->getCalculatedColumns(), static function ($carry, $item) use (&$combinedTitle, $maxLabelLength) {
70 foreach ($item as $series => $value)
71 {
72 if ($series !== 'TITLE')
73 {
74 $carry[$series] += $value;
75 }
76 }
77
78 if ($combinedTitle === '' || (mb_strlen($combinedTitle) < $maxLabelLength + 2))
79 {
80 $combinedTitle .= ', ';
81 $combinedTitle .= $item['TITLE'];
82 }
83
84 return $carry;
85 }, array_fill_keys(array_keys($this->seriesList), 0.0));
86
87 $combinedTitle = mb_substr($combinedTitle, 2);
88 $combinedColumn['TITLE'] = $combinedTitle;
89
90 return $combinedColumn;
91 }
92
97 public function getCalculatedColumns(): array
98 {
99 if (isset($this->columns))
100 {
101 return $this->columns;
102 }
103
104 $columns = [];
105
106 foreach ($this->seriesList as $seriesId => $series)
107 {
109 foreach ($series as $store)
110 {
111 $storeId = $store->getStoreId();
112 if (!isset($columns[$storeId]))
113 {
114 $columns[$storeId] = array_fill_keys(array_keys($this->seriesList), 0.0);
115 $columns[$storeId]['TITLE'] = $store->getStoreName();
116 }
117
118 $columns[$storeId][$seriesId] += $store->getCalculatedSumPrice();
119 }
120 }
121
122 $this->columns = $columns;
123 return $this->columns;
124 }
125
130 public function getStoresCount(): int
131 {
132 return count($this->getCalculatedColumns());
133 }
134}