updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json;
@@ -70,7 +68,7 @@ class Decoder
protected $decodeType;
/**
* @var $_tokenValue
* @var mixed
*/
protected $tokenValue;
@@ -99,7 +97,7 @@ class Decoder
// single, escaped unicode character
$utf16 = chr(hexdec(substr($chrs, ($i + 2), 2)))
. chr(hexdec(substr($chrs, ($i + 4), 2)));
$utf8char = self::_utf162utf8($utf16);
$utf8char = self::utf162utf8($utf16);
$search = ['\\', "\n", "\t", "\r", chr(0x08), chr(0x0C), '"', '\'', '/'];
if (in_array($utf8char, $search)) {
$replace = ['\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\\"', '\\\'', '\\/'];
@@ -109,7 +107,7 @@ class Decoder
$i += 5;
break;
case ($ordChrsC >= 0x20) && ($ordChrsC <= 0x7F):
$utf8 .= $chrs{$i};
$utf8 .= $chrs[$i];
break;
case ($ordChrsC & 0xE0) == 0xC0:
// characters U-00000080 - U-000007FF, mask 110XXXXX
@@ -152,8 +150,8 @@ class Decoder
*
* @param string $source String source to decode
* @param int $decodeType How objects should be decoded -- see
* {@link Zend\Json\Json::TYPE_ARRAY} and {@link Zend\Json\Json::TYPE_OBJECT} for
* valid values
* {@link Json::TYPE_ARRAY} and {@link Json::TYPE_OBJECT} for * valid
* values
* @throws InvalidArgumentException
*/
protected function __construct($source, $decodeType)
@@ -170,72 +168,73 @@ class Decoder
$this->decodeType = $decodeType;
break;
default:
throw new InvalidArgumentException("Unknown decode type '{$decodeType}', please use one of the constants Json::TYPE_*");
throw new InvalidArgumentException(sprintf(
'Unknown decode type "%s", please use one of the Json::TYPE_* constants',
$decodeType
));
}
// Set pointer at first token
$this->_getNextToken();
$this->getNextToken();
}
/**
* Decode a JSON source string
* Decode a JSON source string.
*
* Decodes a JSON encoded string. The value returned will be one of the
* Decodes a JSON encoded string; the value returned will be one of the
* following:
* - integer
* - float
* - boolean
* - null
* - stdClass
* - array
* - array of one or more of the above types
*
* By default, decoded objects will be returned as associative arrays; to
* return a stdClass object instead, pass {@link Zend\Json\Json::TYPE_OBJECT} to
* the $objectDecodeType parameter.
* - integer
* - float
* - boolean
* - null
* - stdClass
* - array
* - array of one or more of the above types
*
* @static
* @access public
* @param string $source String to be decoded
* By default, decoded objects will be returned as a stdClass object;
* to return associative arrays instead, pass {@link Json::TYPE_ARRAY}
* to the $objectDecodeType parameter.
*
* @param string $source String to be decoded.
* @param int $objectDecodeType How objects should be decoded; should be
* either or {@link Zend\Json\Json::TYPE_ARRAY} or
* {@link Zend\Json\Json::TYPE_OBJECT}; defaults to TYPE_ARRAY
* either or {@link Json::TYPE_ARRAY} or {@link Json::TYPE_OBJECT};
* defaults to Json::TYPE_OBJECT.
* @return mixed
*/
public static function decode($source, $objectDecodeType = Json::TYPE_OBJECT)
{
$decoder = new static($source, $objectDecodeType);
return $decoder->_decodeValue();
return $decoder->decodeValue();
}
/**
* Recursive driving routine for supported toplevel tops
* Recursive routine for supported toplevel types.
*
* @return mixed
*/
protected function _decodeValue()
protected function decodeValue()
{
switch ($this->token) {
case self::DATUM:
$result = $this->tokenValue;
$this->_getNextToken();
$this->getNextToken();
return($result);
case self::LBRACE:
return($this->_decodeObject());
return($this->decodeObject());
case self::LBRACKET:
return($this->_decodeArray());
return($this->decodeArray());
default:
return;
}
}
/**
* Decodes an object of the form:
* { "attribute: value, "attribute2" : value,...}
* Decodes an object of the form { "attribute: value, "attribute2" : value, ... }
*
* If Zend\Json\Encoder was used to encode the original object then
* a special attribute called __className which specifies a class
* name that should wrap the data contained within the encoded source.
* If Zend\Json\Encoder was used to encode the original object, then
* a special attribute called __className will specify a class
* name with which to wrap the data contained within the encoded source.
*
* Decodes to either an array or stdClass object, based on the value of
* {@link $decodeType}. If invalid $decodeType present, returns as an
@@ -244,36 +243,36 @@ class Decoder
* @return array|stdClass
* @throws RuntimeException
*/
protected function _decodeObject()
protected function decodeObject()
{
$members = [];
$tok = $this->_getNextToken();
$tok = $this->getNextToken();
while ($tok && $tok != self::RBRACE) {
if ($tok != self::DATUM || ! is_string($this->tokenValue)) {
throw new RuntimeException('Missing key in object encoding: ' . $this->source);
while ($tok && $tok !== self::RBRACE) {
if ($tok !== self::DATUM || ! is_string($this->tokenValue)) {
throw new RuntimeException(sprintf('Missing key in object encoding: %s', $this->source));
}
$key = $this->tokenValue;
$tok = $this->_getNextToken();
$tok = $this->getNextToken();
if ($tok != self::COLON) {
throw new RuntimeException('Missing ":" in object encoding: ' . $this->source);
if ($tok !== self::COLON) {
throw new RuntimeException(sprintf('Missing ":" in object encoding: %s', $this->source));
}
$this->_getNextToken();
$members[$key] = $this->_decodeValue();
$this->getNextToken();
$members[$key] = $this->decodeValue();
$tok = $this->token;
if ($tok == self::RBRACE) {
if ($tok === self::RBRACE) {
break;
}
if ($tok != self::COMMA) {
throw new RuntimeException('Missing "," in object encoding: ' . $this->source);
if ($tok !== self::COMMA) {
throw new RuntimeException(sprintf('Missing "," in object encoding: %s', $this->source));
}
$tok = $this->_getNextToken();
$tok = $this->getNextToken();
}
switch ($this->decodeType) {
@@ -288,70 +287,71 @@ class Decoder
}
break;
case Json::TYPE_ARRAY:
// intentionally fall-through
default:
$result = $members;
break;
}
$this->_getNextToken();
$this->getNextToken();
return $result;
}
/**
* Decodes a JSON array format:
* [element, element2,...,elementN]
* Decodes the JSON array format [element, element2, ..., elementN]
*
* @return array
* @throws RuntimeException
*/
protected function _decodeArray()
protected function decodeArray()
{
$result = [];
$tok = $this->_getNextToken(); // Move past the '['
$tok = $this->getNextToken(); // Move past the '['
$index = 0;
while ($tok && $tok != self::RBRACKET) {
$result[$index++] = $this->_decodeValue();
while ($tok && $tok !== self::RBRACKET) {
$result[$index++] = $this->decodeValue();
$tok = $this->token;
if ($tok == self::RBRACKET || !$tok) {
if ($tok == self::RBRACKET || ! $tok) {
break;
}
if ($tok != self::COMMA) {
throw new RuntimeException('Missing "," in array encoding: ' . $this->source);
if ($tok !== self::COMMA) {
throw new RuntimeException(sprintf('Missing "," in array encoding: %s', $this->source));
}
$tok = $this->_getNextToken();
$tok = $this->getNextToken();
}
$this->_getNextToken();
$this->getNextToken();
return $result;
}
/**
* Removes whitespace characters from the source input
* Removes whitespace characters from the source input.
*/
protected function _eatWhitespace()
protected function eatWhitespace()
{
if (preg_match('/([\t\b\f\n\r ])*/s', $this->source, $matches, PREG_OFFSET_CAPTURE, $this->offset)
&& $matches[0][1] == $this->offset) {
&& $matches[0][1] == $this->offset
) {
$this->offset += strlen($matches[0][0]);
}
}
/**
* Retrieves the next token from the source stream
* Retrieves the next token from the source stream.
*
* @return int Token constant value specified in class definition
* @return int Token constant value specified in class definition.
* @throws RuntimeException
*/
protected function _getNextToken()
protected function getNextToken()
{
$this->token = self::EOF;
$this->tokenValue = null;
$this->_eatWhitespace();
$this->eatWhitespace();
if ($this->offset >= $this->sourceLength) {
return(self::EOF);
@@ -362,7 +362,7 @@ class Decoder
$i = $this->offset;
$start = $i;
switch ($str{$i}) {
switch ($str[$i]) {
case '{':
$this->token = self::LBRACE;
break;
@@ -389,72 +389,76 @@ class Decoder
break;
}
$chr = $str{$i};
$chr = $str[$i];
if ($chr == '\\') {
$i++;
if ($i >= $strLength) {
break;
}
$chr = $str{$i};
switch ($chr) {
case '"':
$result .= '"';
break;
case '\\':
$result .= '\\';
break;
case '/':
$result .= '/';
break;
case 'b':
$result .= "\x08";
break;
case 'f':
$result .= "\x0c";
break;
case 'n':
$result .= "\x0a";
break;
case 'r':
$result .= "\x0d";
break;
case 't':
$result .= "\x09";
break;
case '\'':
$result .= '\'';
break;
default:
throw new RuntimeException("Illegal escape sequence '{$chr}'");
}
} elseif ($chr == '"') {
if ($chr === '"') {
break;
} else {
}
if ($chr !== '\\') {
$result .= $chr;
continue;
}
$i++;
if ($i >= $strLength) {
break;
}
$chr = $str[$i];
switch ($chr) {
case '"':
$result .= '"';
break;
case '\\':
$result .= '\\';
break;
case '/':
$result .= '/';
break;
case 'b':
$result .= "\x08";
break;
case 'f':
$result .= "\x0c";
break;
case 'n':
$result .= "\x0a";
break;
case 'r':
$result .= "\x0d";
break;
case 't':
$result .= "\x09";
break;
case '\'':
$result .= '\'';
break;
default:
throw new RuntimeException(sprintf('Illegal escape sequence "%s"', $chr));
}
} while ($i < $strLength);
$this->token = self::DATUM;
//$this->tokenValue = substr($str, $start + 1, $i - $start - 1);
$this->tokenValue = $result;
break;
case 't':
if (($i+ 3) < $strLength && substr($str, $start, 4) == "true") {
if (($i + 3) < $strLength && $start === strpos($str, "true", $start)) {
$this->token = self::DATUM;
}
$this->tokenValue = true;
$i += 3;
break;
case 'f':
if (($i+ 4) < $strLength && substr($str, $start, 5) == "false") {
if (($i + 4) < $strLength && $start === strpos($str, "false", $start)) {
$this->token = self::DATUM;
}
$this->tokenValue = false;
$i += 4;
break;
case 'n':
if (($i+ 3) < $strLength && substr($str, $start, 4) == "null") {
if (($i + 3) < $strLength && $start === strpos($str, "null", $start)) {
$this->token = self::DATUM;
}
$this->tokenValue = null;
@@ -462,78 +466,81 @@ class Decoder
break;
}
if ($this->token != self::EOF) {
if ($this->token !== self::EOF) {
$this->offset = $i + 1; // Consume the last token character
return($this->token);
return ($this->token);
}
$chr = $str{$i};
if ($chr == '-' || $chr == '.' || ($chr >= '0' && $chr <= '9')) {
if (preg_match('/-?([0-9])*(\.[0-9]*)?((e|E)((-|\+)?)[0-9]+)?/s', $str, $matches, PREG_OFFSET_CAPTURE, $start) && $matches[0][1] == $start) {
$datum = $matches[0][0];
$chr = $str[$i];
if (is_numeric($datum)) {
if (preg_match('/^0\d+$/', $datum)) {
throw new RuntimeException("Octal notation not supported by JSON (value: {$datum})");
} else {
$val = intval($datum);
$fVal = floatval($datum);
$this->tokenValue = ($val == $fVal ? $val : $fVal);
}
} else {
throw new RuntimeException("Illegal number format: {$datum}");
}
$this->token = self::DATUM;
$this->offset = $start + strlen($datum);
}
} else {
if ($chr !== '-' && $chr !== '.' && ($chr < '0' || $chr > '9')) {
throw new RuntimeException('Illegal Token');
}
if (preg_match('/-?([0-9])*(\.[0-9]*)?((e|E)((-|\+)?)[0-9]+)?/s', $str, $matches, PREG_OFFSET_CAPTURE, $start)
&& $matches[0][1] == $start
) {
$datum = $matches[0][0];
if (! is_numeric($datum)) {
throw new RuntimeException(sprintf('Illegal number format: %s', $datum));
}
if (preg_match('/^0\d+$/', $datum)) {
throw new RuntimeException(sprintf('Octal notation not supported by JSON (value: %o)', $datum));
}
$val = intval($datum);
$fVal = floatval($datum);
$this->tokenValue = ($val == $fVal ? $val : $fVal);
$this->token = self::DATUM;
$this->offset = $start + strlen($datum);
}
return $this->token;
}
/**
* Convert a string from one UTF-16 char to one UTF-8 char.
*
* Normally should be handled by mb_convert_encoding, but
* provides a slower PHP-only method for installations
* that lack the multibyte string extension.
* Normally should be handled by mb_convert_encoding, but provides a slower
* PHP-only method for installations that lack the multibyte string
* extension.
*
* This method is from the Solar Framework by Paul M. Jones
* This method is from the Solar Framework by Paul M. Jones.
*
* @link http://solarphp.com
* @param string $utf16 UTF-16 character
* @return string UTF-8 character
*/
protected static function _utf162utf8($utf16)
protected static function utf162utf8($utf16)
{
// Check for mb extension otherwise do by hand.
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
}
$bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
$bytes = (ord($utf16[0]) << 8) | ord($utf16[1]);
switch (true) {
case ((0x7F & $bytes) == $bytes):
// this case should never be reached, because we are in ASCII range
// This case should never be reached, because we are in ASCII range;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0x7F & $bytes);
case (0x07FF & $bytes) == $bytes:
// return a 2-byte UTF-8 character
// Return a 2-byte UTF-8 character;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0xC0 | (($bytes >> 6) & 0x1F))
. chr(0x80 | ($bytes & 0x3F));
. chr(0x80 | ($bytes & 0x3F));
case (0xFFFF & $bytes) == $bytes:
// return a 3-byte UTF-8 character
// Return a 3-byte UTF-8 character;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0xE0 | (($bytes >> 12) & 0x0F))
. chr(0x80 | (($bytes >> 6) & 0x3F))
. chr(0x80 | ($bytes & 0x3F));
. chr(0x80 | (($bytes >> 6) & 0x3F))
. chr(0x80 | ($bytes & 0x3F));
}
// ignoring UTF-32 for now, sorry

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json;
@@ -17,19 +15,19 @@ use Zend\Json\Exception\InvalidArgumentException;
use Zend\Json\Exception\RecursionException;
/**
* Encode PHP constructs to JSON
* Encode PHP constructs to JSON.
*/
class Encoder
{
/**
* Whether or not to check for possible cycling
* Whether or not to check for possible cycling.
*
* @var bool
*/
protected $cycleCheck;
/**
* Additional options used during encoding
* Additional options used during encoding.
*
* @var array
*/
@@ -43,27 +41,24 @@ class Encoder
protected $visited = [];
/**
* Constructor
*
* @param bool $cycleCheck Whether or not to check for recursion when encoding
* @param array $options Additional options used during encoding
* @return Encoder
* @param bool $cycleCheck Whether or not to check for recursion when encoding.
* @param array $options Additional options used during encoding.
*/
protected function __construct($cycleCheck = false, $options = [])
protected function __construct($cycleCheck = false, array $options = [])
{
$this->cycleCheck = $cycleCheck;
$this->options = $options;
}
/**
* Use the JSON encoding scheme for the value specified
* Use the JSON encoding scheme for the value specified.
*
* @param mixed $value The value to be encoded
* @param bool $cycleCheck Whether or not to check for possible object recursion when encoding
* @param array $options Additional options used during encoding
* @return string The encoded value
* @param mixed $value The value to be encoded.
* @param bool $cycleCheck Whether or not to check for possible object recursion when encoding.
* @param array $options Additional options used during encoding.
* @return string The encoded value.
*/
public static function encode($value, $cycleCheck = false, $options = [])
public static function encode($value, $cycleCheck = false, array $options = [])
{
$encoder = new static($cycleCheck, $options);
@@ -71,55 +66,62 @@ class Encoder
$value = $value->jsonSerialize();
}
return $encoder->_encodeValue($value);
return $encoder->encodeValue($value);
}
/**
* Recursive driver which determines the type of value to be encoded
* and then dispatches to the appropriate method. $values are either
* - objects (returns from {@link _encodeObject()})
* - arrays (returns from {@link _encodeArray()})
* - basic datums (e.g. numbers or strings) (returns from {@link _encodeDatum()})
* Encode a value to JSON.
*
* @param $value mixed The value to be encoded
* @return string Encoded value
* Recursive method which determines the type of value to be encoded
* and then dispatches to the appropriate method.
*
* $values are either
* - objects (returns from {@link encodeObject()})
* - arrays (returns from {@link encodeArray()})
* - scalars (returns from {@link encodeDatum()})
*
* @param $value mixed The value to be encoded.
* @return string Encoded value.
*/
protected function _encodeValue(&$value)
protected function encodeValue(&$value)
{
if (is_object($value)) {
return $this->_encodeObject($value);
} elseif (is_array($value)) {
return $this->_encodeArray($value);
return $this->encodeObject($value);
}
return $this->_encodeDatum($value);
if (is_array($value)) {
return $this->encodeArray($value);
}
return $this->encodeDatum($value);
}
/**
* Encode an object to JSON by encoding each of the public properties
* 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.
* A special property is added to the JSON object called '__className' that
* contains the classname of $value; this can be used by consumers of the
* resulting JSON to cast to the specific class.
*
* @param $value object
* @return string
* @throws RecursionException If recursive checks are enabled and the
* object has been serialized previously
* object has been serialized previously.
*/
protected function _encodeObject(&$value)
protected function encodeObject(&$value)
{
if ($this->cycleCheck) {
if ($this->_wasVisited($value)) {
if (isset($this->options['silenceCyclicalExceptions'])
&& $this->options['silenceCyclicalExceptions']===true) {
return '"* RECURSION (' . str_replace('\\', '\\\\', get_class($value)) . ') *"';
} else {
throw new RecursionException(
'Cycles not supported in JSON encoding, cycle introduced by '
. 'class "' . get_class($value) . '"'
);
if ($this->wasVisited($value)) {
if (! isset($this->options['silenceCyclicalExceptions'])
|| $this->options['silenceCyclicalExceptions'] !== true
) {
throw new RecursionException(sprintf(
'Cycles not supported in JSON encoding; cycle introduced by class "%s"',
get_class($value)
));
}
return '"* RECURSION (' . str_replace('\\', '\\\\', get_class($value)) . ') *"';
}
$this->visited[] = $value;
@@ -139,28 +141,30 @@ class Encoder
}
foreach ($propCollection as $name => $propValue) {
if (isset($propValue)) {
$props .= ','
. $this->_encodeValue($name)
. ':'
. $this->_encodeValue($propValue);
if (! isset($propValue)) {
continue;
}
$props .= ','
. $this->encodeValue($name)
. ':'
. $this->encodeValue($propValue);
}
}
$className = get_class($value);
return '{"__className":'
. $this->_encodeString($className)
. $this->encodeString($className)
. $props . '}';
}
/**
* Determine if an object has been serialized already
* Determine if an object has been serialized already.
*
* @param mixed $value
* @return bool
*/
protected function _wasVisited(&$value)
protected function wasVisited(&$value)
{
if (in_array($value, $this->visited, true)) {
return true;
@@ -170,86 +174,110 @@ class Encoder
}
/**
* JSON encode an array value
* 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.
* considered an associative array, and will be passed to
* {@link encodeAssociativeArray()}.
*
* @param $array array
* @return string
*/
protected function _encodeArray(&$array)
protected function encodeArray($array)
{
$tmpArray = [];
// Check for associative array
if (!empty($array) && (array_keys($array) !== range(0, count($array) - 1))) {
if (! empty($array) && (array_keys($array) !== range(0, count($array) - 1))) {
// Associative array
$result = '{';
foreach ($array as $key => $value) {
$key = (string) $key;
$tmpArray[] = $this->_encodeString($key)
. ':'
. $this->_encodeValue($value);
}
$result .= implode(',', $tmpArray);
$result .= '}';
} else {
// Indexed array
$result = '[';
$length = count($array);
for ($i = 0; $i < $length; $i++) {
$tmpArray[] = $this->_encodeValue($array[$i]);
}
$result .= implode(',', $tmpArray);
$result .= ']';
return $this->encodeAssociativeArray($array);
}
// Indexed array
$tmpArray = [];
$result = '[';
$length = count($array);
for ($i = 0; $i < $length; $i++) {
$tmpArray[] = $this->encodeValue($array[$i]);
}
$result .= implode(',', $tmpArray);
$result .= ']';
return $result;
}
/**
* JSON encode a basic data type (string, number, boolean, null)
* Encode an associative array to JSON.
*
* JSON does not have a concept of associative arrays; as such, we encode
* them to objects.
*
* @param array $array Array to encode.
* @return string
*/
protected function encodeAssociativeArray($array)
{
$tmpArray = [];
$result = '{';
foreach ($array as $key => $value) {
$tmpArray[] = sprintf(
'%s:%s',
$this->encodeString((string) $key),
$this->encodeValue($value)
);
}
$result .= implode(',', $tmpArray);
$result .= '}';
return $result;
}
/**
* JSON encode a scalar data type (string, number, boolean, null).
*
* If value type is not a string, number, boolean, or null, the string
* 'null' is returned.
*
* @param mixed $value
* @param mixed $value
* @return string
*/
protected function _encodeDatum(&$value)
protected function encodeDatum($value)
{
$result = 'null';
if (is_int($value) || is_float($value)) {
$result = (string) $value;
$result = str_replace(',', '.', $result);
} elseif (is_string($value)) {
$result = $this->_encodeString($value);
} elseif (is_bool($value)) {
$result = $value ? 'true' : 'false';
return str_replace(',', '.', (string) $value);
}
return $result;
if (is_string($value)) {
return $this->encodeString($value);
}
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
return 'null';
}
/**
* JSON encode a string value by escaping characters as necessary
* JSON encode a string value by escaping characters as necessary.
*
* @param string $string
* @return string
*/
protected function _encodeString(&$string)
protected function encodeString($string)
{
// @codingStandardsIgnoreStart
// Escape these characters with a backslash or unicode escape:
// " \ / \n \r \t \b \f
$search = ['\\', "\n", "\t", "\r", "\b", "\f", '"', '\'', '&', '<', '>', '/'];
$replace = ['\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\\u0022', '\\u0027', '\\u0026', '\\u003C', '\\u003E', '\\/'];
$replace = ['\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\\u0022', '\\u0027', '\\u0026', '\\u003C', '\\u003E', '\\/'];
$string = str_replace($search, $replace, $string);
// @codingStandardsIgnoreEnd
// Escape certain ASCII characters:
// 0x08 => \b
@@ -261,45 +289,45 @@ class Encoder
}
/**
* Encode the constants associated with the ReflectionClass
* parameter. The encoding format is based on the class2 format
* Encode the constants associated with the ReflectionClass parameter.
*
* @param ReflectionClass $cls
* The encoding format is based on the class2 format.
*
* @param ReflectionClass $class
* @return string Encoded constant block in class2 format
*/
private static function _encodeConstants(ReflectionClass $cls)
private static function encodeConstants(ReflectionClass $class)
{
$result = "constants : {";
$constants = $cls->getConstants();
$result = "constants:{";
$constants = $class->getConstants();
if (empty($constants)) {
return $result . '}';
}
$tmpArray = [];
if (!empty($constants)) {
foreach ($constants as $key => $value) {
$tmpArray[] = "$key: " . self::encode($value);
}
$result .= implode(', ', $tmpArray);
foreach ($constants as $key => $value) {
$tmpArray[] = sprintf('%s: %s', $key, self::encode($value));
}
$result .= implode(', ', $tmpArray);
return $result . "}";
}
/**
* Encode the public methods of the ReflectionClass in the
* class2 format
*
* @param ReflectionClass $cls
* @return string Encoded method fragment
* Encode the public methods of the ReflectionClass in the class2 format
*
* @param ReflectionClass $class
* @return string Encoded method fragment.
*/
private static function _encodeMethods(ReflectionClass $cls)
private static function encodeMethods(ReflectionClass $class)
{
$methods = $cls->getMethods();
$result = 'methods:{';
$result = 'methods:{';
$started = false;
foreach ($methods as $method) {
if (! $method->isPublic() || !$method->isUserDefined()) {
foreach ($class->getMethods() as $method) {
if (! $method->isPublic() || ! $method->isUserDefined()) {
continue;
}
@@ -308,105 +336,110 @@ class Encoder
}
$started = true;
$result .= '' . $method->getName(). ':function(';
$result .= sprintf('%s:function(', $method->getName());
if ('__construct' != $method->getName()) {
$parameters = $method->getParameters();
$argsStarted = false;
$argNames = "var argNames=[";
foreach ($parameters as $param) {
if ($argsStarted) {
$result .= ',';
}
$result .= $param->getName();
if ($argsStarted) {
$argNames .= ',';
}
$argNames .= '"' . $param->getName() . '"';
$argsStarted = true;
}
$argNames .= "];";
$result .= "){"
. $argNames
. 'var result = ZAjaxEngine.invokeRemoteMethod('
. "this, '" . $method->getName()
. "',argNames,arguments);"
. 'return(result);}';
} else {
$result .= "){}";
if ('__construct' === $method->getName()) {
$result .= '){}';
continue;
}
$argsStarted = false;
$argNames = "var argNames=[";
foreach ($method->getParameters() as $param) {
if ($argsStarted) {
$result .= ',';
}
$result .= $param->getName();
if ($argsStarted) {
$argNames .= ',';
}
$argNames .= sprintf('"%s"', $param->getName());
$argsStarted = true;
}
$argNames .= "];";
$result .= "){"
. $argNames
. 'var result = ZAjaxEngine.invokeRemoteMethod('
. "this, '"
. $method->getName()
. "',argNames,arguments);"
. 'return(result);}';
}
return $result . "}";
}
/**
* Encode the public properties of the ReflectionClass in the class2
* format.
* Encode the public properties of the ReflectionClass in the class2 format.
*
* @param ReflectionClass $cls
* @param ReflectionClass $class
* @return string Encode properties list
*
*/
private static function _encodeVariables(ReflectionClass $cls)
private static function encodeVariables(ReflectionClass $class)
{
$properties = $cls->getProperties();
$propValues = get_class_vars($cls->getName());
$result = "variables:{";
$propValues = get_class_vars($class->getName());
$result = "variables:{";
$tmpArray = [];
$tmpArray = [];
foreach ($properties as $prop) {
foreach ($class->getProperties() as $prop) {
if (! $prop->isPublic()) {
continue;
}
$tmpArray[] = $prop->getName()
. ':'
. self::encode($propValues[$prop->getName()]);
$name = $prop->getName();
$tmpArray[] = sprintf('%s:%s', $name, self::encode($propValues[$name]));
}
$result .= implode(',', $tmpArray);
return $result . "}";
}
/**
* 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
* 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
*
* @param $className string The name of the class, the class must be
* instantiable using a null constructor
* instantiable using a null constructor.
* @param $package string Optional package name appended to JavaScript
* proxy class name
* @return string The class2 (JavaScript) encoding of the class
* proxy class name.
* @return string The class2 (JavaScript) encoding of the class.
* @throws InvalidArgumentException
*/
public static function encodeClass($className, $package = '')
{
$cls = new \ReflectionClass($className);
if (! $cls->isInstantiable()) {
throw new InvalidArgumentException("'{$className}' must be instantiable");
$class = new ReflectionClass($className);
if (! $class->isInstantiable()) {
throw new InvalidArgumentException(sprintf(
'"%s" must be instantiable',
$className
));
}
return "Class.create('$package$className',{"
. self::_encodeConstants($cls) .","
. self::_encodeMethods($cls) .","
. self::_encodeVariables($cls) .'});';
return sprintf(
'Class.create(\'%s%s\',{%s,%s,%s});',
$package,
$className,
self::encodeConstants($class),
self::encodeMethods($class),
self::encodeVariables($class)
);
}
/**
* Encode several classes at once
* Encode several classes at once.
*
* Returns JSON encoded classes, using {@link encodeClass()}.
*
* @param array $classNames
* @param string[] $classNames
* @param string $package
* @return string
*/
@@ -423,8 +456,8 @@ class Encoder
/**
* Encode Unicode Characters to \u0000 ASCII syntax.
*
* This algorithm was originally developed for the
* Solar Framework by Paul M. Jones
* This algorithm was originally developed for the Solar Framework by Paul
* M. Jones.
*
* @link http://solarphp.com/
* @link https://github.com/solarphp/core/blob/master/Solar/Json.php
@@ -434,12 +467,10 @@ class Encoder
public static function encodeUnicodeString($value)
{
$strlenVar = strlen($value);
$ascii = "";
$ascii = "";
/**
* Iterate over every character in the string,
* escaping with a slash or encoding to UTF-8 where necessary
*/
// Iterate over every character in the string, escaping with a slash or
// encoding to UTF-8 where necessary.
for ($i = 0; $i < $strlenVar; $i++) {
$ordVarC = ord($value[$i]);
@@ -454,7 +485,7 @@ class Encoder
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ordVarC, ord($value[$i + 1]));
$i += 1;
$utf16 = self::_utf82utf16($char);
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
@@ -468,7 +499,7 @@ class Encoder
ord($value[$i + 2])
);
$i += 2;
$utf16 = self::_utf82utf16($char);
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
@@ -483,7 +514,7 @@ class Encoder
ord($value[$i + 3])
);
$i += 3;
$utf16 = self::_utf82utf16($char);
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
@@ -499,7 +530,7 @@ class Encoder
ord($value[$i + 4])
);
$i += 4;
$utf16 = self::_utf82utf16($char);
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
@@ -516,7 +547,7 @@ class Encoder
ord($value[$i + 5])
);
$i += 5;
$utf16 = self::_utf82utf16($char);
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
}
@@ -528,17 +559,17 @@ class Encoder
/**
* 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 multibyte string extension.
* Normally should be handled by mb_convert_encoding, but provides a slower
* PHP-only method for installations that lack the multibyte string
* extension.
*
* This method is from the Solar Framework by Paul M. Jones
* This method is from the Solar Framework by Paul M. Jones.
*
* @link http://solarphp.com
* @link http://solarphp.com
* @param string $utf8 UTF-8 character
* @return string UTF-16 character
*/
protected static function _utf82utf16($utf8)
protected static function utf82utf16($utf8)
{
// Check for mb extension otherwise do by hand.
if (function_exists('mb_convert_encoding')) {
@@ -547,19 +578,21 @@ class Encoder
switch (strlen($utf8)) {
case 1:
// this case should never be reached, because we are in ASCII range
// This case should never be reached, because we are in ASCII range;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return $utf8;
case 2:
// return a UTF-16 character from a 2-byte UTF-8 char
// Return a UTF-16 character from a 2-byte UTF-8 char;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0x07 & (ord($utf8{0}) >> 2)) . chr((0xC0 & (ord($utf8{0}) << 6)) | (0x3F & ord($utf8{1})));
return chr(0x07 & (ord($utf8[0]) >> 2)) . chr((0xC0 & (ord($utf8[0]) << 6)) | (0x3F & ord($utf8[1])));
case 3:
// return a UTF-16 character from a 3-byte UTF-8 char
// Return a UTF-16 character from a 3-byte UTF-8 char;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr((0xF0 & (ord($utf8{0}) << 4)) | (0x0F & (ord($utf8{1}) >> 2))) . chr((0xC0 & (ord($utf8{1}) << 6)) | (0x7F & ord($utf8{2})));
return chr((0xF0 & (ord($utf8[0]) << 4))
| (0x0F & (ord($utf8[1]) >> 2))) . chr((0xC0 & (ord($utf8[1]) << 6))
| (0x7F & ord($utf8[2])));
}
// ignoring UTF-32 for now, sorry

View File

@@ -1,15 +1,12 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json\Exception;
class BadMethodCallException extends \BadMethodCallException implements
ExceptionInterface
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
{
}

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json\Exception;

View File

@@ -1,15 +1,12 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json\Exception;
class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json\Exception;

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json\Exception;

View File

@@ -1,22 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json;
/**
* Class for Zend\Json\Json encode method.
* Encode a string to a native JavaScript expression.
*
* This class simply holds a string with a native Javascript Expression,
* so objects | arrays to be encoded with Zend\Json\Json can contain native
* Javascript Expressions.
* This class simply holds a string with a native JavaScript expression,
* so objects or arrays to be encoded with Zend\Json\Json can contain native
* JavaScript expressions.
*
* Example:
*
* <code>
* $foo = array(
* 'integer' => 9,
@@ -26,9 +25,17 @@ namespace Zend\Json;
* ),
* );
*
* Zend\Json\Json::encode($foo, false, array('enableJsonExprFinder' => true));
* // it will returns json encoded string:
* // {"integer":9,"string":"test string","function":function () {window.alert("javascript function encoded by Zend\Json\Json")}}
* echo Zend\Json\Json::encode($foo, false, ['enableJsonExprFinder' => true]);
* </code>
*
* The above returns the following JSON (formatted for readability):
*
* <code>
* {
* "integer": 9,
* "string": "test string",
* "function": function () {window.alert("javascript function encoded by Zend\Json\Json")}
* }
* </code>
*/
class Expr
@@ -41,9 +48,7 @@ class Expr
protected $expression;
/**
* Constructor
*
* @param string $expression the expression to hold.
* @param string $expression The expression to represent.
*/
public function __construct($expression)
{

View File

@@ -1,18 +1,14 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-json for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-json/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Json;
use SimpleXMLElement;
use Zend\Json\Exception\RecursionException;
use SplQueue;
use Zend\Json\Exception\RuntimeException;
use ZendXml\Security as XmlSecurity;
/**
* Class for encoding to and decoding from JSON.
@@ -20,34 +16,29 @@ use ZendXml\Security as XmlSecurity;
class Json
{
/**
* How objects should be encoded -- arrays or as stdClass. TYPE_ARRAY is 1
* so that it is a boolean true value, allowing it to be used with
* ext/json's functions.
* How objects should be encoded: as arrays or as stdClass.
*
* TYPE_ARRAY is 1, which also conveniently evaluates to a boolean true
* value, allowing it to be used with ext/json's functions.
*/
const TYPE_ARRAY = 1;
const TYPE_OBJECT = 0;
/**
* To check the allowed nesting depth of the XML tree during xml2json conversion.
*
* @var int
*/
public static $maxRecursionDepthAllowed = 25;
/**
* Whether or not to use the built-in PHP functions.
*
* @var bool
*/
public static $useBuiltinEncoderDecoder = false;
/**
* Decodes the given $encodedValue string which is
* encoded in the JSON format
* Decodes the given $encodedValue string from JSON.
*
* Uses ext/json's json_decode if available.
* Uses json_decode() from ext/json if available.
*
* @param string $encodedValue Encoded in JSON format
* @param int $objectDecodeType Optional; flag indicating how to decode
* objects. See {@link Zend\Json\Decoder::decode()} for details.
* objects. See {@link Decoder::decode()} for details.
* @return mixed
* @throws RuntimeException
*/
@@ -55,22 +46,7 @@ class Json
{
$encodedValue = (string) $encodedValue;
if (function_exists('json_decode') && static::$useBuiltinEncoderDecoder !== true) {
$decode = json_decode($encodedValue, $objectDecodeType);
switch (json_last_error()) {
case JSON_ERROR_NONE:
break;
case JSON_ERROR_DEPTH:
throw new RuntimeException('Decoding failed: Maximum stack depth exceeded');
case JSON_ERROR_CTRL_CHAR:
throw new RuntimeException('Decoding failed: Unexpected control character found');
case JSON_ERROR_SYNTAX:
throw new RuntimeException('Decoding failed: Syntax error');
default:
throw new RuntimeException('Decoding failed');
}
return $decode;
return self::decodeViaPhpBuiltIn($encodedValue, $objectDecodeType);
}
return Decoder::decode($encodedValue, $objectDecodeType);
@@ -96,71 +72,41 @@ class Json
* @param array $options Additional options used during encoding
* @return string JSON encoded object
*/
public static function encode($valueToEncode, $cycleCheck = false, $options = [])
public static function encode($valueToEncode, $cycleCheck = false, array $options = [])
{
if (is_object($valueToEncode)) {
if (method_exists($valueToEncode, 'toJson')) {
return $valueToEncode->toJson();
} elseif (method_exists($valueToEncode, 'toArray')) {
}
if (method_exists($valueToEncode, 'toArray')) {
return static::encode($valueToEncode->toArray(), $cycleCheck, $options);
}
}
// Pre-encoding look for Zend\Json\Expr objects and replacing by tmp ids
$javascriptExpressions = [];
// Pre-process and replace javascript expressions with placeholders
$javascriptExpressions = new SplQueue();
if (isset($options['enableJsonExprFinder'])
&& ($options['enableJsonExprFinder'] == true)
&& $options['enableJsonExprFinder'] == true
) {
$valueToEncode = static::_recursiveJsonExprFinder($valueToEncode, $javascriptExpressions);
$valueToEncode = static::recursiveJsonExprFinder($valueToEncode, $javascriptExpressions);
}
$prettyPrint = (isset($options['prettyPrint']) && ($options['prettyPrint'] == true));
// Encoding
if (function_exists('json_encode') && static::$useBuiltinEncoderDecoder !== true) {
$encodeOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;
$prettyPrint = (isset($options['prettyPrint']) && ($options['prettyPrint'] === true));
$encodedResult = self::encodeValue($valueToEncode, $cycleCheck, $options, $prettyPrint);
if ($prettyPrint && defined('JSON_PRETTY_PRINT')) {
$encodeOptions |= JSON_PRETTY_PRINT;
$prettyPrint = false;
}
$encodedResult = json_encode(
$valueToEncode,
$encodeOptions
);
} else {
$encodedResult = Encoder::encode($valueToEncode, $cycleCheck, $options);
}
if ($prettyPrint) {
$encodedResult = self::prettyPrint($encodedResult, ["indent" => " "]);
}
//only do post-processing to revert back the Zend\Json\Expr if any.
if (count($javascriptExpressions) > 0) {
$count = count($javascriptExpressions);
for ($i = 0; $i < $count; $i++) {
$magicKey = $javascriptExpressions[$i]['magicKey'];
$value = $javascriptExpressions[$i]['value'];
$encodedResult = str_replace(
//instead of replacing "key:magicKey", we replace directly magicKey by value because "key" never changes.
'"' . $magicKey . '"',
$value,
$encodedResult
);
}
}
// Post-process to revert back any Zend\Json\Expr instances.
$encodedResult = self::injectJavascriptExpressions($encodedResult, $javascriptExpressions);
return $encodedResult;
}
/**
* Check & Replace Zend\Json\Expr for tmp ids in the valueToEncode
* Discover and replace javascript expressions with temporary placeholders.
*
* Check if the value is a Zend\Json\Expr, and if replace its value
* with a magic key and save the javascript expression in an array.
* Check each value to determine if it is a Zend\Json\Expr; if so, replace the value with
* a magic key and add the javascript expression to the queue.
*
* NOTE this method is recursive.
*
@@ -168,239 +114,297 @@ class Json
*
* @see encode
* @param mixed $value a string - object property to be encoded
* @param array $javascriptExpressions
* @param SplQueue $javascriptExpressions
* @param null|string|int $currentKey
* @return mixed
*/
protected static function _recursiveJsonExprFinder(
&$value,
array &$javascriptExpressions,
protected static function recursiveJsonExprFinder(
$value,
SplQueue $javascriptExpressions,
$currentKey = null
) {
if ($value instanceof Expr) {
// TODO: Optimize with ascii keys, if performance is bad
$magicKey = "____" . $currentKey . "_" . (count($javascriptExpressions));
$javascriptExpressions[] = [
//if currentKey is integer, encodeUnicodeString call is not required.
"magicKey" => (is_int($currentKey)) ? $magicKey : Encoder::encodeUnicodeString($magicKey),
"value" => $value->__toString(),
];
$value = $magicKey;
} elseif (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = static::_recursiveJsonExprFinder($value[$k], $javascriptExpressions, $k);
}
} elseif (is_object($value)) {
foreach ($value as $k => $v) {
$value->$k = static::_recursiveJsonExprFinder($value->$k, $javascriptExpressions, $k);
}
$javascriptExpressions->enqueue([
// If currentKey is integer, encodeUnicodeString call is not required.
'magicKey' => (is_int($currentKey)) ? $magicKey : Encoder::encodeUnicodeString($magicKey),
'value' => $value,
]);
return $magicKey;
}
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = static::recursiveJsonExprFinder($value[$k], $javascriptExpressions, $k);
}
return $value;
}
if (is_object($value)) {
foreach ($value as $k => $v) {
$value->$k = static::recursiveJsonExprFinder($value->$k, $javascriptExpressions, $k);
}
return $value;
}
return $value;
}
/**
* Return the value of an XML attribute text or the text between
* the XML tags
*
* In order to allow Zend\Json\Expr from xml, we check if the node
* matches the pattern that try to detect if it is a new Zend\Json\Expr
* if it matches, we return a new Zend\Json\Expr instead of a text node
*
* @param SimpleXMLElement $simpleXmlElementObject
* @return Expr|string
*/
protected static function _getXmlValue($simpleXmlElementObject)
{
$pattern = '/^[\s]*new Zend[_\\]Json[_\\]Expr[\s]*\([\s]*[\"\']{1}(.*)[\"\']{1}[\s]*\)[\s]*$/';
$matchings = [];
$match = preg_match($pattern, $simpleXmlElementObject, $matchings);
if ($match) {
return new Expr($matchings[1]);
}
return (trim(strval($simpleXmlElementObject)));
}
/**
* _processXml - Contains the logic for xml2json
*
* The logic in this function is a recursive one.
*
* The main caller of this function (i.e. fromXml) needs to provide
* only the first two parameters i.e. the SimpleXMLElement object and
* the flag for ignoring or not ignoring XML attributes. The third parameter
* will be used internally within this function during the recursive calls.
*
* This function converts the SimpleXMLElement object into a PHP array by
* calling a recursive (protected static) function in this class. Once all
* the XML elements are stored in the PHP array, it is returned to the caller.
*
* @param SimpleXMLElement $simpleXmlElementObject
* @param bool $ignoreXmlAttributes
* @param int $recursionDepth
* @throws Exception\RecursionException if the XML tree is deeper than the allowed limit.
* @return array
*/
protected static function _processXml($simpleXmlElementObject, $ignoreXmlAttributes, $recursionDepth = 0)
{
// Keep an eye on how deeply we are involved in recursion.
if ($recursionDepth > static::$maxRecursionDepthAllowed) {
// XML tree is too deep. Exit now by throwing an exception.
throw new RecursionException(
"Function _processXml exceeded the allowed recursion depth of "
. static::$maxRecursionDepthAllowed
);
}
$children = $simpleXmlElementObject->children();
$name = $simpleXmlElementObject->getName();
$value = static::_getXmlValue($simpleXmlElementObject);
$attributes = (array) $simpleXmlElementObject->attributes();
if (!count($children)) {
if (!empty($attributes) && !$ignoreXmlAttributes) {
foreach ($attributes['@attributes'] as $k => $v) {
$attributes['@attributes'][$k] = static::_getXmlValue($v);
}
if (!empty($value)) {
$attributes['@text'] = $value;
}
return [$name => $attributes];
}
return [$name => $value];
}
$childArray = [];
foreach ($children as $child) {
$childname = $child->getName();
$element = static::_processXml($child, $ignoreXmlAttributes, $recursionDepth + 1);
if (array_key_exists($childname, $childArray)) {
if (empty($subChild[$childname])) {
$childArray[$childname] = [$childArray[$childname]];
$subChild[$childname] = true;
}
$childArray[$childname][] = $element[$childname];
} else {
$childArray[$childname] = $element[$childname];
}
}
if (!empty($attributes) && !$ignoreXmlAttributes) {
foreach ($attributes['@attributes'] as $k => $v) {
$attributes['@attributes'][$k] = static::_getXmlValue($v);
}
$childArray['@attributes'] = $attributes['@attributes'];
}
if (!empty($value)) {
$childArray['@text'] = $value;
}
return [$name => $childArray];
}
/**
* @deprecated by https://github.com/zendframework/zf2/pull/6778
* fromXml - Converts XML to JSON
*
* Converts a XML formatted string into a JSON formatted string.
* The value returned will be a string in JSON format.
*
* The caller of this function needs to provide only the first parameter,
* which is an XML formatted String. The second parameter is optional, which
* lets the user to select if the XML attributes in the input XML string
* should be included or ignored in xml2json conversion.
*
* This function converts the XML formatted string into a PHP array by
* calling a recursive (protected static) function in this class. Then, it
* converts that PHP array into JSON by calling the "encode" static function.
*
* NOTE: Encoding native javascript expressions via Zend\Json\Expr is not possible.
*
* @static
* @access public
* @param string $xmlStringContents XML String to be converted
* @param bool $ignoreXmlAttributes Include or exclude XML attributes in
* the xml2json conversion process.
* @return mixed - JSON formatted string on success
* @throws \Zend\Json\Exception\RuntimeException if the input not a XML formatted string
*/
public static function fromXml($xmlStringContents, $ignoreXmlAttributes = true)
{
// Load the XML formatted string into a Simple XML Element object.
$simpleXmlElementObject = XmlSecurity::scan($xmlStringContents);
// If it is not a valid XML content, throw an exception.
if (!$simpleXmlElementObject) {
throw new RuntimeException('Function fromXml was called with an invalid XML formatted string.');
} // End of if ($simpleXmlElementObject === null)
// Call the recursive function to convert the XML into a PHP array.
$resultArray = static::_processXml($simpleXmlElementObject, $ignoreXmlAttributes);
// Convert the PHP array to JSON using Zend\Json\Json encode method.
// It is just that simple.
$jsonStringOutput = static::encode($resultArray);
return($jsonStringOutput);
}
/**
* Pretty-print JSON string
*
* Use 'indent' option to select indentation string - by default it's a tab
* Use 'indent' option to select indentation string; by default, four
* spaces are used.
*
* @param string $json Original JSON string
* @param array $options Encoding options
* @return string
*/
public static function prettyPrint($json, $options = [])
public static function prettyPrint($json, array $options = [])
{
$tokens = preg_split('|([\{\}\]\[,])|', $json, -1, PREG_SPLIT_DELIM_CAPTURE);
$result = "";
$indent = 0;
$indentString = isset($options['indent']) ? $options['indent'] : ' ';
$ind = " ";
if (isset($options['indent'])) {
$ind = $options['indent'];
}
$json = trim($json);
$length = strlen($json);
$stack = [];
$result = '';
$inLiteral = false;
foreach ($tokens as $token) {
$token = trim($token);
if ($token == "") {
for ($i = 0; $i < $length; ++$i) {
switch ($json[$i]) {
case '{':
case '[':
if ($inLiteral) {
break;
}
$stack[] = $json[$i];
$result .= $json[$i];
while (isset($json[$i + 1]) && preg_match('/\s/', $json[$i + 1])) {
++$i;
}
if (isset($json[$i + 1]) && $json[$i + 1] !== '}' && $json[$i + 1] !== ']') {
$result .= "\n" . str_repeat($indentString, count($stack));
}
continue 2;
case '}':
case ']':
if ($inLiteral) {
break;
}
$last = end($stack);
if (($last === '{' && $json[$i] === '}')
|| ($last === '[' && $json[$i] === ']')
) {
array_pop($stack);
}
$result .= $json[$i];
while (isset($json[$i + 1]) && preg_match('/\s/', $json[$i + 1])) {
++$i;
}
if (isset($json[$i + 1]) && ($json[$i + 1] === '}' || $json[$i + 1] === ']')) {
$result .= "\n" . str_repeat($indentString, count($stack) - 1);
}
continue 2;
case '"':
$result .= '"';
if (! $inLiteral) {
$inLiteral = true;
} else {
$backslashes = 0;
$n = $i;
while ($json[--$n] === '\\') {
++$backslashes;
}
if (($backslashes % 2) === 0) {
$inLiteral = false;
while (isset($json[$i + 1]) && preg_match('/\s/', $json[$i + 1])) {
++$i;
}
if (isset($json[$i + 1]) && ($json[$i + 1] === '}' || $json[$i + 1] === ']')) {
$result .= "\n" . str_repeat($indentString, count($stack) - 1);
}
}
}
continue 2;
case ':':
if (! $inLiteral) {
$result .= ': ';
continue 2;
}
break;
case ',':
if (! $inLiteral) {
$result .= ',' . "\n" . str_repeat($indentString, count($stack));
continue 2;
}
break;
default:
if (! $inLiteral && preg_match('/\s/', $json[$i])) {
continue 2;
}
break;
}
$result .= $json[$i];
if ($inLiteral) {
continue;
}
if (preg_match('/^("(?:.*)"):[ ]?(.*)$/', $token, $matches)) {
$token = $matches[1] . ': ' . $matches[2];
while (isset($json[$i + 1]) && preg_match('/\s/', $json[$i + 1])) {
++$i;
}
$prefix = str_repeat($ind, $indent);
if (!$inLiteral && ($token == "{" || $token == "[")) {
$indent++;
if ($result != "" && $result[strlen($result)-1] == "\n") {
$result .= $prefix;
}
$result .= "$token\n";
} elseif (!$inLiteral && ($token == "}" || $token == "]")) {
$indent--;
$prefix = str_repeat($ind, $indent);
$result .= "\n$prefix$token";
} elseif (!$inLiteral && $token == ",") {
$result .= "$token\n";
} else {
$result .= ($inLiteral ? '' : $prefix) . $token;
//remove escaped backslash sequences causing false positives in next check
$token = str_replace('\\', '', $token);
// Count # of unescaped double-quotes in token, subtract # of
// escaped double-quotes and if the result is odd then we are
// inside a string literal
if ((substr_count($token, '"')-substr_count($token, '\\"')) % 2 != 0) {
$inLiteral = !$inLiteral;
}
if (isset($json[$i + 1]) && ($json[$i + 1] === '}' || $json[$i + 1] === ']')) {
$result .= "\n" . str_repeat($indentString, count($stack) - 1);
}
}
return $result;
}
/**
* Decode a value using the PHP built-in json_decode function.
*
* @param string $encodedValue
* @param int $objectDecodeType
* @return mixed
* @throws RuntimeException
*/
private static function decodeViaPhpBuiltIn($encodedValue, $objectDecodeType)
{
$decoded = json_decode($encodedValue, (bool) $objectDecodeType);
switch (json_last_error()) {
case JSON_ERROR_NONE:
return $decoded;
case JSON_ERROR_DEPTH:
throw new RuntimeException('Decoding failed: Maximum stack depth exceeded');
case JSON_ERROR_CTRL_CHAR:
throw new RuntimeException('Decoding failed: Unexpected control character found');
case JSON_ERROR_SYNTAX:
throw new RuntimeException('Decoding failed: Syntax error');
default:
throw new RuntimeException('Decoding failed');
}
}
/**
* Encode a value to JSON.
*
* Intermediary step between injecting JavaScript expressions.
*
* Delegates to either the PHP built-in json_encode operation, or the
* Encoder component, based on availability of the built-in and/or whether
* or not the component encoder is requested.
*
* @param mixed $valueToEncode
* @param bool $cycleCheck
* @param array $options
* @param bool $prettyPrint
* @return string
*/
private static function encodeValue($valueToEncode, $cycleCheck, array $options, $prettyPrint)
{
if (function_exists('json_encode') && static::$useBuiltinEncoderDecoder !== true) {
return self::encodeViaPhpBuiltIn($valueToEncode, $prettyPrint);
}
return self::encodeViaEncoder($valueToEncode, $cycleCheck, $options, $prettyPrint);
}
/**
* Encode a value to JSON using the PHP built-in json_encode function.
*
* Uses the encoding options:
*
* - JSON_HEX_TAG
* - JSON_HEX_APOS
* - JSON_HEX_QUOT
* - JSON_HEX_AMP
*
* If $prettyPrint is boolean true, also uses JSON_PRETTY_PRINT.
*
* @param mixed $valueToEncode
* @param bool $prettyPrint
* @return string|false Boolean false return value if json_encode is not
* available, or the $useBuiltinEncoderDecoder flag is enabled.
*/
private static function encodeViaPhpBuiltIn($valueToEncode, $prettyPrint = false)
{
if (! function_exists('json_encode') || static::$useBuiltinEncoderDecoder === true) {
return false;
}
$encodeOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;
if ($prettyPrint) {
$encodeOptions |= JSON_PRETTY_PRINT;
}
return json_encode($valueToEncode, $encodeOptions);
}
/**
* Encode a value to JSON using the Encoder class.
*
* Passes the value, cycle check flag, and options to Encoder::encode().
*
* Once the result is returned, determines if pretty printing is required,
* and, if so, returns the result of that operation, otherwise returning
* the encoded value.
*
* @param mixed $valueToEncode
* @param bool $cycleCheck
* @param array $options
* @param bool $prettyPrint
* @return string
*/
private static function encodeViaEncoder($valueToEncode, $cycleCheck, array $options, $prettyPrint)
{
$encodedResult = Encoder::encode($valueToEncode, $cycleCheck, $options);
if ($prettyPrint) {
return self::prettyPrint($encodedResult, ['indent' => ' ']);
}
return $encodedResult;
}
/**
* Inject javascript expressions into the encoded value.
*
* Loops through each, substituting the "magicKey" of each with its
* associated value.
*
* @param string $encodedValue
* @param SplQueue $javascriptExpressions
* @return string
*/
private static function injectJavascriptExpressions($encodedValue, SplQueue $javascriptExpressions)
{
foreach ($javascriptExpressions as $expression) {
$encodedValue = str_replace(
sprintf('"%s"', $expression['magicKey']),
$expression['value'],
(string) $encodedValue
);
}
return $encodedValue;
}
}

View File

@@ -1,87 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server;
use Zend\Server\Cache as ServerCache;
use Zend\Stdlib\ErrorHandler;
/**
* Zend\Json\Server\Cache: cache Zend\Json\Server\Server server definition and SMD
*/
class Cache extends ServerCache
{
/**
* Cache a service map description (SMD) to a file
*
* Returns true on success, false on failure
*
* @param string $filename
* @param \Zend\Json\Server\Server $server
* @return bool
*/
public static function saveSmd($filename, Server $server)
{
if (!is_string($filename) || (!file_exists($filename) && !is_writable(dirname($filename)))) {
return false;
}
ErrorHandler::start();
$test = file_put_contents($filename, $server->getServiceMap()->toJson());
ErrorHandler::stop();
if (0 === $test) {
return false;
}
return true;
}
/**
* Retrieve a cached SMD
*
* On success, returns the cached SMD (a JSON string); a failure, returns
* boolean false.
*
* @param string $filename
* @return string|false
*/
public static function getSmd($filename)
{
if (!is_string($filename) || !file_exists($filename) || !is_readable($filename)) {
return false;
}
ErrorHandler::start();
$smd = file_get_contents($filename);
ErrorHandler::stop();
if (false === $smd) {
return false;
}
return $smd;
}
/**
* Delete a file containing a cached SMD
*
* @param string $filename
* @return bool
*/
public static function deleteSmd($filename)
{
if (is_string($filename) && file_exists($filename)) {
unlink($filename);
return true;
}
return false;
}
}

View File

@@ -1,193 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server;
use Zend\Http\Client as HttpClient;
use Zend\Server\Client as ServerClient;
class Client implements ServerClient
{
/**
* Full address of the JSON-RPC service.
*
* @var string
*/
protected $serverAddress;
/**
* HTTP Client to use for requests.
*
* @var HttpClient
*/
protected $httpClient;
/**
* Request of the last method call.
*
* @var Request
*/
protected $lastRequest;
/**
* Response received from the last method call.
*
* @var Response
*/
protected $lastResponse;
/**
* Request ID counter.
*
* @var int
*/
protected $id = 0;
/**
* Create a new JSON-RPC client to a remote server.
*
* @param string $server Full address of the JSON-RPC service.
* @param HttpClient $httpClient HTTP Client to use for requests.
*/
public function __construct($server, HttpClient $httpClient = null)
{
$this->httpClient = $httpClient ?: new HttpClient();
$this->serverAddress = $server;
}
/**
* Sets the HTTP client object to use for connecting the JSON-RPC server.
*
* @param HttpClient $httpClient New HTTP client to use.
* @return Client Self instance.
*/
public function setHttpClient(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
return $this;
}
/**
* Gets the HTTP client object.
*
* @return HttpClient HTTP client.
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* The request of the last method call.
*
* @return Request Request instance.
*/
public function getLastRequest()
{
return $this->lastRequest;
}
/**
* The response received from the last method call.
*
* @return Response Response instance.
*/
public function getLastResponse()
{
return $this->lastResponse;
}
/**
* Perform a JSON-RPC request and return a response.
*
* @param Request $request Request.
* @return Response Response.
* @throws Exception\HttpException When HTTP communication fails.
*/
public function doRequest($request)
{
$this->lastRequest = $request;
$httpRequest = $this->httpClient->getRequest();
if ($httpRequest->getUriString() === null) {
$this->httpClient->setUri($this->serverAddress);
}
$headers = $httpRequest->getHeaders();
$headers->addHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
]);
if (!$headers->get('User-Agent')) {
$headers->addHeaderLine('User-Agent', 'Zend_Json_Server_Client');
}
$this->httpClient->setRawBody($request->__toString());
$this->httpClient->setMethod('POST');
$httpResponse = $this->httpClient->send();
if (!$httpResponse->isSuccess()) {
throw new Exception\HttpException(
$httpResponse->getReasonPhrase(),
$httpResponse->getStatusCode()
);
}
$response = new Response();
$this->lastResponse = $response;
// import all response data from JSON HTTP response
$response->loadJson($httpResponse->getBody());
return $response;
}
/**
* Send a JSON-RPC request to the service (for a specific method).
*
* @param string $method Name of the method we want to call.
* @param array $params Array of parameters for the method.
* @return mixed Method call results.
* @throws Exception\ErrorException When remote call fails.
*/
public function call($method, $params = [])
{
$request = $this->createRequest($method, $params);
$response = $this->doRequest($request);
if ($response->isError()) {
$error = $response->getError();
throw new Exception\ErrorException(
$error->getMessage(),
$error->getCode()
);
}
return $response->getResult();
}
/**
* Create request object.
*
* @param string $method Method to call.
* @param array $params List of arguments.
* @return Request Created request.
*/
protected function createRequest($method, array $params)
{
$request = new Request();
$request->setMethod($method)
->setParams($params)
->setId(++$this->id);
return $request;
}
}

View File

@@ -1,173 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server;
class Error
{
const ERROR_PARSE = -32700;
const ERROR_INVALID_REQUEST = -32600;
const ERROR_INVALID_METHOD = -32601;
const ERROR_INVALID_PARAMS = -32602;
const ERROR_INTERNAL = -32603;
const ERROR_OTHER = -32000;
/**
* Current code
* @var int
*/
protected $code = self::ERROR_OTHER;
/**
* Error data
* @var mixed
*/
protected $data;
/**
* Error message
* @var string
*/
protected $message;
/**
* Constructor
*
* @param string $message
* @param int $code
* @param mixed $data
*/
public function __construct($message = null, $code = self::ERROR_OTHER, $data = null)
{
$this->setMessage($message)
->setCode($code)
->setData($data);
}
/**
* Set error code.
*
* If the error code is 0, it will be set to -32000 (ERROR_OTHER).
*
* @param int $code
* @return \Zend\Json\Server\Error
*/
public function setCode($code)
{
if (!is_scalar($code) || is_bool($code) || is_float($code)) {
return $this;
}
if (is_string($code) && !is_numeric($code)) {
return $this;
}
$code = (int) $code;
if (0 === $code) {
$this->code = self::ERROR_OTHER;
} else {
$this->code = $code;
}
return $this;
}
/**
* Get error code
*
* @return int|null
*/
public function getCode()
{
return $this->code;
}
/**
* Set error message
*
* @param string $message
* @return \Zend\Json\Server\Error
*/
public function setMessage($message)
{
if (!is_scalar($message)) {
return $this;
}
$this->message = (string) $message;
return $this;
}
/**
* Get error message
*
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* Set error data
*
* @param mixed $data
* @return \Zend\Json\Server\Error
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get error data
*
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* Cast error to array
*
* @return array
*/
public function toArray()
{
return [
'code' => $this->getCode(),
'message' => $this->getMessage(),
'data' => $this->getData(),
];
}
/**
* Cast error to JSON
*
* @return string
*/
public function toJson()
{
return \Zend\Json\Json::encode($this->toArray());
}
/**
* Cast to string (JSON)
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
}

View File

@@ -1,20 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server\Exception;
use Zend\Json\Exception;
/**
* Thrown by Zend\Json\Server\Client when a JSON-RPC fault response is returned.
*/
class ErrorException extends Exception\BadMethodCallException implements
ExceptionInterface
{
}

View File

@@ -1,16 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server\Exception;
use Zend\Json\Exception\ExceptionInterface as Exception;
interface ExceptionInterface extends Exception
{
}

View File

@@ -1,18 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server\Exception;
/**
* Thrown by Zend\Json\Server\Client when an HTTP error occurs during an
* JSON-RPC method call.
*/
class HttpException extends RuntimeException
{
}

View File

@@ -1,17 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server\Exception;
use Zend\Json\Exception;
class InvalidArgumentException extends Exception\InvalidArgumentException implements
ExceptionInterface
{
}

View File

@@ -1,17 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server\Exception;
use Zend\Json\Exception;
class RuntimeException extends Exception\RuntimeException implements
ExceptionInterface
{
}

View File

@@ -1,294 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server;
use Zend\Json;
/**
* @todo Revised method regex to allow NS; however, should SMD be revised to strip PHP NS instead when attaching functions?
*/
class Request
{
/**
* Request ID
* @var mixed
*/
protected $id;
/**
* Flag
* @var bool
*/
protected $isMethodError = false;
/**
* Flag
* @var bool
*/
protected $isParseError = false;
/**
* Requested method
* @var string
*/
protected $method;
/**
* Regex for method
* @var string
*/
protected $methodRegex = '/^[a-z][a-z0-9\\\\_.]*$/i';
/**
* Request parameters
* @var array
*/
protected $params = [];
/**
* JSON-RPC version of request
* @var string
*/
protected $version = '1.0';
/**
* Set request state
*
* @param array $options
* @return \Zend\Json\Server\Request
*/
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
} elseif ($key == 'jsonrpc') {
$this->setVersion($value);
}
}
return $this;
}
/**
* Add a parameter to the request
*
* @param mixed $value
* @param string $key
* @return \Zend\Json\Server\Request
*/
public function addParam($value, $key = null)
{
if ((null === $key) || !is_string($key)) {
$index = count($this->params);
$this->params[$index] = $value;
} else {
$this->params[$key] = $value;
}
return $this;
}
/**
* Add many params
*
* @param array $params
* @return \Zend\Json\Server\Request
*/
public function addParams(array $params)
{
foreach ($params as $key => $value) {
$this->addParam($value, $key);
}
return $this;
}
/**
* Overwrite params
*
* @param array $params
* @return \Zend\Json\Server\Request
*/
public function setParams(array $params)
{
$this->params = [];
return $this->addParams($params);
}
/**
* Retrieve param by index or key
*
* @param int|string $index
* @return mixed|null Null when not found
*/
public function getParam($index)
{
if (array_key_exists($index, $this->params)) {
return $this->params[$index];
}
return;
}
/**
* Retrieve parameters
*
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Set request method
*
* @param string $name
* @return \Zend\Json\Server\Request
*/
public function setMethod($name)
{
if (!preg_match($this->methodRegex, $name)) {
$this->isMethodError = true;
} else {
$this->method = $name;
}
return $this;
}
/**
* Get request method name
*
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* Was a bad method provided?
*
* @return bool
*/
public function isMethodError()
{
return $this->isMethodError;
}
/**
* Was a malformed JSON provided?
*
* @return bool
*/
public function isParseError()
{
return $this->isParseError;
}
/**
* Set request identifier
*
* @param mixed $name
* @return \Zend\Json\Server\Request
*/
public function setId($name)
{
$this->id = (string) $name;
return $this;
}
/**
* Retrieve request identifier
*
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* Set JSON-RPC version
*
* @param string $version
* @return \Zend\Json\Server\Request
*/
public function setVersion($version)
{
if ('2.0' == $version) {
$this->version = '2.0';
} else {
$this->version = '1.0';
}
return $this;
}
/**
* Retrieve JSON-RPC version
*
* @return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Set request state based on JSON
*
* @param string $json
* @return void
*/
public function loadJson($json)
{
try {
$options = Json\Json::decode($json, Json\Json::TYPE_ARRAY);
$this->setOptions($options);
} catch (\Exception $e) {
$this->isParseError = true;
}
}
/**
* Cast request to JSON
*
* @return string
*/
public function toJson()
{
$jsonArray = [
'method' => $this->getMethod()
];
if (null !== ($id = $this->getId())) {
$jsonArray['id'] = $id;
}
$params = $this->getParams();
if (!empty($params)) {
$jsonArray['params'] = $params;
}
if ('2.0' == $this->getVersion()) {
$jsonArray['jsonrpc'] = '2.0';
}
return Json\Json::encode($jsonArray);
}
/**
* Cast request to string (JSON)
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
}

View File

@@ -1,46 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server\Request;
use Zend\Json\Server\Request as JsonRequest;
class Http extends JsonRequest
{
/**
* Raw JSON pulled from POST body
* @var string
*/
protected $rawJson;
/**
* Constructor
*
* Pull JSON request from raw POST body and use to populate request.
*
*/
public function __construct()
{
$json = file_get_contents('php://input');
$this->rawJson = $json;
if (!empty($json)) {
$this->loadJson($json);
}
}
/**
* Get JSON from raw POST body
*
* @return string
*/
public function getRawJson()
{
return $this->rawJson;
}
}

View File

@@ -1,279 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server;
use Zend\Json\Json;
class Response
{
/**
* Response error
* @var null|Error
*/
protected $error;
/**
* Request ID
* @var mixed
*/
protected $id;
/**
* Result
* @var mixed
*/
protected $result;
/**
* Service map
* @var Smd
*/
protected $serviceMap;
/**
* JSON-RPC version
* @var string
*/
protected $version;
/**
* @var $args
*/
protected $args;
/**
* Set response state
*
* @param array $options
* @return Response
*/
public function setOptions(array $options)
{
// re-produce error state
if (isset($options['error']) && is_array($options['error'])) {
$error = $options['error'];
$options['error'] = new Error($error['message'], $error['code'], $error['data']);
}
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
} elseif ($key == 'jsonrpc') {
$this->setVersion($value);
}
}
return $this;
}
/**
* Set response state based on JSON
*
* @param string $json
* @return void
* @throws Exception\RuntimeException
*/
public function loadJson($json)
{
$options = Json::decode($json, Json::TYPE_ARRAY);
if (!is_array($options)) {
throw new Exception\RuntimeException('json is not a valid response; array expected');
}
$this->setOptions($options);
}
/**
* Set result
*
* @param mixed $value
* @return Response
*/
public function setResult($value)
{
$this->result = $value;
return $this;
}
/**
* Get result
*
* @return mixed
*/
public function getResult()
{
return $this->result;
}
// RPC error, if response results in fault
/**
* Set result error
*
* @param mixed $error
* @return Response
*/
public function setError(Error $error = null)
{
$this->error = $error;
return $this;
}
/**
* Get response error
*
* @return null|Error
*/
public function getError()
{
return $this->error;
}
/**
* Is the response an error?
*
* @return bool
*/
public function isError()
{
return $this->getError() instanceof Error;
}
/**
* Set request ID
*
* @param mixed $name
* @return Response
*/
public function setId($name)
{
$this->id = $name;
return $this;
}
/**
* Get request ID
*
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* Set JSON-RPC version
*
* @param string $version
* @return Response
*/
public function setVersion($version)
{
$version = (string) $version;
if ('2.0' == $version) {
$this->version = '2.0';
} else {
$this->version = null;
}
return $this;
}
/**
* Retrieve JSON-RPC version
*
* @return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Cast to JSON
*
* @return string
*/
public function toJson()
{
if ($this->isError()) {
$response = [
'error' => $this->getError()->toArray(),
'id' => $this->getId(),
];
} else {
$response = [
'result' => $this->getResult(),
'id' => $this->getId(),
];
}
if (null !== ($version = $this->getVersion())) {
$response['jsonrpc'] = $version;
}
return \Zend\Json\Json::encode($response);
}
/**
* Retrieve args
*
* @return mixed
*/
public function getArgs()
{
return $this->args;
}
/**
* Set args
*
* @param mixed $args
* @return self
*/
public function setArgs($args)
{
$this->args = $args;
return $this;
}
/**
* Set service map object
*
* @param Smd $serviceMap
* @return Response
*/
public function setServiceMap($serviceMap)
{
$this->serviceMap = $serviceMap;
return $this;
}
/**
* Retrieve service map
*
* @return Smd|null
*/
public function getServiceMap()
{
return $this->serviceMap;
}
/**
* Cast to string (JSON)
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
}

View File

@@ -1,62 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server\Response;
use Zend\Json\Server\Response as JsonResponse;
class Http extends JsonResponse
{
/**
* Emit JSON
*
* Send appropriate HTTP headers. If no Id, then return an empty string.
*
* @return string
*/
public function toJson()
{
$this->sendHeaders();
if (!$this->isError() && null === $this->getId()) {
return '';
}
return parent::toJson();
}
/**
* Send headers
*
* If headers are already sent, do nothing. If null ID, send HTTP 204
* header. Otherwise, send content type header based on content type of
* service map.
*
* @return void
*/
public function sendHeaders()
{
if (headers_sent()) {
return;
}
if (!$this->isError() && (null === $this->getId())) {
header('HTTP/1.1 204 No Content');
return;
}
if (null === ($smd = $this->getServiceMap())) {
return;
}
$contentType = $smd->getContentType();
if (!empty($contentType)) {
header('Content-Type: ' . $contentType);
}
}
}

View File

@@ -1,564 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server;
use ReflectionFunction;
use ReflectionMethod;
use Zend\Server\AbstractServer;
use Zend\Server\Definition;
use Zend\Server\Method;
use Zend\Server\Reflection;
class Server extends AbstractServer
{
/**#@+
* Version Constants
*/
const VERSION_1 = '1.0';
const VERSION_2 = '2.0';
/**#@-*/
/**
* Flag: whether or not to auto-emit the response
* @var bool
*/
protected $returnResponse = false;
/**
* Inherited from Zend\Server\AbstractServer
*
* @var bool Flag; allow overwriting existing methods when creating server definition
*/
protected $overwriteExistingMethods = true;
/**
* Request object
* @var Request
*/
protected $request;
/**
* Response object
* @var Response
*/
protected $response;
/**
* SMD object
* @var Smd
*/
protected $serviceMap;
/**
* SMD class accessors
* @var array
*/
protected $smdMethods;
/**
* Attach a function or callback to the server
*
* @param string|array|callable $function Valid PHP callback
* @param string $namespace Ignored
* @throws Exception\InvalidArgumentException if function invalid or not callable
* @return Server
*/
public function addFunction($function, $namespace = '')
{
if (!is_string($function) && (!is_array($function) || (2 > count($function)))) {
throw new Exception\InvalidArgumentException('Unable to attach function; invalid');
}
if (!is_callable($function)) {
throw new Exception\InvalidArgumentException('Unable to attach function; does not exist');
}
$argv = null;
if (2 < func_num_args()) {
$argv = func_get_args();
$argv = array_slice($argv, 2);
}
$class = null;
if (is_string($function)) {
$method = Reflection::reflectFunction($function, $argv, $namespace);
} else {
$class = array_shift($function);
$action = array_shift($function);
$reflection = Reflection::reflectClass($class, $argv, $namespace);
$methods = $reflection->getMethods();
$found = false;
foreach ($methods as $method) {
if ($action == $method->getName()) {
$found = true;
break;
}
}
if (!$found) {
$this->fault('Method not found', Error::ERROR_INVALID_METHOD);
return $this;
}
}
$definition = $this->_buildSignature($method, $class);
$this->_addMethodServiceMap($definition);
return $this;
}
/**
* Register a class with the server
*
* @param string $class
* @param string $namespace Ignored
* @param mixed $argv Ignored
* @return Server
*/
public function setClass($class, $namespace = '', $argv = null)
{
if (2 < func_num_args()) {
$argv = func_get_args();
$argv = array_slice($argv, 2);
}
$reflection = Reflection::reflectClass($class, $argv, $namespace);
foreach ($reflection->getMethods() as $method) {
$definition = $this->_buildSignature($method, $class);
$this->_addMethodServiceMap($definition);
}
return $this;
}
/**
* Indicate fault response
*
* @param string $fault
* @param int $code
* @param mixed $data
* @return Error
*/
public function fault($fault = null, $code = 404, $data = null)
{
$error = new Error($fault, $code, $data);
$this->getResponse()->setError($error);
return $error;
}
/**
* Handle request
*
* @param Request $request
* @return null|Response
* @throws Exception\InvalidArgumentException
*/
public function handle($request = false)
{
if ((false !== $request) && (!$request instanceof Request)) {
throw new Exception\InvalidArgumentException('Invalid request type provided; cannot handle');
} elseif ($request) {
$this->setRequest($request);
}
// Handle request
$this->_handle();
// Get response
$response = $this->_getReadyResponse();
// Emit response?
if (!$this->returnResponse) {
echo $response;
return;
}
// or return it?
return $response;
}
/**
* Load function definitions
*
* @param array|Definition $definition
* @throws Exception\InvalidArgumentException
* @return void
*/
public function loadFunctions($definition)
{
if (!is_array($definition) && (!$definition instanceof Definition)) {
throw new Exception\InvalidArgumentException('Invalid definition provided to loadFunctions()');
}
foreach ($definition as $key => $method) {
$this->table->addMethod($method, $key);
$this->_addMethodServiceMap($method);
}
}
public function setPersistence($mode)
{
}
/**
* Set request object
*
* @param Request $request
* @return Server
*/
public function setRequest(Request $request)
{
$this->request = $request;
return $this;
}
/**
* Get JSON-RPC request object
*
* @return Request
*/
public function getRequest()
{
if (null === ($request = $this->request)) {
$this->setRequest(new Request\Http());
}
return $this->request;
}
/**
* Set response object
*
* @param Response $response
* @return Server
*/
public function setResponse(Response $response)
{
$this->response = $response;
return $this;
}
/**
* Get response object
*
* @return Response
*/
public function getResponse()
{
if (null === ($response = $this->response)) {
$this->setResponse(new Response\Http());
}
return $this->response;
}
/**
* Set return response flag
*
* If true, {@link handle()} will return the response instead of
* automatically sending it back to the requesting client.
*
* The response is always available via {@link getResponse()}.
*
* @param bool $flag
* @return Server
*/
public function setReturnResponse($flag = true)
{
$this->returnResponse = (bool) $flag;
return $this;
}
/**
* Retrieve return response flag
*
* @return bool
*/
public function getReturnResponse()
{
return $this->returnResponse;
}
// overloading for SMD metadata
/**
* Overload to accessors of SMD object
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
if (preg_match('/^(set|get)/', $method, $matches)) {
if (in_array($method, $this->_getSmdMethods())) {
if ('set' == $matches[1]) {
$value = array_shift($args);
$this->getServiceMap()->$method($value);
return $this;
} else {
return $this->getServiceMap()->$method();
}
}
}
return;
}
/**
* Retrieve SMD object
*
* @return Smd
*/
public function getServiceMap()
{
if (null === $this->serviceMap) {
$this->serviceMap = new Smd();
}
return $this->serviceMap;
}
/**
* Add service method to service map
*
* @param Method\Definition $method
* @return void
*/
protected function _addMethodServiceMap(Method\Definition $method)
{
$serviceInfo = [
'name' => $method->getName(),
'return' => $this->_getReturnType($method),
];
$params = $this->_getParams($method);
$serviceInfo['params'] = $params;
$serviceMap = $this->getServiceMap();
if (false !== $serviceMap->getService($serviceInfo['name'])) {
$serviceMap->removeService($serviceInfo['name']);
}
$serviceMap->addService($serviceInfo);
}
/**
* Translate PHP type to JSON type
*
* @param string $type
* @return string
*/
protected function _fixType($type)
{
return $type;
}
/**
* Get default params from signature
*
* @param array $args
* @param array $params
* @return array
*/
protected function _getDefaultParams(array $args, array $params)
{
if (false === $this->isAssociative($args)) {
$params = array_slice($params, count($args));
}
foreach ($params as $param) {
if (isset($args[$param['name']]) || !array_key_exists('default', $param)) {
continue;
}
$args[$param['name']] = $param['default'];
}
return $args;
}
/**
* check whether array is associative or not
*
* @param array $array
* @return bool
*/
private function isAssociative(array $array)
{
$keys = array_keys($array);
return ($keys != array_keys($keys));
}
/**
* Get method param type
*
* @param Method\Definition $method
* @return string|array
*/
protected function _getParams(Method\Definition $method)
{
$params = [];
foreach ($method->getPrototypes() as $prototype) {
foreach ($prototype->getParameterObjects() as $key => $parameter) {
if (!isset($params[$key])) {
$params[$key] = [
'type' => $parameter->getType(),
'name' => $parameter->getName(),
'optional' => $parameter->isOptional(),
];
if (null !== ($default = $parameter->getDefaultValue())) {
$params[$key]['default'] = $default;
}
$description = $parameter->getDescription();
if (!empty($description)) {
$params[$key]['description'] = $description;
}
continue;
}
$newType = $parameter->getType();
if (!is_array($params[$key]['type'])) {
if ($params[$key]['type'] == $newType) {
continue;
}
$params[$key]['type'] = (array) $params[$key]['type'];
} elseif (in_array($newType, $params[$key]['type'])) {
continue;
}
array_push($params[$key]['type'], $parameter->getType());
}
}
return $params;
}
/**
* Set response state
*
* @return Response
*/
protected function _getReadyResponse()
{
$request = $this->getRequest();
$response = $this->getResponse();
$response->setServiceMap($this->getServiceMap());
if (null !== ($id = $request->getId())) {
$response->setId($id);
}
if (null !== ($version = $request->getVersion())) {
$response->setVersion($version);
}
return $response;
}
/**
* Get method return type
*
* @param Method\Definition $method
* @return string|array
*/
protected function _getReturnType(Method\Definition $method)
{
$return = [];
foreach ($method->getPrototypes() as $prototype) {
$return[] = $prototype->getReturnType();
}
if (1 == count($return)) {
return $return[0];
}
return $return;
}
/**
* Retrieve list of allowed SMD methods for proxying
*
* @return array
*/
protected function _getSmdMethods()
{
if (null === $this->smdMethods) {
$this->smdMethods = [];
$methods = get_class_methods('Zend\\Json\\Server\\Smd');
foreach ($methods as $method) {
if (!preg_match('/^(set|get)/', $method)) {
continue;
}
if (strstr($method, 'Service')) {
continue;
}
$this->smdMethods[] = $method;
}
}
return $this->smdMethods;
}
/**
* Internal method for handling request
*
* @return void
*/
protected function _handle()
{
$request = $this->getRequest();
if ($request->isParseError()) {
return $this->fault('Parse error', Error::ERROR_PARSE);
}
if (!$request->isMethodError() && (null === $request->getMethod())) {
return $this->fault('Invalid Request', Error::ERROR_INVALID_REQUEST);
}
if ($request->isMethodError()) {
return $this->fault('Invalid Request', Error::ERROR_INVALID_REQUEST);
}
$method = $request->getMethod();
if (!$this->table->hasMethod($method)) {
return $this->fault('Method not found', Error::ERROR_INVALID_METHOD);
}
$params = $request->getParams();
$invokable = $this->table->getMethod($method);
$serviceMap = $this->getServiceMap();
$service = $serviceMap->getService($method);
$serviceParams = $service->getParams();
if (count($params) < count($serviceParams)) {
$params = $this->_getDefaultParams($params, $serviceParams);
}
//Make sure named parameters are passed in correct order
if (is_string(key($params))) {
$callback = $invokable->getCallback();
if ('function' == $callback->getType()) {
$reflection = new ReflectionFunction($callback->getFunction());
} else {
$reflection = new ReflectionMethod(
$callback->getClass(),
$callback->getMethod()
);
}
$orderedParams = [];
foreach ($reflection->getParameters() as $refParam) {
if (array_key_exists($refParam->getName(), $params)) {
$orderedParams[$refParam->getName()] = $params[$refParam->getName()];
} elseif ($refParam->isOptional()) {
$orderedParams[$refParam->getName()] = null;
} else {
return $this->fault('Invalid params', Error::ERROR_INVALID_PARAMS);
}
}
$params = $orderedParams;
}
try {
$result = $this->_dispatch($invokable, $params);
} catch (\Exception $e) {
return $this->fault($e->getMessage(), $e->getCode(), $e);
}
$this->getResponse()->setResult($result);
}
}

View File

@@ -1,461 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server;
use Zend\Json\Server\Exception\InvalidArgumentException;
use Zend\Json\Server\Exception\RuntimeException;
class Smd
{
const ENV_JSONRPC_1 = 'JSON-RPC-1.0';
const ENV_JSONRPC_2 = 'JSON-RPC-2.0';
const SMD_VERSION = '2.0';
/**
* Content type
* @var string
*/
protected $contentType = 'application/json';
/**
* Content type regex
* @var string
*/
protected $contentTypeRegex = '#[a-z]+/[a-z][a-z-]+#i';
/**
* Service description
* @var string
*/
protected $description;
/**
* Generate Dojo-compatible SMD
* @var bool
*/
protected $dojoCompatible = false;
/**
* Current envelope
* @var string
*/
protected $envelope = self::ENV_JSONRPC_1;
/**
* Allowed envelope types
* @var array
*/
protected $envelopeTypes = [
self::ENV_JSONRPC_1,
self::ENV_JSONRPC_2,
];
/**
* Service id
* @var string
*/
protected $id;
/**
* Services offered
* @var array
*/
protected $services = [];
/**
* Service target
* @var string
*/
protected $target;
/**
* Global transport
* @var string
*/
protected $transport = 'POST';
/**
* Allowed transport types
* @var array
*/
protected $transportTypes = ['POST'];
/**
* Set object state via options
*
* @param array $options
* @return Smd
*/
public function setOptions(array $options)
{
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (method_exists($this, $method)) {
$this->$method($value);
}
}
return $this;
}
/**
* Set transport
*
* @param string $transport
* @throws Exception\InvalidArgumentException
* @return \Zend\Json\Server\Smd
*/
public function setTransport($transport)
{
if (!in_array($transport, $this->transportTypes)) {
throw new InvalidArgumentException("Invalid transport '{$transport}' specified");
}
$this->transport = $transport;
return $this;
}
/**
* Get transport
*
* @return string
*/
public function getTransport()
{
return $this->transport;
}
/**
* Set envelope
*
* @param string $envelopeType
* @throws Exception\InvalidArgumentException
* @return Smd
*/
public function setEnvelope($envelopeType)
{
if (!in_array($envelopeType, $this->envelopeTypes)) {
throw new InvalidArgumentException("Invalid envelope type '{$envelopeType}'");
}
$this->envelope = $envelopeType;
return $this;
}
/**
* Retrieve envelope
*
* @return string
*/
public function getEnvelope()
{
return $this->envelope;
}
// Content-Type of response; default to application/json
/**
* Set content type
*
* @param string $type
* @throws Exception\InvalidArgumentException
* @return \Zend\Json\Server\Smd
*/
public function setContentType($type)
{
if (!preg_match($this->contentTypeRegex, $type)) {
throw new InvalidArgumentException("Invalid content type '{$type}' specified");
}
$this->contentType = $type;
return $this;
}
/**
* Retrieve content type
*
* @return string
*/
public function getContentType()
{
return $this->contentType;
}
/**
* Set service target
*
* @param string $target
* @return Smd
*/
public function setTarget($target)
{
$this->target = (string) $target;
return $this;
}
/**
* Retrieve service target
*
* @return string
*/
public function getTarget()
{
return $this->target;
}
/**
* Set service ID
*
* @param string $id
* @return Smd
*/
public function setId($id)
{
$this->id = (string) $id;
return $this->id;
}
/**
* Get service id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set service description
*
* @param string $description
* @return Smd
*/
public function setDescription($description)
{
$this->description = (string) $description;
return $this->description;
}
/**
* Get service description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Indicate whether or not to generate Dojo-compatible SMD
*
* @param bool $flag
* @return Smd
*/
public function setDojoCompatible($flag)
{
$this->dojoCompatible = (bool) $flag;
return $this;
}
/**
* Is this a Dojo compatible SMD?
*
* @return bool
*/
public function isDojoCompatible()
{
return $this->dojoCompatible;
}
/**
* Add Service
*
* @param Smd\Service|array $service
* @throws Exception\RuntimeException
* @throws Exception\InvalidArgumentException
* @return Smd
*/
public function addService($service)
{
if ($service instanceof Smd\Service) {
$name = $service->getName();
} elseif (is_array($service)) {
$service = new Smd\Service($service);
$name = $service->getName();
} else {
throw new InvalidArgumentException('Invalid service passed to addService()');
}
if (array_key_exists($name, $this->services)) {
throw new RuntimeException('Attempt to register a service already registered detected');
}
$this->services[$name] = $service;
return $this;
}
/**
* Add many services
*
* @param array $services
* @return Smd
*/
public function addServices(array $services)
{
foreach ($services as $service) {
$this->addService($service);
}
return $this;
}
/**
* Overwrite existing services with new ones
*
* @param array $services
* @return Smd
*/
public function setServices(array $services)
{
$this->services = [];
return $this->addServices($services);
}
/**
* Get service object
*
* @param string $name
* @return bool|Smd\Service
*/
public function getService($name)
{
if (array_key_exists($name, $this->services)) {
return $this->services[$name];
}
return false;
}
/**
* Return services
*
* @return array
*/
public function getServices()
{
return $this->services;
}
/**
* Remove service
*
* @param string $name
* @return bool
*/
public function removeService($name)
{
if (array_key_exists($name, $this->services)) {
unset($this->services[$name]);
return true;
}
return false;
}
/**
* Cast to array
*
* @return array
*/
public function toArray()
{
if ($this->isDojoCompatible()) {
return $this->toDojoArray();
}
$description = $this->getDescription();
$transport = $this->getTransport();
$envelope = $this->getEnvelope();
$contentType = $this->getContentType();
$SMDVersion = static::SMD_VERSION;
$service = compact('transport', 'envelope', 'contentType', 'SMDVersion', 'description');
if (null !== ($target = $this->getTarget())) {
$service['target'] = $target;
}
if (null !== ($id = $this->getId())) {
$service['id'] = $id;
}
$services = $this->getServices();
if (!empty($services)) {
$service['services'] = [];
foreach ($services as $name => $svc) {
$svc->setEnvelope($envelope);
$service['services'][$name] = $svc->toArray();
}
$service['methods'] = $service['services'];
}
return $service;
}
/**
* Export to DOJO-compatible SMD array
*
* @return array
*/
public function toDojoArray()
{
$SMDVersion = '.1';
$serviceType = 'JSON-RPC';
$service = compact('SMDVersion', 'serviceType');
$target = $this->getTarget();
$services = $this->getServices();
if (!empty($services)) {
$service['methods'] = [];
foreach ($services as $name => $svc) {
$method = [
'name' => $name,
'serviceURL' => $target,
];
$params = [];
foreach ($svc->getParams() as $param) {
$paramName = array_key_exists('name', $param) ? $param['name'] : $param['type'];
$params[] = [
'name' => $paramName,
'type' => $param['type'],
];
}
if (!empty($params)) {
$method['parameters'] = $params;
}
$service['methods'][] = $method;
}
}
return $service;
}
/**
* Cast to JSON
*
* @return string
*/
public function toJson()
{
return \Zend\Json\Json::encode($this->toArray());
}
/**
* Cast to string (JSON)
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
}

View File

@@ -1,465 +0,0 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Json\Server\Smd;
use Zend\Json\Server\Exception\InvalidArgumentException;
use Zend\Json\Server\Smd;
/**
* Create Service Mapping Description for a method
*
* @todo Revised method regex to allow NS; however, should SMD be revised to strip PHP NS instead when attaching functions?
*/
class Service
{
/**#@+
* Service metadata
* @var string
*/
protected $envelope = Smd::ENV_JSONRPC_1;
protected $name;
protected $return;
protected $target;
protected $transport = 'POST';
/**#@-*/
/**
* Allowed envelope types
* @var array
*/
protected $envelopeTypes = [
Smd::ENV_JSONRPC_1,
Smd::ENV_JSONRPC_2,
];
/**
* Regex for names
* @var string
*
* @link http://php.net/manual/en/language.oop5.basic.php
* @link http://www.jsonrpc.org/specification#request_object
*/
protected $nameRegex = '/^(?!^rpc\.)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\.\\\]*$/';
/**
* Parameter option types
* @var array
*/
protected $paramOptionTypes = [
'name' => 'is_string',
'optional' => 'is_bool',
'default' => null,
'description' => 'is_string',
];
/**
* Service params
* @var array
*/
protected $params = [];
/**
* Mapping of parameter types to JSON-RPC types
* @var array
*/
protected $paramMap = [
'any' => 'any',
'arr' => 'array',
'array' => 'array',
'assoc' => 'object',
'bool' => 'boolean',
'boolean' => 'boolean',
'dbl' => 'float',
'double' => 'float',
'false' => 'boolean',
'float' => 'float',
'hash' => 'object',
'integer' => 'integer',
'int' => 'integer',
'mixed' => 'any',
'nil' => 'null',
'null' => 'null',
'object' => 'object',
'string' => 'string',
'str' => 'string',
'struct' => 'object',
'true' => 'boolean',
'void' => 'null',
];
/**
* Allowed transport types
* @var array
*/
protected $transportTypes = [
'POST',
];
/**
* Constructor
*
* @param string|array $spec
* @throws InvalidArgumentException if no name provided
*/
public function __construct($spec)
{
if (is_string($spec)) {
$this->setName($spec);
} elseif (is_array($spec)) {
$this->setOptions($spec);
}
if (null == $this->getName()) {
throw new InvalidArgumentException('SMD service description requires a name; none provided');
}
}
/**
* Set object state
*
* @param array $options
* @return Service
*/
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
if ('options' == strtolower($key)) {
continue;
}
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
/**
* Set service name
*
* @param string $name
* @return Service
* @throws InvalidArgumentException
*/
public function setName($name)
{
$name = (string) $name;
if (!preg_match($this->nameRegex, $name)) {
throw new InvalidArgumentException("Invalid name '{$name} provided for service; must follow PHP method naming conventions");
}
$this->name = $name;
return $this;
}
/**
* Retrieve name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set Transport
*
* Currently limited to POST
*
* @param string $transport
* @throws InvalidArgumentException
* @return Service
*/
public function setTransport($transport)
{
if (!in_array($transport, $this->transportTypes)) {
throw new InvalidArgumentException("Invalid transport '{$transport}'; please select one of (" . implode(', ', $this->transportTypes) . ')');
}
$this->transport = $transport;
return $this;
}
/**
* Get transport
*
* @return string
*/
public function getTransport()
{
return $this->transport;
}
/**
* Set service target
*
* @param string $target
* @return Service
*/
public function setTarget($target)
{
$this->target = (string) $target;
return $this;
}
/**
* Get service target
*
* @return string
*/
public function getTarget()
{
return $this->target;
}
/**
* Set envelope type
*
* @param string $envelopeType
* @throws InvalidArgumentException
* @return Service
*/
public function setEnvelope($envelopeType)
{
if (!in_array($envelopeType, $this->envelopeTypes)) {
throw new InvalidArgumentException("Invalid envelope type '{$envelopeType}'; please specify one of (" . implode(', ', $this->envelopeTypes) . ')');
}
$this->envelope = $envelopeType;
return $this;
}
/**
* Get envelope type
*
* @return string
*/
public function getEnvelope()
{
return $this->envelope;
}
/**
* Add a parameter to the service
*
* @param string|array $type
* @param array $options
* @param int|null $order
* @throws InvalidArgumentException
* @return Service
*/
public function addParam($type, array $options = [], $order = null)
{
if (is_string($type)) {
$type = $this->_validateParamType($type);
} elseif (is_array($type)) {
foreach ($type as $key => $paramType) {
$type[$key] = $this->_validateParamType($paramType);
}
} else {
throw new InvalidArgumentException('Invalid param type provided');
}
$paramOptions = [
'type' => $type,
];
foreach ($options as $key => $value) {
if (in_array($key, array_keys($this->paramOptionTypes))) {
if (null !== ($callback = $this->paramOptionTypes[$key])) {
if (!$callback($value)) {
continue;
}
}
$paramOptions[$key] = $value;
}
}
$this->params[] = [
'param' => $paramOptions,
'order' => $order,
];
return $this;
}
/**
* Add params
*
* Each param should be an array, and should include the key 'type'.
*
* @param array $params
* @return Service
*/
public function addParams(array $params)
{
ksort($params);
foreach ($params as $options) {
if (!is_array($options)) {
continue;
}
if (!array_key_exists('type', $options)) {
continue;
}
$type = $options['type'];
$order = (array_key_exists('order', $options)) ? $options['order'] : null;
$this->addParam($type, $options, $order);
}
return $this;
}
/**
* Overwrite all parameters
*
* @param array $params
* @return Service
*/
public function setParams(array $params)
{
$this->params = [];
return $this->addParams($params);
}
/**
* Get all parameters
*
* Returns all params in specified order.
*
* @return array
*/
public function getParams()
{
$params = [];
$index = 0;
foreach ($this->params as $param) {
if (null === $param['order']) {
if (array_search($index, array_keys($params), true)) {
++$index;
}
$params[$index] = $param['param'];
++$index;
} else {
$params[$param['order']] = $param['param'];
}
}
ksort($params);
return $params;
}
/**
* Set return type
*
* @param string|array $type
* @throws InvalidArgumentException
* @return Service
*/
public function setReturn($type)
{
if (is_string($type)) {
$type = $this->_validateParamType($type, true);
} elseif (is_array($type)) {
foreach ($type as $key => $returnType) {
$type[$key] = $this->_validateParamType($returnType, true);
}
} else {
throw new InvalidArgumentException("Invalid param type provided ('" . gettype($type) . "')");
}
$this->return = $type;
return $this;
}
/**
* Get return type
*
* @return string|array
*/
public function getReturn()
{
return $this->return;
}
/**
* Cast service description to array
*
* @return array
*/
public function toArray()
{
$envelope = $this->getEnvelope();
$target = $this->getTarget();
$transport = $this->getTransport();
$parameters = $this->getParams();
$returns = $this->getReturn();
$name = $this->getName();
if (empty($target)) {
return compact('envelope', 'transport', 'name', 'parameters', 'returns');
}
return compact('envelope', 'target', 'transport', 'name', 'parameters', 'returns');
}
/**
* Return JSON encoding of service
*
* @return string
*/
public function toJson()
{
$service = [$this->getName() => $this->toArray()];
return \Zend\Json\Json::encode($service);
}
/**
* Cast to string
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
/**
* Validate parameter type
*
* @param string $type
* @param bool $isReturn
* @return string
* @throws InvalidArgumentException
*/
protected function _validateParamType($type, $isReturn = false)
{
if (!is_string($type)) {
throw new InvalidArgumentException("Invalid param type provided ('{$type}')");
}
if (!array_key_exists($type, $this->paramMap)) {
$type = 'object';
}
$paramType = $this->paramMap[$type];
if (!$isReturn && ('null' == $paramType)) {
throw new InvalidArgumentException("Invalid param type provided ('{$type}')");
}
return $paramType;
}
}