Thanks to visit codestin.com
Credit goes to github.com

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

Commit 00b350f

Browse files
committed
Merge branch 'feature/module-manager-events' of https://github.com/EvanDotPro/zf2 into features/module-events-refactor

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

src/Options.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Zend\Stdlib;
4+
5+
abstract class Options implements ParameterObject
6+
{
7+
8+
public function __construct($config = null)
9+
{
10+
if (!is_null($config)) {
11+
if (is_array($config) || $config instanceof \Traversable) {
12+
$this->processArray($config);
13+
} else {
14+
throw new \InvalidArgumentException(
15+
'Parameter to \\Zend\\Stdlib\\Options\'s '
16+
. 'constructor must be an array or implement the '
17+
. '\\Traversable interface'
18+
);
19+
}
20+
}
21+
}
22+
23+
protected function processArray(array $config)
24+
{
25+
foreach ($config as $key => $value) {
26+
$setter = $this->assembleSetterNameFromConfigKey($key);
27+
$this->{$setter}($value);
28+
}
29+
}
30+
31+
protected function assembleSetterNameFromConfigKey($key)
32+
{
33+
$parts = explode('_', $key);
34+
$parts = array_map('ucfirst', $parts);
35+
$setter = 'set' . implode('', $parts);
36+
if (!method_exists($this, $setter)) {
37+
throw new \BadMethodCallException(
38+
'The configuration key "' . $key . '" does not '
39+
. 'have a matching ' . $setter . ' setter method '
40+
. 'which must be defined'
41+
);
42+
}
43+
return $setter;
44+
}
45+
46+
protected function assembleGetterNameFromConfigKey($key)
47+
{
48+
$parts = explode('_', $key);
49+
$parts = array_map('ucfirst', $parts);
50+
$getter = 'get' . implode('', $parts);
51+
if (!method_exists($this, $getter)) {
52+
throw new \BadMethodCallException(
53+
'The configuration key "' . $key . '" does not '
54+
. 'have a matching ' . $getter . ' getter method '
55+
. 'which must be defined'
56+
);
57+
}
58+
return $getter;
59+
}
60+
61+
public function __set($key, $value)
62+
{
63+
$setter = $this->assembleSetterNameFromConfigKey($key);
64+
$this->{$setter}($value);
65+
}
66+
67+
public function __get($key)
68+
{
69+
$getter = $this->assembleGetterNameFromConfigKey($key);
70+
return $this->{$getter}();
71+
}
72+
73+
public function __isset($key)
74+
{
75+
$getter = $this->assembleGetterNameFromConfigKey($key);
76+
return !is_null($this->{$getter}());
77+
}
78+
79+
public function __unset($key)
80+
{
81+
$setter = $this->assembleSetterNameFromConfigKey($key);
82+
try {
83+
$this->{$setter}(null);
84+
} catch(\InvalidArgumentException $e) {
85+
throw new \InvalidArgumentException(
86+
'The class property $' . $key . ' cannot be unset as'
87+
. ' NULL is an invalid value for it: ' . $e->getMessage()
88+
);
89+
}
90+
}
91+
92+
}

src/ParameterObject.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Zend\Stdlib;
4+
5+
interface ParameterObject
6+
{
7+
8+
public function __set($key, $value);
9+
10+
public function __get($key);
11+
12+
public function __isset($key);
13+
14+
public function __unset($key);
15+
16+
}

0 commit comments

Comments
 (0)