Bitrix-D7  20.5.0
arrayfield.php
См. документацию.
1 <?php
2 /**
3  * Bitrix Framework
4  * @package bitrix
5  * @subpackage main
6  * @copyright 2001-2018 Bitrix
7  */
8 
10 
12 
13 /**
14  * @package bitrix
15  * @subpackage main
16  */
17 class ArrayField extends ScalarField
18 {
19  /** @var string json, serialize, custom */
20  protected $serializationType;
21 
22  /** @var callable */
23  protected $encodeFunction;
24 
25  /** @var callable */
26  protected $decodeFunction;
27 
28  public function __construct($name, $parameters = [])
29  {
31 
32  $this->addSaveDataModifier([$this, 'encode']);
33  $this->addFetchDataModifier([$this, 'decode']);
34 
35  parent::__construct($name, $parameters);
36  }
37 
38  /**
39  * Sets json serialization format
40  *
41  * @return $this
42  */
43  public function configureSerializationJson()
44  {
45  $this->serializationType = 'json';
46  $this->encodeFunction = [$this, 'encodeJson'];
47  $this->decodeFunction = [$this, 'decodeJson'];
48 
49  return $this;
50  }
51 
52  /**
53  * Sets php serialization format
54  *
55  * @return $this
56  */
57  public function configureSerializationPhp()
58  {
59  $this->serializationType = 'php';
60  $this->encodeFunction = [$this, 'encodePhp'];
61  $this->decodeFunction = [$this, 'decodePhp'];
62 
63  return $this;
64  }
65 
66  /**
67  * Custom encode handler
68  *
69  * @param callable $callback
70  *
71  * @return $this
72  */
73  public function configureSerializeCallback($callback)
74  {
75  $this->encodeFunction = $callback;
76  $this->serializationType = 'custom';
77 
78  return $this;
79  }
80 
81  /**
82  * Custom decode handler
83  *
84  * @param $callback
85  *
86  * @return $this
87  */
88  public function configureUnserializeCallback($callback)
89  {
90  $this->decodeFunction = $callback;
91  $this->serializationType = 'custom';
92 
93  return $this;
94  }
95 
96  /**
97  * @param array $value
98  *
99  * @return string
100  */
101  public function encode($value)
102  {
103  $callback = $this->encodeFunction;
104  return $callback($value);
105  }
106 
107  /**
108  * @param string $value
109  *
110  * @return array
111  */
112  public function decode($value)
113  {
114  if($value <> '')
115  {
116  $callback = $this->decodeFunction;
117  return $callback($value);
118  }
119 
120  return [];
121  }
122 
123  /**
124  * @param $value
125  *
126  * @return mixed
127  * @throws \Bitrix\Main\ArgumentException
128  */
129  public function encodeJson($value)
130  {
131  return Json::encode($value);
132  }
133 
134  /**
135  * @param $value
136  *
137  * @return mixed
138  * @throws \Bitrix\Main\ArgumentException
139  */
140  public function decodeJson($value)
141  {
142  return Json::decode($value);
143  }
144 
145  /**
146  * @param $value
147  *
148  * @return string
149  */
150  public function encodePhp($value)
151  {
152  return serialize($value);
153  }
154 
155  /**
156  * @param $value
157  *
158  * @return array
159  */
160  public function decodePhp($value)
161  {
162  return unserialize($value);
163  }
164 
165  /**
166  * @param mixed $value
167  *
168  * @return array
169  */
170  public function cast($value)
171  {
172  return (array) $value;
173  }
174 
175  /**
176  * @param mixed $value
177  *
178  * @return mixed|string
179  * @throws \Bitrix\Main\SystemException
180  */
181  public function convertValueFromDb($value)
182  {
183  return $this->getConnection()->getSqlHelper()->convertFromDbString($value);
184  }
185 
186  /**
187  * @param mixed $value
188  *
189  * @return string
190  * @throws \Bitrix\Main\SystemException
191  */
192  public function convertValueToDb($value)
193  {
194  return $this->getConnection()->getSqlHelper()->convertToDbString($value);
195  }
196 
197  /**
198  * @return string
199  */
200  public function getGetterTypeHint()
201  {
202  return 'array';
203  }
204 
205  /**
206  * @return string
207  */
208  public function getSetterTypeHint()
209  {
210  return 'array';
211  }
212 }
configureSerializationJson()
Sets json serialization format.
Definition: arrayfield.php:43
configureUnserializeCallback($callback)
Custom decode handler.
Definition: arrayfield.php:88
configureSerializationPhp()
Sets php serialization format.
Definition: arrayfield.php:57
__construct($name, $parameters=[])
Definition: arrayfield.php:28
configureSerializeCallback($callback)
Custom encode handler.
Definition: arrayfield.php:73
static decode($data)
Takes a JSON encoded string and converts it into a PHP variable.
Definition: web/json.php:51
static encode($data, $options=null)
Returns a string containing the JSON representation of $data.
Definition: web/json.php:22
__construct(Base $connector)
Constructor.
Definition: resultview.php:40