1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Node.php
См. документацию.
1<?php
2declare(strict_types=1);
3
4namespace Bitrix\Landing\Copilot\Data\Node;
5
6use Bitrix\Landing;
7use Bitrix\Landing\Copilot\Data;
8use Bitrix\Landing\Copilot\Data\Type\NodeType;
9
10abstract class Node
11{
12 protected const TYPE = null;
13
14 private string $code;
15
16 protected Data\Block $block;
18 protected ?array $placeholders;
19 protected bool $isAvatarNode;
20 protected ?array $promptTexts;
21
22 protected function __construct(string $code, array $data)
23 {
24 $this->code = $code;
25
26 $this->placeholders = $data['placeholders'] ?? [];
27 $this->isAvatarNode = (bool)($data['isAvatarNode'] ?? false);
28 $this->promptTexts = ($data['promptTexts'] ?? null);
29 }
30
36 public function setParentBlock(Data\Block $block): self
37 {
38 $this->block = $block;
39
40 return $this;
41 }
42
47 public function getParentBlock(): ?Data\Block
48 {
49 return $this->block ?? null;
50 }
51
56 public function getType(): ?NodeType
57 {
58 return static::TYPE;
59 }
60
68 public static function create(NodeType $type, string $code, array $data): ?Node
69 {
70 return match ($type)
71 {
72 NodeType::Img => new Img($code, $data),
73 NodeType::Icon => new Icon($code, $data),
74 NodeType::Text => new Text($code, $data),
75 NodeType::Link => new Link($code, $data),
76 default => null,
77 };
78 }
79
80 abstract public function setData(array $data): void;
81
82 // todo: move to toArray
83 abstract public function getValues(?int $position = null): array;
84
85 public function getCode(): string
86 {
87 return $this->code;
88 }
89
90 public function getPlaceholders(): ?array
91 {
92 return $this->placeholders;
93 }
94
95 public function isAvatarNode(): bool
96 {
97 return $this->isAvatarNode;
98 }
99
105 public function addPromptTexts(array $promptTexts): static
106 {
107 if ($this->promptTexts === null)
108 {
109 $this->promptTexts = [];
110 }
111 $this->promptTexts = array_merge($this->promptTexts, $promptTexts);
112
113 return $this;
114 }
115
116 public function getPromptTexts(): ?array
117 {
118 return $this->promptTexts;
119 }
120
121 public function setPromptTexts(array $promptTexts): void
122 {
123 $this->promptTexts = $promptTexts;
124 }
125
126 public function toArray(): array
127 {
128 $data = [];
129 $data['code'] = $this->code;
130 $data['type'] = $this->getType()->value;
131 $data['placeholders'] = $this->getPlaceholders();
132 if ($this->getPromptTexts())
133 {
134 $data['promptTexts'] = $this->getPromptTexts();
135 }
136 $data['values'] = $this->getValues();
137 $data['isAvatarNode'] = $this->isAvatarNode();
138
139 return $data;
140 }
141
147 public static function fromArray(array $data): ?self
148 {
149 if (!isset($data['type'], $data['code']))
150 {
151 return null;
152 }
153
154 $type = NodeType::from($data['type']);
155
156 if (in_array($type, NodeType::cases(), true))
157 {
158 return self::create($type, $data['code'], $data);
159 }
160
161 return null;
162 }
163
169 public function toLanding(?int $position = null): bool
170 {
171 return $this->canApplyToLanding();
172 }
173
178 protected function canApplyToLanding(): bool
179 {
180 $isSiteExists = $this->block->getParentSite()?->getSiteId();
181 $isLandingExists = $this->block->getParentSite()?->getLandingId();
182 $isBlockExists = (bool)$this->getBlockInstance();
183
184 if (
185 !$isSiteExists
186 || !$isLandingExists
187 || !$isBlockExists
188 )
189 {
190 return false;
191 }
192
193 return true;
194 }
195
196 protected function getBlockInstance(): ?Landing\Block
197 {
198 if (!isset($this->blockInstance))
199 {
200 $blockId = $this->block->getId();
201 if (!$blockId)
202 {
203 return null;
204 }
205
206 $instance = new Landing\Block($blockId);
207 if (!$instance->exist())
208 {
209 return null;
210 }
211
212 $this->blockInstance = $instance;
213 }
214
215 return $this->blockInstance;
216 }
217}
$type
Определения options.php:106
static fromArray(array $data)
Определения Node.php:147
Landing Block $blockInstance
Определения Node.php:17
__construct(string $code, array $data)
Определения Node.php:22
static create(NodeType $type, string $code, array $data)
Определения Node.php:68
addPromptTexts(array $promptTexts)
Определения Node.php:105
toLanding(?int $position=null)
Определения Node.php:169
array $promptTexts
Определения Node.php:20
Data Block $block
Определения Node.php:16
setParentBlock(Data\Block $block)
Определения Node.php:36
array $placeholders
Определения Node.php:18
setPromptTexts(array $promptTexts)
Определения Node.php:121
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения agent.php:3
Определения aliases.php:105
Определения base32.php:2
$instance
Определения ps_b24_final.php:14