Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
resultserializable.php
1<?
2namespace Bitrix\Sale;
3
7use Serializable;
8
16 extends Result
17 implements Serializable
18{
19 public function serialize(): ?string
20 {
21 return serialize($this);
22 }
23
24 public function unserialize($data): void
25 {
26 $vars = unserialize($data, ['allowed_classes' => [static::class]]);
27 $this->__unserialize($vars);
28 }
29
30 public function __serialize(): array
31 {
32 $result = get_object_vars($this);
33
34 foreach($result as $name => $value)
35 if(empty($value))
36 unset($result[$name]);
37
38 $result['errors'] = array();
39
40 if($this->errors)
41 {
43 foreach($this->errors->toArray() as $error)
44 {
45 $result['errors'][] = array(
46 'code' => $error->getCode(),
47 'message' => $error->getMessage()
48 );
49 }
50 }
51
52 $result['CHARSET'] = ToUpper(SITE_CHARSET);
53
54 return $result;
55 }
56
57 public function __unserialize(array $vars): void
58 {
59 $isNeedRecode = !empty($vars['CHARSET']) && $vars['CHARSET'] != ToUpper(SITE_CHARSET);
60 $this->errors = new ErrorCollection();
61
62 foreach($vars as $name => $value)
63 {
64 if(!property_exists($this, $name))
65 continue;
66
67 if($name == 'errors')
68 {
69 foreach($value as $error)
70 {
71 if($isNeedRecode)
72 $error['message'] = Encoding::convertEncoding($error['message'], $vars['CHARSET'], SITE_CHARSET);
73
74 $this->addError(new Error($error['message'], $error['code']));
75 }
76 }
77 else
78 {
79 if($isNeedRecode)
80 $value = Encoding::convertEncoding($value, $vars['CHARSET'], SITE_CHARSET);
81
82 $this->$name = $value;
83 }
84 }
85 }
86}