1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.3.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Database\Type;
16:
17: use Cake\Database\Driver;
18: use Cake\Database\Type;
19: use Cake\Database\TypeInterface;
20: use Cake\Database\Type\BatchCastingInterface;
21: use InvalidArgumentException;
22: use PDO;
23:
24: /**
25: * Json type converter.
26: *
27: * Use to convert json data between PHP and the database types.
28: */
29: class JsonType extends Type implements TypeInterface, BatchCastingInterface
30: {
31: /**
32: * Identifier name for this type.
33: *
34: * (This property is declared here again so that the inheritance from
35: * Cake\Database\Type can be removed in the future.)
36: *
37: * @var string|null
38: */
39: protected $_name;
40:
41: /**
42: * Constructor.
43: *
44: * (This method is declared here again so that the inheritance from
45: * Cake\Database\Type can be removed in the future.)
46: *
47: * @param string|null $name The name identifying this type
48: */
49: public function __construct($name = null)
50: {
51: $this->_name = $name;
52: }
53:
54: /**
55: * Convert a value data into a JSON string
56: *
57: * @param mixed $value The value to convert.
58: * @param \Cake\Database\Driver $driver The driver instance to convert with.
59: * @return string|null
60: */
61: public function toDatabase($value, Driver $driver)
62: {
63: if (is_resource($value)) {
64: throw new InvalidArgumentException('Cannot convert a resource value to JSON');
65: }
66:
67: return json_encode($value);
68: }
69:
70: /**
71: * Convert string values to PHP arrays.
72: *
73: * @param mixed $value The value to convert.
74: * @param \Cake\Database\Driver $driver The driver instance to convert with.
75: * @return string|array|null
76: */
77: public function toPHP($value, Driver $driver)
78: {
79: return json_decode($value, true);
80: }
81:
82: /**
83: * {@inheritDoc}
84: *
85: * @return array
86: */
87: public function manyToPHP(array $values, array $fields, Driver $driver)
88: {
89: foreach ($fields as $field) {
90: if (!isset($values[$field])) {
91: continue;
92: }
93:
94: $values[$field] = json_decode($values[$field], true);
95: }
96:
97: return $values;
98: }
99:
100: /**
101: * Get the correct PDO binding type for string data.
102: *
103: * @param mixed $value The value being bound.
104: * @param \Cake\Database\Driver $driver The driver.
105: * @return int
106: */
107: public function toStatement($value, Driver $driver)
108: {
109: return PDO::PARAM_STR;
110: }
111:
112: /**
113: * Marshals request data into a JSON compatible structure.
114: *
115: * @param mixed $value The value to convert.
116: * @return mixed Converted value.
117: */
118: public function marshal($value)
119: {
120: return $value;
121: }
122: }
123: