Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Entity.php
1<?php
2
4
9
10class Entity implements RestEntity
11{
12 public ?int $id = null;
13 public ?string $placement = null;
14 public ?string $title = null;
15 public ?int $restApplicationId = null;
16 public ?array $options = null;
17 public ?int $order = null;
18
19
20 public function __construct(?array $fields = null)
21 {
22 if ($fields !== null)
23 {
24 $this->hydrate($fields);
25 }
26 }
27
28 public function hydrate(array $fields): void
29 {
30 $this->id = $fields['id'] ?? null;
31 $this->placement = $fields['placement'] ?? null;
32 $this->title = $fields['title'] ?? null;
33 $this->restApplicationId = $fields['restApplicationId'] ?? null;
34 $this->order = $fields['order'] ?? null;
35 $this->hydrateOptions(($fields['options'] ?? null));
36 }
37
38 private function hydrateOptions(?array $options): void
39 {
40 if (!$options)
41 {
42 return;
43 }
44
45 if (isset($options['role']))
46 {
47 $this->options['role'] = mb_strtolower($options['role']);
48 }
49 if (isset($options['extranet']))
50 {
51 $this->options['extranet'] = $options['extranet'] === 'Y'? 'Y': 'N';
52 }
53 if (isset($options['context']))
54 {
55 $this->options['context'] = $this->getContext($options['context']);
56 }
57 if (isset($options['width']))
58 {
59 $this->options['width'] = $options['width'];
60 }
61 if (isset($options['height']))
62 {
63 $this->options['height'] = $options['height'];
64 }
65 if (isset($options['iconName']))
66 {
67 $this->options['iconName'] = $options['iconName'];
68 }
69 if ($this->placement === Placement::IM_TEXTAREA || $this->placement === Placement::IM_SIDEBAR)
70 {
71 $this->options['color'] = Color::getColor($options['color']) ?? Color::getColorByNumber($this->id);
72 }
73 }
74
75 private function getContext(string $contextOption): array
76 {
77 $userContextList = explode(';', trim($contextOption));
78 if (in_array(Context::ALL, $userContextList, true))
79 {
80 return [mb_strtolower(Context::ALL)];
81 }
82
83 return array_map('mb_strtolower', $userContextList);
84 }
85
89 public function getId(): ?int
90 {
91 return $this->id;
92 }
93
97 public static function getRestEntityName(): string
98 {
99 return 'placementApplication';
100 }
101
102 protected function toRestFormatOptions(): array
103 {
105 if (isset($options['extranet']))
106 {
107 unset($options['extranet']);
108 }
109 if (isset($options['role']))
110 {
111 unset($options['role']);
112 }
113
114 return $options;
115 }
116
120 public function toRestFormat(array $option = []): array
121 {
122 return [
123 'id' => (int)$this->id,
124 'title' => $this->title,
125 'options' => $this->toRestFormatOptions(),
126 'placement' => $this->placement,
127 'order' => $this->order,
128 'loadConfiguration' => [
130 'PLACEMENT' => $this->placement,
131 'PLACEMENT_ID' => $this->id,
132 ],
133 ];
134 }
135}
static getColorByNumber($number)
Definition color.php:144
static getColor($code)
Definition color.php:121