Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
state.php
1<?php
2
4
5
9
10final class State extends Dictionary
11{
12 private const STATE_NAME_VAR = '__state';
13 private const CHAIN_NAME_VAR = '__chain';
14 private const STATE_SIGNER_SALT = 'discount.preset.state';
15
17 private $signer = null;
18
23 public function __construct(array $values = null)
24 {
25 parent::__construct($values);
26
27 $this->setDefaultValues();
28 $this->initSigner();
29 }
30
31 private function setDefaultValues()
32 {
33 if(empty($this[self::CHAIN_NAME_VAR]))
34 {
35 $this[self::CHAIN_NAME_VAR] = array();
36 }
37 }
38
39 private function initSigner(): void
40 {
41 $this->signer = new Signer();
42 }
43
44 private function sign(string $data): string
45 {
46 return $this->signer->sign($data, self::STATE_SIGNER_SALT);
47 }
48
49 public function unSign(string $data): string
50 {
51 try
52 {
53 $signedData = $this->signer->unsign($data, self::STATE_SIGNER_SALT);
54 }
55 catch(\Bitrix\Main\Security\Sign\BadSignatureException $e)
56 {
57 die('Bad signature.');
58 }
59 return $signedData;
60 }
61
62 public function set($name, $value = null)
63 {
64 parent::set($name, $value);
65
66 $this->setDefaultValues();
67
68 return $this;
69 }
70
71 public function append(array $values)
72 {
73 foreach($values as $name => $value)
74 {
75 $this[$name] = $value;
76 }
77
78 return $this;
79 }
80
87 public function get($name, $defaultValue = null)
88 {
89 $value = parent::get($name);
90 if ($defaultValue && is_callable($defaultValue))
91 {
92 return $defaultValue($value);
93 }
94
95 return $value !== null? $value : $defaultValue;
96 }
97
98 public static function createFromEncodedData($data)
99 {
100 $state = new State;
101
102 if(empty($data))
103 {
104 return $state;
105 }
106
107 $data = $state->unSign($data);
108
109 $data = base64_decode($data);
110 if($data === false)
111 {
112 return $state;
113 }
114 $data = unserialize($data, ['allowed_classes' => ['Bitrix\Main\Type\DateTime', 'DateTime']]);
115
116 return $state->set($data?: array());
117 }
118
119 public static function createFromRequest(HttpRequest $request)
120 {
121 $prevState = self::createFromEncodedData($request->getPost(self::STATE_NAME_VAR));
122
123 $postData = array();
124 foreach($request->getPostList()->toArray() as $name => $data)
125 {
126 if(is_array($data) && count($data) === 1 && ($data[0] !== '0' && empty($data[0])))
127 {
128 //empty array
129 unset($prevState[$name]);
130 continue;
131 }
132
133 if(is_array($data) && ($data[0] !== '0' && empty($data[0])))
134 {
135 unset($data[0]);
136 }
137 $postData[$name] = $data;
138 }
139
140 return new State(array_merge($prevState->toArray(), $postData));
141 }
142
143 public function addStepChain($step)
144 {
145 $chain = $this[self::CHAIN_NAME_VAR];
146 $lastStep = end($chain);
147
148 if($lastStep != $step)
149 {
150 $chain[] = $step;
151 $this[self::CHAIN_NAME_VAR] = $chain;
152 }
153
154 return $this;
155 }
156
157 public function popStepChain()
158 {
159 $chain = $this[self::CHAIN_NAME_VAR];
160 $step = array_pop($chain);
161
162 $this[self::CHAIN_NAME_VAR] = $chain;
163
164 return $step;
165 }
166
167 public function getPrevStep()
168 {
169 return end($this[self::CHAIN_NAME_VAR]);
170 }
171
172 private function getStepChain(): array
173 {
174 return $this->get(self::CHAIN_NAME_VAR, array());
175 }
176
177 public function __toString()
178 {
179 $data = $this->toArray();
180 $value = $this->sign(base64_encode(serialize($data)));
181
182 return '<input type="hidden" name="' . self::STATE_NAME_VAR . '" value="' . $value . '">';
183 }
184
185 public function toString()
186 {
187 return $this->__toString();
188 }
189
190 public function toArray()
191 {
192 $toArray = parent::toArray();
193
194 unset(
195 $toArray['sessid'],
196 $toArray['lang'],
197 $toArray['__next_step']
198 );
199
200 return $toArray;
201 }
202
203 public function getStepNumber()
204 {
205 $countPrevSteps = count($this->getStepChain());
206
207 return $countPrevSteps + 1;
208 }
209}
static createFromEncodedData($data)
Definition state.php:98
static createFromRequest(HttpRequest $request)
Definition state.php:119
__construct(array $values=null)
Definition state.php:23