Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ActionFactory.php
1<?php
2
4
27
32{
33 protected const UNDO = 'UNDO';
34 protected const REDO = 'REDO';
35 protected const ACTIONS = [
36 'ADD_BLOCK' => [
37 self::UNDO => RemoveBlockAction::class,
38 self::REDO => AddBlockAction::class,
39 ],
40 'REMOVE_BLOCK' => [
41 self::UNDO => AddBlockAction::class,
42 self::REDO => RemoveBlockAction::class,
43 ],
44 'SORT_BLOCK' => [
45 self::UNDO => SortBlockAction::class,
46 self::REDO => SortBlockAction::class,
47 ],
48 'ADD_CARD' => [
49 self::UNDO => RemoveCardAction::class,
50 self::REDO => AddCardAction::class,
51 ],
52 'REMOVE_CARD' => [
53 self::UNDO => AddCardAction::class,
54 self::REDO => RemoveCardAction::class,
55 ],
56 'EDIT_TEXT' => [
57 self::UNDO => EditTextAction::class,
58 self::REDO => EditTextAction::class,
59 ],
60 'EDIT_MAP' => [
61 self::UNDO => EditMapAction::class,
62 self::REDO => EditMapAction::class,
63 ],
64 'EDIT_EMBED' => [
65 self::UNDO => EditEmbedAction::class,
66 self::REDO => EditEmbedAction::class,
67 ],
68 'EDIT_IMG' => [
69 self::UNDO => EditImgAction::class,
70 self::REDO => EditImgAction::class,
71 ],
72 'EDIT_ICON' => [
73 self::UNDO => EditIconAction::class,
74 self::REDO => EditIconAction::class,
75 ],
76 'EDIT_LINK' => [
77 self::UNDO => EditLinkAction::class,
78 self::REDO => EditLinkAction::class,
79 ],
80 'EDIT_STYLE' => [
81 self::UNDO => EditStyleAction::class,
82 self::REDO => EditStyleAction::class,
83 ],
84 'EDIT_ATTRIBUTES' => [
85 self::UNDO => EditAttributesAction::class,
86 self::REDO => EditAttributesAction::class,
87 ],
88 'UPDATE_CONTENT' => [
89 self::UNDO => UpdateContentAction::class,
90 self::REDO => UpdateContentAction::class,
91 ],
92 'CHANGE_ANCHOR' => [
93 self::UNDO => ChangeAnchorAction::class,
94 self::REDO => ChangeAnchorAction::class,
95 ],
96 'CHANGE_NODE_NAME_ACTION' => [
97 self::UNDO => ChangeNodeNameAction::class,
98 self::REDO => ChangeNodeNameAction::class,
99 ],
100 'UPDATE_DYNAMIC' => [
101 self::UNDO => UpdateDynamicAction::class,
102 self::REDO => UpdateDynamicAction::class,
103 ],
104 'MULTIPLY' => [
105 self::UNDO => MultiplyAction::class,
106 self::REDO => MultiplyAction::class,
107 ],
108 'ADD_NODE' => [
109 self::UNDO => RemoveNodeAction::class,
110 self::REDO => AddNodeAction::class,
111 ],
112 'REMOVE_NODE' => [
113 self::UNDO => AddNodeAction::class,
114 self::REDO => RemoveNodeAction::class,
115 ],
116 'REPLACE_LANDING' => [
117 self::UNDO => ReplaceLanding::class,
118 self::REDO => ReplaceLanding::class,
119 ],
120 ];
121 public const MULTIPLY_ACTION_NAME = 'MULTIPLY';
122 protected const COMPARE_METHOD = 'compareParams';
123
129 public static function getAction(string $actionName, ?bool $undo = false): ?BaseAction
130 {
131 if (self::isActionExists($actionName))
132 {
133 $class = self::getActionClass($actionName, $undo);
134 if ($class)
135 {
136 return new $class();
137 }
138 }
139
140 // todo: null or err?
141 return null;
142 }
143
149 public static function isActionExists(string $actionName): bool
150 {
151 return array_key_exists($actionName, self::ACTIONS);
152 }
153
160 public static function getActionClass(string $actionName, ?bool $undo = false): ?string
161 {
162 $direction = self::getDirectionName($undo);
163 $class = self::ACTIONS[$actionName][$direction];
164
165 if (class_exists($class))
166 {
167 return $class;
168 }
169
170 return null;
171 }
172
178 public static function getDirectionName(?bool $undo = false): string
179 {
180 return $undo ? self::UNDO : self::REDO;
181 }
182
189 public static function compareSteps(array $prevStep, array $nextStep): bool
190 {
191 if (
192 $prevStep['ENTITY_TYPE'] === $nextStep['ENTITY_TYPE']
193 && $prevStep['ENTITY_ID'] === $nextStep['ENTITY_ID']
194 && $prevStep['ACTION'] === $nextStep['ACTION']
195 )
196 {
197 $class = self::getActionClass($prevStep['ACTION']);
198 if ($class && is_callable([$class, self::COMPARE_METHOD]))
199 {
200 return call_user_func(
201 [$class, self::COMPARE_METHOD],
202 $prevStep['ACTION_PARAMS'],
203 $nextStep['ACTION_PARAMS']
204 );
205 }
206 }
207
208 return false;
209 }
210}
static getAction(string $actionName, ?bool $undo=false)
static isActionExists(string $actionName)
static getActionClass(string $actionName, ?bool $undo=false)
static compareSteps(array $prevStep, array $nextStep)
static getDirectionName(?bool $undo=false)