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

Commit

Permalink
Merge branch 'hotfix/cs-options' of https://github.com/Intiilapa/zf2
Browse files Browse the repository at this point in the history
…into hotfix/options-cs
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 20 deletions.
39 changes: 39 additions & 0 deletions src/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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_Stdlib
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

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

use Zend\Stdlib\Exception;

/**
* Bad method call exception
*
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class BadMethodCallException extends \BadMethodCallException
implements Exception
{
}
100 changes: 85 additions & 15 deletions src/Options.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
<?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_Stdlib
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Stdlib;

use Traversable;

/**
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Options implements ParameterObject
{

/**
* @param array|Traversable|null $config
* @return Options
* @throws Exception\InvalidArgumentException
*/
public function __construct($config = null)
{
if (!is_null($config)) {
if (is_array($config) || $config instanceof \Traversable) {
if (is_array($config) || $config instanceof Traversable) {
$this->processArray($config);
} else {
throw new \InvalidArgumentException(
'Parameter to \\Zend\\Stdlib\\Options\'s '
throw new Exception\InvalidArgumentException(
'Parameter to \Zend\Stdlib\Options\'s '
. 'constructor must be an array or implement the '
. '\\Traversable interface'
. 'Traversable interface'
);
}
}
}

/**
* @param array $config
* @return void
*/
protected function processArray(array $config)
{
foreach ($config as $key => $value) {
Expand All @@ -28,65 +65,98 @@ protected function processArray(array $config)
}
}

/**
* @param string $key name of option with underscore
* @return string name of setter method
* @throws Exception\BadMethodCallException if setter method is undefined
*/
protected function assembleSetterNameFromConfigKey($key)
{
$parts = explode('_', $key);
$parts = array_map('ucfirst', $parts);
$setter = 'set' . implode('', $parts);
if (!method_exists($this, $setter)) {
throw new \BadMethodCallException(
throw new Exception\BadMethodCallException(
'The configuration key "' . $key . '" does not '
. 'have a matching ' . $setter . ' setter method '
. 'which must be defined'
);
}
return $setter;
}


/**
* @param string $key name of option with underscore
* @return string name of getter method
* @throws Exception\BadMethodCallException if getter method is undefined
*/
protected function assembleGetterNameFromConfigKey($key)
{
$parts = explode('_', $key);
$parts = array_map('ucfirst', $parts);
$getter = 'get' . implode('', $parts);
if (!method_exists($this, $getter)) {
throw new \BadMethodCallException(
throw new Exception\BadMethodCallException(
'The configuration key "' . $key . '" does not '
. 'have a matching ' . $getter . ' getter method '
. 'which must be defined'
);
}
return $getter;
}


/**
* @see ParameterObject::__set()
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
$setter = $this->assembleSetterNameFromConfigKey($key);
$this->{$setter}($value);
}


/**
* @see ParameterObject::__get()
* @param string $key
* @return mixed
*/
public function __get($key)
{
$getter = $this->assembleGetterNameFromConfigKey($key);
return $this->{$getter}();
}


/**
* @see ParameterObject::__isset()
* @param string $key
* @return boolean
*/
public function __isset($key)
{
$getter = $this->assembleGetterNameFromConfigKey($key);
return !is_null($this->{$getter}());
}


/**
* @see ParameterObject::__unset()
* @param string $key
* @return void
* @throws Exception\InvalidArgumentException
*/
public function __unset($key)
{
$setter = $this->assembleSetterNameFromConfigKey($key);
try {
$this->{$setter}(null);
} catch(\InvalidArgumentException $e) {
throw new \InvalidArgumentException(
throw new Exception\InvalidArgumentException(
'The class property $' . $key . ' cannot be unset as'
. ' NULL is an invalid value for it: ' . $e->getMessage()
. ' NULL is an invalid value for it',
0,
$e
);
}
}

}
52 changes: 47 additions & 5 deletions src/ParameterObject.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
<?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_Stdlib
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Stdlib;

/**
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface ParameterObject
{

/**
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value);


/**
* @param string $key
* @return mixed
*/
public function __get($key);


/**
* @param string $key
* @return boolean
*/
public function __isset($key);

public function __unset($key);

/**
* @param string $key
* @return void
*/
public function __unset($key);
}

0 comments on commit ccab83f

Please sign in to comment.