Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
Zend_Json_Encoder Class Reference

Static Public Member Functions

static encode ($value, $cycleCheck=false, $options=array())
 
static encodeClass ($className, $package='')
 
static encodeClasses (array $classNames, $package='')
 
static encodeUnicodeString ($value)
 

Protected Member Functions

 __construct ($cycleCheck=false, $options=array())
 
 _encodeValue (&$value)
 
 _encodeObject (&$value)
 
 _wasVisited (&$value)
 
 _encodeArray (&$array)
 
 _encodeDatum (&$value)
 
 _encodeString (&$string)
 

Static Protected Member Functions

static _utf82utf16 ($utf8)
 

Protected Attributes

 $_cycleCheck
 
 $_options = array()
 
 $_visited = array()
 

Detailed Description

Definition at line 30 of file Encoder.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $cycleCheck = false,
  $options = array() 
)
protected

Constructor

Parameters
boolean$cycleCheckWhether or not to check for recursion when encoding
array$optionsAdditional options used during encoding
Returns
void

Definition at line 60 of file Encoder.php.

61  {
62  $this->_cycleCheck = $cycleCheck;
63  $this->_options = $options;
64  }

Member Function Documentation

◆ _encodeArray()

_encodeArray ( $array)
protected

JSON encode an array value

Recursively encodes each value of an array and returns a JSON encoded array string.

Arrays are defined as integer-indexed arrays starting at index 0, where the last index is (count($array) -1); any deviation from that is considered an associative array, and will be encoded as such.

Parameters
array&$array
Returns
string

Definition at line 192 of file Encoder.php.

193  {
194  $tmpArray = array();
195 
196  // Check for associative array
197  if (!empty($array) && (array_keys($array) !== range(0, count($array) - 1))) {
198  // Associative array
199  $result = '{';
200  foreach ($array as $key => $value) {
201  $key = (string) $key;
202  $tmpArray[] = $this->_encodeString($key)
203  . ':'
204  . $this->_encodeValue($value);
205  }
206  $result .= implode(',', $tmpArray);
207  $result .= '}';
208  } else {
209  // Indexed array
210  $result = '[';
211  $length = count($array);
212  for ($i = 0; $i < $length; $i++) {
213  $tmpArray[] = $this->_encodeValue($array[$i]);
214  }
215  $result .= implode(',', $tmpArray);
216  $result .= ']';
217  }
218 
219  return $result;
220  }
$value
Definition: gender.phtml:16
_encodeString(&$string)
Definition: Encoder.php:255
_encodeValue(&$value)
Definition: Encoder.php:90
$i
Definition: gallery.phtml:31

◆ _encodeDatum()

_encodeDatum ( $value)
protected

JSON encode a basic data type (string, number, boolean, null)

If value type is not a string, number, boolean, or null, the string 'null' is returned.

Parameters
mixed&$value
Returns
string

Definition at line 232 of file Encoder.php.

