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

Commit

Permalink
Merge branch 'develop' of https://github.com/zendframework/zf2 into h…
Browse files Browse the repository at this point in the history
…otfix/zend-session-global-session-storage
  • Loading branch information
Mike Willbanks committed Jan 10, 2013
3 parents b229f2c + 136dbdd + 8d37cd0 commit 9506764
Show file tree
Hide file tree
Showing 26 changed files with 2,353 additions and 52 deletions.
10 changes: 5 additions & 5 deletions src/AbstractOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ abstract class AbstractOptions implements ParameterObjectInterface

/**
* @param array|Traversable|null $options
* @return AbstractOptions
* @throws Exception\InvalidArgumentException
*/
public function __construct($options = null)
{
Expand All @@ -41,7 +39,7 @@ public function __construct($options = null)
/**
* @param array|Traversable $options
* @throws Exception\InvalidArgumentException
* @return void
* @return AbstractOptions Provides fluent interface
*/
public function setFromArray($options)
{
Expand All @@ -55,6 +53,7 @@ public function setFromArray($options)
foreach ($options as $key => $value) {
$this->__set($key, $value);
}
return $this;
}

/**
Expand Down Expand Up @@ -115,6 +114,7 @@ public function __get($key)
. 'which must be defined'
);
}

return $this->{$getter}();
}

Expand All @@ -131,14 +131,14 @@ public function __isset($key)
/**
* @see ParameterObject::__unset()
* @param string $key
* @return void
* @throws Exception\InvalidArgumentException
* @return void
*/
public function __unset($key)
{
try {
$this->__set($key, null);
} catch (\InvalidArgumentException $e) {
} catch (Exception\BadMethodCallException $e) {
throw new Exception\InvalidArgumentException(
'The class property $' . $key . ' cannot be unset as'
. ' NULL is an invalid value for it',
Expand Down
72 changes: 42 additions & 30 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,77 +22,88 @@
abstract class ErrorHandler
{
/**
* Flag to mark started
* Active stack
*
* @var bool
* @var array
*/
protected static $started = false;
protected static $stack = array();

/**
* All errors as one instance of ErrorException
* using the previous exception support.
* Check if this error handler is active
*
* @var null|ErrorException
* @return boolean
*/
protected static $errorException = null;
public static function started()
{
return (bool) static::getNestedLevel();
}

/**
* If the error handler has been started.
* Get the current nested level
*
* @return bool
* @return int
*/
public static function started()
public static function getNestedLevel()
{
return static::$started;
return count(static::$stack);
}

/**
* Starting the error handler
*
* @param int $errorLevel
* @throws Exception\LogicException If already started
*/
public static function start($errorLevel = \E_WARNING)
{
if (static::started() === true) {
throw new Exception\LogicException('ErrorHandler already started');
if (!static::$stack) {
set_error_handler(array(get_called_class(), 'addError'), $errorLevel);
}

static::$started = true;
static::$errorException = null;

set_error_handler(array(get_called_class(), 'addError'), $errorLevel);
static::$stack[] = null;
}

/**
* Stopping the error handler
*
* @param bool $throw Throw the ErrorException if any
* @return null|ErrorException
* @throws Exception\LogicException If not started before
* @throws ErrorException If an error has been catched and $throw is true
*/
public static function stop($throw = false)
{
if (static::started() === false) {
throw new Exception\LogicException('ErrorHandler not started');
}
$errorException = null;

$errorException = static::$errorException;
if (static::$stack) {
$errorException = array_pop(static::$stack);

static::$started = false;
static::$errorException = null;
restore_error_handler();
if (!static::$stack) {
restore_error_handler();
}

if ($errorException && $throw) {
throw $errorException;
if ($errorException && $throw) {
throw $errorException;
}
}

return $errorException;
}

/**
* Add an error to the stack.
* Stop all active handler
*
* @return void
*/
public static function clean()
{
if (static::$stack) {
restore_error_handler();
}

static::$stack = array();
}

/**
* Add an error to the stack
*
* @param int $errno
* @param string $errstr
Expand All @@ -102,6 +113,7 @@ public static function stop($throw = false)
*/
public static function addError($errno, $errstr = '', $errfile = '', $errline = 0)
{
static::$errorException = new ErrorException($errstr, 0, $errno, $errfile, $errline, static::$errorException);
$stack = & static::$stack[ count(static::$stack) - 1 ];
$stack = new ErrorException($errstr, 0, $errno, $errfile, $errline, $stack);
}
}
22 changes: 22 additions & 0 deletions src/Exception/ExtensionNotLoadedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Stdlib
*/

namespace Zend\Stdlib\Exception;

/**
* Extension not loaded exception
*
* @category Zend
* @package Zend_Stdlib
* @subpackage Exception
*/
class ExtensionNotLoadedException extends RuntimeException
{
}
22 changes: 22 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Stdlib
*/

namespace Zend\Stdlib\Exception;

/**
* Runtime exception
*
* @category Zend
* @package Zend_Stdlib
* @subpackage Exception
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
18 changes: 16 additions & 2 deletions src/Hydrator/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Zend\Stdlib\Hydrator;

use ArrayObject;
use Zend\Stdlib\Exception;
use Zend\Stdlib\Hydrator\StrategyEnabledInterface;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;

Expand Down Expand Up @@ -44,7 +45,19 @@ public function __construct()
*/
public function getStrategy($name)
{
return $this->strategies[$name];
if (isset($this->strategies[$name])) {
return $this->strategies[$name];
}

if (!isset($this->strategies['*'])) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: no strategy by name of "%s", and no wildcard strategy present',
__METHOD__,
$name
));
}

return $this->strategies['*'];
}

/**
Expand All @@ -55,7 +68,8 @@ public function getStrategy($name)
*/
public function hasStrategy($name)
{
return array_key_exists($name, $this->strategies);
return array_key_exists($name, $this->strategies)
|| array_key_exists('*', $this->strategies);
}

/**
Expand Down
45 changes: 42 additions & 3 deletions src/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,61 @@
* @package Zend_Stdlib
* @subpackage Hydrator
*/
class ClassMethods extends AbstractHydrator
class ClassMethods extends AbstractHydrator implements HydratorOptionsInterface
{
/**
* Flag defining whether array keys are underscore-separated (true) or camel case (false)
* @var bool
*/
protected $underscoreSeparatedKeys;
protected $underscoreSeparatedKeys = true;

/**
* Define if extract values will use camel case or name with underscore
* @param bool $underscoreSeparatedKeys
* @param bool|array $underscoreSeparatedKeys
*/
public function __construct($underscoreSeparatedKeys = true)
{
parent::__construct();
$this->setUnderscoreSeparatedKeys($underscoreSeparatedKeys);
}

/**
* @param array|\Traversable $options
* @return ClassMethods
* @throws Exception\InvalidArgumentException
*/
public function setOptions($options)
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (!is_array($options)) {
throw new Exception\InvalidArgumentException(
'The options parameter must be an array or a Traversable'
);
}
if (isset($options['underscoreSeparatedKeys'])) {
$this->setUnderscoreSeparatedKeys($options['underscoreSeparatedKeys']);
}

return $this;
}

/**
* @param boolean $underscoreSeparatedKeys
* @return ClassMethods
*/
public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys)
{
$this->underscoreSeparatedKeys = $underscoreSeparatedKeys;
return $this;
}

/**
* @return boolean
*/
public function getUnderscoreSeparatedKeys()
{
return $this->underscoreSeparatedKeys;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Hydrator/HydratorOptionsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Stdlib
*/

namespace Zend\Stdlib\Hydrator;

/**
* @category Zend
* @package Zend_Stdlib
* @subpackage Hydrator
*/
interface HydratorOptionsInterface
{
/**
* @param array|\Traversable $options
* @return HydratorOptionsInterface
*/
public function setOptions($options);
}
Loading

0 comments on commit 9506764

Please sign in to comment.