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

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2 into ZF2-377
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-mabe committed Jul 17, 2012
7 parents c9c769e + dda791d + cb39e7e + 54a28dc + 70d382a + 8bbad0e + 9321185 commit d9b45ef
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 85 deletions.
76 changes: 40 additions & 36 deletions src/AbstractOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
*/
abstract class AbstractOptions implements ParameterObjectInterface
{
/**
* We use the __ prefix to avoid collisions with properties in
* user-implmentations.
*
* @var bool
*/
protected $__strictMode__ = true;

/**
* @param array|Traversable|null $options
* @return AbstractOptions
Expand Down Expand Up @@ -49,43 +57,23 @@ public function setFromArray($options)
}

/**
* @param string $key name of option with underscore
* @return string name of setter method
* @throws Exception\BadMethodCallException if setter method is undefined
* Cast to array
*
* @return array
*/
protected function assembleSetterNameFromKey($key)
public function toArray()
{
$parts = explode('_', $key);
$parts = array_map('ucfirst', $parts);
$setter = 'set' . implode('', $parts);
if (!method_exists($this, $setter)) {
throw new Exception\BadMethodCallException(
'The option "' . $key . '" does not '
. 'have a matching ' . $setter . ' setter method '
. 'which must be defined'
);
$array = array();
$transform = function($letters) {
$letter = array_shift($letters);
return '_' . strtolower($letter);
};
foreach ($this as $key => $value) {
if ($key === '__strictMode__') continue;
$normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
$array[$normalizedKey] = $value;
}
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 assembleGetterNameFromKey($key)
{
$parts = explode('_', $key);
$parts = array_map('ucfirst', $parts);
$getter = 'get' . implode('', $parts);
if (!method_exists($this, $getter)) {
throw new Exception\BadMethodCallException(
'The option "' . $key . '" does not '
. 'have a matching ' . $getter . ' getter method '
. 'which must be defined'
);
}
return $getter;
return $array;
}

/**
Expand All @@ -96,7 +84,16 @@ protected function assembleGetterNameFromKey($key)
*/
public function __set($key, $value)
{
$setter = $this->assembleSetterNameFromKey($key);
$setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
if ($this->__strictMode__ && !method_exists($this, $setter)) {
throw new Exception\BadMethodCallException(
'The option "' . $key . '" does not '
. 'have a matching ' . $setter . ' setter method '
. 'which must be defined'
);
} elseif (!$this->__strictMode__ && !method_exists($this, $setter)) {
return;
}
$this->{$setter}($value);
}

Expand All @@ -107,7 +104,14 @@ public function __set($key, $value)
*/
public function __get($key)
{
$getter = $this->assembleGetterNameFromKey($key);
$getter = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
if (!method_exists($this, $getter)) {
throw new Exception\BadMethodCallException(
'The option "' . $key . '" does not '
. 'have a matching ' . $getter . ' getter method '
. 'which must be defined'
);
}
return $this->{$getter}();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Hydrator/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Reflection implements HydratorInterface
* Simple in-memory array cache of ReflectionProperties used.
* @var array
*/
static protected $reflProperties = array();
protected static $reflProperties = array();

/**
* Extract values from an object
Expand Down
47 changes: 0 additions & 47 deletions src/SubClass.php

This file was deleted.

18 changes: 17 additions & 1 deletion test/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use ArrayObject;
use ZendTest\Stdlib\TestAsset\TestOptions;
use ZendTest\Stdlib\TestAsset\TestOptionsNoStrict;
use ZendTest\Stdlib\TestAsset\TestTraversable;
use Zend\Stdlib\Exception\InvalidArgumentException;

Expand All @@ -32,6 +33,22 @@ public function testConstructionWithTraversable()
$this->assertEquals(1, $options->test_field);
}

public function testInvalidFieldThrowsException()
{
$this->setExpectedException('BadMethodCallException');
$options = new TestOptions(array('foo' => 'bar'));
}

public function testNonStrictOptionsDoesNotThrowException()
{
try {
$options = new TestOptionsNoStrict(array('foo' => 'bar'));
} catch (\Exception $e) {
$this->fail('Nonstrict options should not throw an exception');
}
}


public function testConstructionWithNull()
{
try {
Expand All @@ -48,6 +65,5 @@ public function testUnsetting()
$this->assertEquals(true, isset($options->test_field));
unset($options->testField);
$this->assertEquals(false, isset($options->test_field));

}
}
33 changes: 33 additions & 0 deletions test/TestAsset/TestOptionsNoStrict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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 ZendTest\Stdlib\TestAsset;

use Zend\Stdlib\AbstractOptions;

/**
* Dummy TestOptions used to test Stdlib\Options
*/
class TestOptionsNoStrict extends AbstractOptions
{
protected $__strictMode__ = false;

protected $testField;

public function setTestField($value)
{
$this->testField = $value;
}

public function getTestField()
{
return $this->testField;
}
}

0 comments on commit d9b45ef

Please sign in to comment.