Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'local/milestones/exceptions' into merges/prolic-excepti…
Browse files Browse the repository at this point in the history
…ons-log
  • Loading branch information
Ralph Schindler committed Oct 5, 2010
8 parents a9438e2 + d59be1f + 643e2df + a49e8f2 + 08fd26d + b52d5dc + de5abc1 + 7de1269 commit 34ca3a0
Show file tree
Hide file tree
Showing 29 changed files with 388 additions and 307 deletions.
74 changes: 36 additions & 38 deletions src/Decoder.php
Expand Up @@ -16,40 +16,44 @@
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/

/**
* @namespace
*/
namespace Zend\Json;

use Zend\Json\Exception\RuntimeException,
Zend\Json\Exception\InvalidArgumentException;

/**
* Decode JSON encoded string to PHP variable constructs
*
* @uses stdClass
* @uses \Zend\Json\Json
* @uses \Zend\Json\Exception
* @uses Zend\Json\Json
* @uses Zend\Json\Exception\RuntimeException
* @uses Zend\Json\Exception\InvalidArgumentException
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Decoder
{

/**
* Parse tokens used to decode the JSON object. These are not
* for public consumption, they are just used internally to the
* class.
*/
const EOF = 0;
const DATUM = 1;
const EOF = 0;
const DATUM = 1;
const LBRACE = 2;
const LBRACKET = 3;
const RBRACE = 4;
const RBRACKET = 5;
const COMMA = 6;
const COLON = 7;
const LBRACKET = 3;
const RBRACE = 4;
const RBRACKET = 5;
const COMMA = 6;
const COLON = 7;

/**
* Use to maintain a "pointer" to the source being decoded
Expand Down Expand Up @@ -105,12 +109,14 @@ protected function __construct($source, $decodeType)
$this->_token = self::EOF;
$this->_offset = 0;

// Normalize and set $decodeType
if (!in_array($decodeType, array(Json::TYPE_ARRAY, Json::TYPE_OBJECT)))
{
$decodeType = Json::TYPE_ARRAY;
switch ($decodeType) {
case Json::TYPE_ARRAY:
case Json::TYPE_OBJECT:
$this->_decodeType = $decodeType;
break;
default:
throw new InvalidArgumentException("Unknown decode type '{$decodeType}', please use one of the constants Json::TYPE_*");
}
$this->_decodeType = $decodeType;

// Set pointer at first token
$this->_getNextToken();
Expand All @@ -133,31 +139,20 @@ protected function __construct($source, $decodeType)
* return a StdClass object instead, pass {@link Zend_Json::TYPE_OBJECT} to
* the $objectDecodeType parameter.
*
* Throws a Zend_Json_Exception if the source string is null.
*
* @static
* @access public
* @param string $source String to be decoded
* @param int $objectDecodeType How objects should be decoded; should be
* either or {@link Zend_Json::TYPE_ARRAY} or
* {@link Zend_Json::TYPE_OBJECT}; defaults to TYPE_ARRAY
* @return mixed
* @throws \Zend\Json\Exception
*/
public static function decode($source = null, $objectDecodeType = Json::TYPE_ARRAY)
public static function decode($source, $objectDecodeType = Json::TYPE_ARRAY)
{
if (null === $source) {
throw new Exception('Must specify JSON encoded source for decoding');
} elseif (!is_string($source)) {
throw new Exception('Can only decode JSON encoded strings');
}

$decoder = new self($source, $objectDecodeType);

return $decoder->_decodeValue();
}


/**
* Recursive driving rountine for supported toplevel tops
*
Expand Down Expand Up @@ -196,6 +191,7 @@ protected function _decodeValue()
* array.
*
* @return array|StdClass
* @throws Zend\Json\Exception\RuntimeException
*/
protected function _decodeObject()
{
Expand All @@ -204,14 +200,14 @@ protected function _decodeObject()

while ($tok && $tok != self::RBRACE) {
if ($tok != self::DATUM || ! is_string($this->_tokenValue)) {
throw new Exception('Missing key in object encoding: ' . $this->_source);
throw new RuntimeException('Missing key in object encoding: ' . $this->_source);
}

$key = $this->_tokenValue;
$tok = $this->_getNextToken();

if ($tok != self::COLON) {
throw new Exception('Missing ":" in object encoding: ' . $this->_source);
throw new RuntimeException('Missing ":" in object encoding: ' . $this->_source);
}

$tok = $this->_getNextToken();
Expand All @@ -223,7 +219,7 @@ protected function _decodeObject()
}

if ($tok != self::COMMA) {
throw new Exception('Missing "," in object encoding: ' . $this->_source);
throw new RuntimeException('Missing "," in object encoding: ' . $this->_source);
}

$tok = $this->_getNextToken();
Expand Down Expand Up @@ -252,6 +248,7 @@ protected function _decodeObject()
* [element, element2,...,elementN]
*
* @return array
* @throws Zend\Json\Exception\RuntimeException
*/
protected function _decodeArray()
{
Expand All @@ -269,14 +266,14 @@ protected function _decodeArray()
}

if ($tok != self::COMMA) {
throw new Exception('Missing "," in array encoding: ' . $this->_source);
throw new RuntimeException('Missing "," in array encoding: ' . $this->_source);
}

$tok = $this->_getNextToken();
}