233  {
234  $result = 'null';
235 
236  if (is_int($value) || is_float($value)) {
237  $result = (string) $value;
238  $result = str_replace(",", ".", $result);
239  } elseif (is_string($value)) {
240  $result = $this->_encodeString($value);
241  } elseif (is_bool($value)) {
242  $result = $value ? 'true' : 'false';
243  }
244 
245  return $result;
246  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
_encodeString(&$string)
Definition: Encoder.php:255

◆ _encodeObject()

_encodeObject ( $value)
protected

Encode an object to JSON by encoding each of the public properties

A special property is added to the JSON object called '__className' that contains the name of the class of $value. This is used to decode the object on the client into a specific class.

Parameters
object$value
Returns
string
Exceptions
Zend_Json_ExceptionIf recursive checks are enabled and the object has been serialized previously

Definition at line 114 of file Encoder.php.

115  {
116  if ($this->_cycleCheck) {
117  if ($this->_wasVisited($value)) {
118 
119  if (isset($this->_options['silenceCyclicalExceptions'])
120  && $this->_options['silenceCyclicalExceptions']===true) {
121 
122  return '"* RECURSION (' . get_class($value) . ') *"';
123 
124  } else {
125  #require_once 'Zend/Json/Exception.php';
126  throw new Zend_Json_Exception(
127  'Cycles not supported in JSON encoding, cycle introduced by '
128  . 'class "' . get_class($value) . '"'
129  );
130  }
131  }
132 
133  $this->_visited[] = $value;
134  }
135 
136  $props = '';
137  if (method_exists($value, 'toJson')) {
138  $props =',' . preg_replace("/^\{(.*)\}$/","\\1",$value->toJson());
139  } else {
140  if ($value instanceof IteratorAggregate) {
141  $propCollection = $value->getIterator();
142  } elseif ($value instanceof Iterator) {
143  $propCollection = $value;
144  } else {
145  $propCollection = get_object_vars($value);
146  }
147 
148  foreach ($propCollection as $name => $propValue) {
149  if (isset($propValue)) {
150  $props .= ','
151  . $this->_encodeString($name)
152  . ':'
153  . $this->_encodeValue($propValue);
154  }
155  }
156  }
157  $className = get_class($value);
158  return '{"__className":' . $this->_encodeString($className)
159  . $props . '}';
160  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
_encodeString(&$string)
Definition: Encoder.php:255
_encodeValue(&$value)
Definition: Encoder.php:90
_wasVisited(&$value)
Definition: Encoder.php:169
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ _encodeString()

_encodeString ( $string)
protected

JSON encode a string value by escaping characters as necessary

Parameters
string&$value
Returns
string

Definition at line 255 of file Encoder.php.

256  {
257  // Escape these characters with a backslash:
258  // " \ / \n \r \t \b \f
259  $search = array('\\', "\n", "\t", "\r", "\b", "\f", '"', '/');
260  $replace = array('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"', '\\/');
261  $string = str_replace($search, $replace, $string);
262 
263  // Escape certain ASCII characters:
264  // 0x08 => \b
265  // 0x0c => \f
266  $string = str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
267  $string = self::encodeUnicodeString($string);
268 
269  return '"' . $string . '"';
270  }
static encodeUnicodeString($value)
Definition: Encoder.php:450

◆ _encodeValue()

_encodeValue ( $value)
protected

Recursive driver which determines the type of value to be encoded and then dispatches to the appropriate method. $values are either

Parameters
mixed$valueThe value to be encoded
Returns
string Encoded value

Definition at line 90 of file Encoder.php.

91  {
92  if (is_object($value)) {
93  return $this->_encodeObject($value);
94  } else if (is_array($value)) {
95  return $this->_encodeArray($value);
96  }
97 
98  return $this->_encodeDatum($value);
99  }
_encodeDatum(&$value)
Definition: Encoder.php:232
_encodeObject(&$value)
Definition: Encoder.php:114
$value
Definition: gender.phtml:16
_encodeArray(&$array)
Definition: Encoder.php:192

◆ _utf82utf16()

static _utf82utf16 (   $utf8)
staticprotected

Convert a string from one UTF-8 char to one UTF-16 char.

Normally should be handled by mb_convert_encoding, but provides a slower PHP-only method for installations that lack the multibye string extension.

This method is from the Solar Framework by Paul M. Jones

string $utf8 UTF-8 character string UTF-16 character

Definition at line 545 of file Encoder.php.

546  {
547  // Check for mb extension otherwise do by hand.
548  if( function_exists('mb_convert_encoding') ) {
549  return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
550  }
551 
552  switch (strlen($utf8)) {
553  case 1:
554  // this case should never be reached, because we are in ASCII range
555  // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
556  return $utf8;
557 
558  case 2:
559  // return a UTF-16 character from a 2-byte UTF-8 char
560  // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
561  return chr(0x07 & (ord($utf8{0}) >> 2))
562  . chr((0xC0 & (ord($utf8{0}) << 6))
563  | (0x3F & ord($utf8{1})));
564 
565  case 3:
566  // return a UTF-16 character from a 3-byte UTF-8 char
567  // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
568  return chr((0xF0 & (ord($utf8{0}) << 4))
569  | (0x0F & (ord($utf8{1}) >> 2)))
570  . chr((0xC0 & (ord($utf8{1}) << 6))
571  | (0x7F & ord($utf8{2})));
572  }
573 
574  // ignoring UTF-32 for now, sorry
575  return '';
576  }

◆ _wasVisited()

_wasVisited ( $value)
protected

Determine if an object has been serialized already

Parameters
mixed$value
Returns
boolean

Definition at line 169 of file Encoder.php.

170  {
171  if (in_array($value, $this->_visited, true)) {
172  return true;
173  }
174 
175  return false;
176  }
$value
Definition: gender.phtml:16

◆ encode()

static encode (   $value,
  $cycleCheck = false,
  $options = array() 
)
static

Use the JSON encoding scheme for the value specified

Parameters
mixed$valueThe value to be encoded
boolean$cycleCheckWhether or not to check for possible object recursion when encoding
array$optionsAdditional options used during encoding
Returns
string The encoded value

Definition at line 74 of file Encoder.php.

75  {
76  $encoder = new self(($cycleCheck) ? true : false, $options);
77  return $encoder->_encodeValue($value);
78  }
$value
Definition: gender.phtml:16

◆ encodeClass()

static encodeClass (   $className,
  $package = '' 
)
static

Encodes the given $className into the class2 model of encoding PHP classes into JavaScript class2 classes. NOTE: Currently only public methods and variables are proxied onto the client machine

Parameters
string$classNameThe name of the class, the class must be instantiable using a null constructor
string$packageOptional package name appended to JavaScript proxy class name
Returns
string The class2 (JavaScript) encoding of the class
Exceptions
Zend_Json_Exception

Definition at line 405 of file Encoder.php.

406  {
407  $cls = new ReflectionClass($className);
408  if (! $cls->isInstantiable()) {
409  #require_once 'Zend/Json/Exception.php';
410  throw new Zend_Json_Exception("$className must be instantiable");
411  }
412 
413  return "Class.create('$package$className',{"
414  . self::_encodeConstants($cls) .","
415  . self::_encodeMethods($cls) .","
416  . self::_encodeVariables($cls) .'});';
417  }
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31

◆ encodeClasses()

static encodeClasses ( array  $classNames,
  $package = '' 
)
static

Encode several classes at once

Returns JSON encoded classes, using encodeClass().

Parameters
array$classNames
string$package
Returns
string

Definition at line 429 of file Encoder.php.

430  {
431  $result = '';
432  foreach ($classNames as $className) {
433  $result .= self::encodeClass($className, $package);
434  }
435 
436  return $result;
437  }
static encodeClass($className, $package='')
Definition: Encoder.php:405
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31

◆ encodeUnicodeString()

static encodeUnicodeString (   $value)
static

Encode Unicode Characters to \u0000 ASCII syntax.

This algorithm was originally developed for the Solar Framework by Paul M. Jones

http://svn.solarphp.com/core/trunk/Solar/Json.php string $value string

Iterate over every character in the string, escaping with a slash or encoding to UTF-8 where necessary

Definition at line 450 of file Encoder.php.

451  {
452  $strlen_var = strlen($value);
453  $ascii = "";
454 
459  for($i = 0; $i < $strlen_var; $i++) {
460  $ord_var_c = ord($value[$i]);
461 
462  switch (true) {
463  case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
464  // characters U-00000000 - U-0000007F (same as ASCII)
465  $ascii .= $value[$i];
466  break;
467 
468  case (($ord_var_c & 0xE0) == 0xC0):
469  // characters U-00000080 - U-000007FF, mask 110XXXXX
470  // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
471  $char = pack('C*', $ord_var_c, ord($value[$i + 1]));
472  $i += 1;
473  $utf16 = self::_utf82utf16($char);
474  $ascii .= sprintf('\u%04s', bin2hex($utf16));
475  break;
476 
477  case (($ord_var_c & 0xF0) == 0xE0):
478  // characters U-00000800 - U-0000FFFF, mask 1110XXXX
479  // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
480  $char = pack('C*', $ord_var_c,
481  ord($value[$i + 1]),
482  ord($value[$i + 2]));
483  $i += 2;
484  $utf16 = self::_utf82utf16($char);
485  $ascii .= sprintf('\u%04s', bin2hex($utf16));
486  break;
487 
488  case (($ord_var_c & 0xF8) == 0xF0):
489  // characters U-00010000 - U-001FFFFF, mask 11110XXX
490  // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
491  $char = pack('C*', $ord_var_c,
492  ord($value[$i + 1]),
493  ord($value[$i + 2]),
494  ord($value[$i + 3]));
495  $i += 3;
496  $utf16 = self::_utf82utf16($char);
497  $ascii .= sprintf('\u%04s', bin2hex($utf16));
498  break;
499 
500  case (($ord_var_c & 0xFC) == 0xF8):
501  // characters U-00200000 - U-03FFFFFF, mask 111110XX
502  // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
503  $char = pack('C*', $ord_var_c,
504  ord($value[$i + 1]),
505  ord($value[$i + 2]),
506  ord($value[$i + 3]),
507  ord($value[$i + 4]));
508  $i += 4;
509  $utf16 = self::_utf82utf16($char);
510  $ascii .= sprintf('\u%04s', bin2hex($utf16));
511  break;
512 
513  case (($ord_var_c & 0xFE) == 0xFC):
514  // characters U-04000000 - U-7FFFFFFF, mask 1111110X
515  // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
516  $char = pack('C*', $ord_var_c,
517  ord($value[$i + 1]),
518  ord($value[$i + 2]),
519  ord($value[$i + 3]),
520  ord($value[$i + 4]),
521  ord($value[$i + 5]));
522  $i += 5;
523  $utf16 = self::_utf82utf16($char);
524  $ascii .= sprintf('\u%04s', bin2hex($utf16));
525  break;
526  }
527  }
528 
529  return $ascii;
530  }
$value
Definition: gender.phtml:16
static _utf82utf16($utf8)
Definition: Encoder.php:545
$i
Definition: gallery.phtml:31

Field Documentation

◆ $_cycleCheck

$_cycleCheck
protected

Definition at line 37 of file Encoder.php.

◆ $_options

$_options = array()
protected

Definition at line 44 of file Encoder.php.

◆ $_visited

$_visited = array()
protected

Definition at line 51 of file Encoder.php.


The documentation for this class was generated from the following file: