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

Commit

Permalink
Merge branch 'feature/module-manager-events' of https://github.com/Ev…
Browse files Browse the repository at this point in the history
…anDotPro/zf2 into features/module-events-refactor
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Zend\Stdlib;

abstract class Options implements ParameterObject
{

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

protected function processArray(array $config)
{
foreach ($config as $key => $value) {
$setter = $this->assembleSetterNameFromConfigKey($key);
$this->{$setter}($value);
}
}

protected function assembleSetterNameFromConfigKey($key)
{
$parts = explode('_', $key);
$parts = array_map('ucfirst', $parts);
$setter = 'set' . implode('', $parts);
if (!method_exists($this, $setter)) {
throw new \BadMethodCallException(
'The configuration key "' . $key . '" does not '
. 'have a matching ' . $setter . ' setter method '
. 'which must be defined'
);
}
return $setter;
}

protected function assembleGetterNameFromConfigKey($key)
{
$parts = explode('_', $key);
$parts = array_map('ucfirst', $parts);
$getter = 'get' . implode('', $parts);
if (!method_exists($this, $getter)) {
throw new \BadMethodCallException(
'The configuration key "' . $key . '" does not '
. 'have a matching ' . $getter . ' getter method '
. 'which must be defined'
);
}
return $getter;
}

public function __set($key, $value)
{
$setter = $this->assembleSetterNameFromConfigKey($key);
$this->{$setter}($value);
}

public function __get($key)
{
$getter = $this->assembleGetterNameFromConfigKey($key);
return $this->{$getter}();
}

public function __isset($key)
{
$getter = $this->assembleGetterNameFromConfigKey($key);
return !is_null($this->{$getter}());
}

public function __unset($key)
{
$setter = $this->assembleSetterNameFromConfigKey($key);
try {
$this->{$setter}(null);
} catch(\InvalidArgumentException $e) {
throw new \InvalidArgumentException(
'The class property $' . $key . ' cannot be unset as'
. ' NULL is an invalid value for it: ' . $e->getMessage()
);
}
}

}
16 changes: 16 additions & 0 deletions src/ParameterObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Zend\Stdlib;

interface ParameterObject
{

public function __set($key, $value);

public function __get($key);

public function __isset($key);

public function __unset($key);

}

0 comments on commit 00b350f

Please sign in to comment.