$this->_getNextToken();
return($result);
return $result;
}


Expand All @@ -302,6 +299,7 @@ protected function _eatWhitespace()
* Retrieves the next token from the source stream
*
* @return int Token constant value specified in class definition
* @throws Zend\Json\Exception\RuntimeException
*/
protected function _getNextToken()
{
Expand Down Expand Up @@ -382,8 +380,7 @@ protected function _getNextToken()
$result .= '\'';
break;
default:
throw new Exception("Illegal escape "
. "sequence '" . $chr . "'");
throw new RuntimeException("Illegal escape sequence '{$chr}'");
}
} elseif($chr == '"') {
break;
Expand Down Expand Up @@ -433,24 +430,24 @@ protected function _getNextToken()

if (is_numeric($datum)) {
if (preg_match('/^0\d+$/', $datum)) {
throw new Exception("Octal notation not supported by JSON (value: $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 Exception("Illegal number format: $datum");
throw new RuntimeException("Illegal number format: {$datum}");
}

$this->_token = self::DATUM;
$this->_offset = $start + strlen($datum);
}
} else {
throw new Exception('Illegal Token');
throw new RuntimeException('Illegal Token');
}

return($this->_token);
return $this->_token;
}

/**
Expand All @@ -466,6 +463,7 @@ protected function _getNextToken()
*/
public static function decodeUnicodeString($chrs)
{
$chrs = (string)$chrs;
$delim = substr($chrs, 0, 1);
$utf8 = '';
$strlen_chrs = strlen($chrs);
Expand Down
16 changes: 10 additions & 6 deletions src/Encoder.php
Expand Up @@ -16,19 +16,22 @@
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/

/**
* @namespace
*/
namespace Zend\Json;

use Zend\Json\Exception\RecursionException,
Zend\Json\Exception\InvalidArgumentException;

/**
* Encode PHP constructs to JSON
*
* @uses ReflectionClass
* @uses \Zend\Json\Exception
* @uses Zend\Json\Exception\RecursionException
* @uses Zend\Json\Exception\InvalidArgumentException
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
Expand Down Expand Up @@ -117,7 +120,8 @@ protected function _encodeValue(&$value)
*
* @param $value object
* @return string
* @throws \Zend\Json\Exception If recursive checks are enabled and the object has been serialized previously
* @throws Zend\Json\Exception\RecursionException If recursive checks are enabled
* and the object has been serialized previously
*/
protected function _encodeObject(&$value)
{
Expand All @@ -130,7 +134,7 @@ protected function _encodeObject(&$value)
return '"* RECURSION (' . str_replace('\\', '\\\\', get_class($value)) . ') *"';

} else {
throw new Exception(
throw new RecursionException(
'Cycles not supported in JSON encoding, cycle introduced by '
. 'class "' . get_class($value) . '"'
);
Expand Down Expand Up @@ -403,13 +407,13 @@ private static function _encodeVariables(\ReflectionClass $cls)
* @param $package string Optional package name appended to JavaScript
* proxy class name
* @return string The class2 (JavaScript) encoding of the class
* @throws \Zend\Json\Exception
* @throws Zend\Json\Exception\InvalidArgumentException
*/
public static function encodeClass($className, $package = '')
{
$cls = new \ReflectionClass($className);
if (! $cls->isInstantiable()) {
throw new Exception("$className must be instantiable");
throw new InvalidArgumentException("'{$className}' must be instantiable");
}

return "Class.create('$package$className',{"
Expand Down
6 changes: 2 additions & 4 deletions src/Exception.php
Expand Up @@ -16,7 +16,6 @@
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/

/**
Expand All @@ -25,12 +24,11 @@
namespace Zend\Json;

/**
* @uses \Zend\Exception
* @uses Zend\Exception
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Exception extends \Zend\Exception
interface Exception
{}

35 changes: 35 additions & 0 deletions src/Exception/InvalidArgumentException.php
@@ -0,0 +1,35 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Json\Exception;

/**
* @uses InvalidArgumentException
* @uses Zend\Json\Exception
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidArgumentException extends \InvalidArgumentException implements \Zend\Json\Exception
{}
34 changes: 34 additions & 0 deletions src/Exception/RecursionException.php
@@ -0,0 +1,34 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Json\Exception;

/**
* @uses Zend\Json\Exception\RuntimeException
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RecursionException extends RuntimeException
{}

0 comments on commit 34ca3a0

Please sign in to comment.