1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
JobCollection.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Collab\Onboarding\Entity;
6
7use ArrayIterator;
8use Bitrix\Im\V2\Promotion\Entity\PromotionList;
9use Bitrix\Main\Type\Collection;
10use Bitrix\Main\Type\Contract\Arrayable;
11use Bitrix\Main\Validation\Rule\ElementsType;
12use Bitrix\Main\Validation\Rule\Recursive\Validatable;
13use Bitrix\Socialnetwork\Collab\Onboarding\Integration\Im\Promotion\Converter\PromotionListConverter;
14use Bitrix\Socialnetwork\Collab\Onboarding\Internals\Validation\Validator\JobDataValidator;
15use IteratorAggregate;
16
17final class JobCollection implements IteratorAggregate, Arrayable
18{
19 #[ElementsType(className: AbstractJob::class)]
20 #[Validatable(true)]
21 private array $jobs;
22 private ?ArrayIterator $iterator = null;
23
24 public function __construct(?AbstractJob ...$jobs)
25 {
26 $this->jobs = array_filter(
27 $jobs,
28 static fn($job) => $job !== null
29 );
30 }
31
32 public function add(AbstractJob $job): self
33 {
34 $this->jobs[] = $job;
35
36 return $this;
37 }
38
39 public function merge(JobCollection $jobCollection): self
40 {
41 foreach ($jobCollection as $job)
42 {
43 $this->jobs[] = $job;
44 }
45
46 return $this;
47 }
48
49 public function cleanIntersectingJobs(self $jobCollection): self
50 {
51 $filteredIds = array_flip($jobCollection->getIdList());
52
53 $this->jobs = array_filter(
54 $this->jobs,
55 static fn(AbstractJob $job) => !isset($filteredIds[$job->getId()])
56 );
57
58 return $this;
59 }
60
61 public function getImmediatelyExecuted(): self
62 {
63 return $this->getByImmediately(true);
64 }
65
66 public function getNotImmediatelyExecuted(): self
67 {
68 return $this->getByImmediately(false);
69 }
70
71 private function getByImmediately(bool $isImmediately): self
72 {
73 $jobCollection = new self();
74
75 foreach ($this->jobs as $job)
76 {
77 if ($isImmediately === $job->isImmediatelyExecuted())
78 {
79 $jobCollection->add($job);
80 }
81 }
82
83 return $jobCollection;
84 }
85
89 public function getJobTypeList(): array
90 {
91 $types = [];
92 foreach ($this->jobs as $job)
93 {
94 $types[] = $job->getType()->value;
95 }
96
97 return array_unique($types);
98 }
99
103 public function getUserIdList(): array
104 {
105 $ids = [];
106 foreach ($this->jobs as $job)
107 {
108 $ids[] = $job->getUserId();
109 }
110
111 Collection::normalizeArrayValuesByInt($ids, false);
112
113 return $ids;
114 }
115
119 public function getCollabIdList(): array
120 {
121 $ids = [];
122 foreach ($this->jobs as $job)
123 {
124 $ids[] = $job->getCollabId();
125 }
126
127 Collection::normalizeArrayValuesByInt($ids, false);
128
129 return $ids;
130 }
131
135 public function getIdList(): array
136 {
137 $ids = [];
138 foreach ($this->jobs as $job)
139 {
140 $ids[] = $job->getId();
141 }
142
143 Collection::normalizeArrayValuesByInt($ids, false);
144
145 return $ids;
146 }
147
148 public function isEmpty(): bool
149 {
150 return empty($this->jobs);
151 }
152
153 public function getIterator(): ArrayIterator
154 {
155 if ($this->iterator === null)
156 {
157 $this->iterator = new ArrayIterator($this->jobs);
158 }
159
160 return $this->iterator;
161 }
162
164 public function sortByUserId(): array
165 {
166 $sorted = [];
167 foreach ($this->jobs as $job)
168 {
169 $userId = $job->getUserId();
170
171 if (!isset($sorted[$userId]))
172 {
173 $sorted[$userId] = new JobCollection();
174 }
175
176 $sorted[$userId]->add($job);
177 }
178
179 return $sorted;
180 }
181
182 public static function mapFromArray(array $jobCollectionData): self
183 {
184 $jobCollection = new self();
185
186 $jobDataValidator = new JobDataValidator();
187 foreach ($jobCollectionData as $jobData)
188 {
189 if (!$jobDataValidator->validate($jobData)->isSuccess())
190 {
191 continue;
192 }
193
194 $job = JobFactory::create($jobData['collabId'], $jobData['userId'], $jobData['type'])
195 ?->setId($jobData['id'])
196 ->setCreatedDate($jobData['createdDate'])
197 ->setNextExecution($jobData['nextExecution'])
198 ;
199
200 $jobCollection->add($job);
201 }
202
203 return $jobCollection;
204 }
205
206 public function toArray(): array
207 {
208 return array_map(
209 static fn(AbstractJob $job) => $job->toArray(),
210 $this->jobs
211 );
212 }
213
214 public function toPromotions(): ?PromotionList
215 {
216 return PromotionListConverter::getInstance()
217 ->convertJobCollection($this)
218 ;
219 }
220}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static mapFromArray(array $jobCollectionData)
Определения JobCollection.php:182
merge(JobCollection $jobCollection)
Определения JobCollection.php:39
static create(int $collabId, int $userId, string $type)
Определения JobFactory.php:19
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$iterator
Определения yandex_run.php:610