@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @param Context|null $parentContext |
| 32 | 32 | * @param array $values Array of initial value "overrides" |
| 33 | 33 | */ |
| 34 | - public function __construct ($parentContext = null, $values = []) |
|
| 34 | + public function __construct($parentContext = null, $values = []) |
|
| 35 | 35 | { |
| 36 | 36 | $this->parentContext = $parentContext; |
| 37 | 37 | $this->setDefaults(); |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * @param string $key |
| 46 | 46 | * @param mixed $value |
| 47 | 47 | */ |
| 48 | - protected function setDefault ($key, $value) |
|
| 48 | + protected function setDefault($key, $value) |
|
| 49 | 49 | { |
| 50 | 50 | $this->defaults[$key] = $value; |
| 51 | 51 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return bool |
| 59 | 59 | */ |
| 60 | - protected function isDefaultSet ($key) |
|
| 60 | + protected function isDefaultSet($key) |
|
| 61 | 61 | { |
| 62 | 62 | return isset($this->defaults[$key]); |
| 63 | 63 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * @param array $values |
| 68 | 68 | * @return static Array of initial value "overrides" |
| 69 | 69 | */ |
| 70 | - public function createSubContext ($values = []) |
|
| 70 | + public function createSubContext($values = []) |
|
| 71 | 71 | { |
| 72 | 72 | return new static($this, $values); |
| 73 | 73 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * Returns an array of parent contexts up to and including the current context, in order of ascending depth |
| 77 | 77 | * @return Context[] |
| 78 | 78 | */ |
| 79 | - public function getContextTree () |
|
| 79 | + public function getContextTree() |
|
| 80 | 80 | { |
| 81 | 81 | $parentContext = $this; |
| 82 | 82 | $parents = array($parentContext); |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | * |
| 95 | 95 | * @return mixed[] |
| 96 | 96 | */ |
| 97 | - public function getValueTree ($name, $removeDuplicates = true) |
|
| 97 | + public function getValueTree($name, $removeDuplicates = true) |
|
| 98 | 98 | { |
| 99 | 99 | $contextTree = $this->getContextTree(); |
| 100 | 100 | $values = array(); |
@@ -111,14 +111,14 @@ discard block |
||
| 111 | 111 | * @param array $values |
| 112 | 112 | * @return $this |
| 113 | 113 | */ |
| 114 | - public function set ($values = []) |
|
| 114 | + public function set($values = []) |
|
| 115 | 115 | { |
| 116 | 116 | foreach ($values as $key => $value) |
| 117 | 117 | $this->{$key} = $value; |
| 118 | 118 | return $this; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - public function __get ($name) |
|
| 121 | + public function __get($name) |
|
| 122 | 122 | { |
| 123 | 123 | // First, check if the "actual" property exists |
| 124 | 124 | if (property_exists($this, $name)) |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | return array_key_exists($name, $this->defaults) ? $this->defaults[$name] : null; |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - public function __set ($name, $value) |
|
| 137 | + public function __set($name, $value) |
|
| 138 | 138 | { |
| 139 | 139 | if (property_exists($this, $name)) |
| 140 | 140 | $this->{$name} = $value; |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | /** |
| 146 | 146 | * Loads all the default properties starting with an underscore into the defaults array |
| 147 | 147 | */ |
| 148 | - private function setDefaults () |
|
| 148 | + private function setDefaults() |
|
| 149 | 149 | { |
| 150 | 150 | $class = get_class($this); |
| 151 | 151 | do { |
@@ -80,8 +80,9 @@ discard block |
||
| 80 | 80 | { |
| 81 | 81 | $parentContext = $this; |
| 82 | 82 | $parents = array($parentContext); |
| 83 | - while (($parentContext = $parentContext->parentContext) !== null) |
|
| 84 | - array_unshift($parents, $parentContext); |
|
| 83 | + while (($parentContext = $parentContext->parentContext) !== null) { |
|
| 84 | + array_unshift($parents, $parentContext); |
|
| 85 | + } |
|
| 85 | 86 | return $parents; |
| 86 | 87 | } |
| 87 | 88 | |
@@ -100,8 +101,9 @@ discard block |
||
| 100 | 101 | $values = array(); |
| 101 | 102 | foreach ($contextTree as $context) { |
| 102 | 103 | $value = $context->{$name}; |
| 103 | - if ($value !== null && (!$removeDuplicates || (!count($values) || $values[count($values) - 1] !== $value))) |
|
| 104 | - $values[] = $value; |
|
| 104 | + if ($value !== null && (!$removeDuplicates || (!count($values) || $values[count($values) - 1] !== $value))) { |
|
| 105 | + $values[] = $value; |
|
| 106 | + } |
|
| 105 | 107 | } |
| 106 | 108 | return $values; |
| 107 | 109 | } |
@@ -113,33 +115,39 @@ discard block |
||
| 113 | 115 | */ |
| 114 | 116 | public function set ($values = []) |
| 115 | 117 | { |
| 116 | - foreach ($values as $key => $value) |
|
| 117 | - $this->{$key} = $value; |
|
| 118 | + foreach ($values as $key => $value) { |
|
| 119 | + $this->{$key} = $value; |
|
| 120 | + } |
|
| 118 | 121 | return $this; |
| 119 | 122 | } |
| 120 | 123 | |
| 121 | 124 | public function __get ($name) |
| 122 | 125 | { |
| 123 | 126 | // First, check if the "actual" property exists |
| 124 | - if (property_exists($this, $name)) |
|
| 125 | - return $this->{$name}; |
|
| 127 | + if (property_exists($this, $name)) { |
|
| 128 | + return $this->{$name}; |
|
| 129 | + } |
|
| 126 | 130 | // Try to retrieve the property from the "values" array |
| 127 | - else if (array_key_exists($name, $this->values)) |
|
| 128 | - return $this->values[$name]; |
|
| 131 | + else if (array_key_exists($name, $this->values)) { |
|
| 132 | + return $this->values[$name]; |
|
| 133 | + } |
|
| 129 | 134 | // Try to retrieve the property from the parent context |
| 130 | - else if ($this->parentContext !== null && ($parentValue = $this->parentContext->{$name}) !== null) |
|
| 131 | - return $parentValue; |
|
| 135 | + else if ($this->parentContext !== null && ($parentValue = $this->parentContext->{$name}) !== null) { |
|
| 136 | + return $parentValue; |
|
| 137 | + } |
|
| 132 | 138 | // Try to retrieve the property from the "defaults" array |
| 133 | - else |
|
| 134 | - return array_key_exists($name, $this->defaults) ? $this->defaults[$name] : null; |
|
| 139 | + else { |
|
| 140 | + return array_key_exists($name, $this->defaults) ? $this->defaults[$name] : null; |
|
| 141 | + } |
|
| 135 | 142 | } |
| 136 | 143 | |
| 137 | 144 | public function __set ($name, $value) |
| 138 | 145 | { |
| 139 | - if (property_exists($this, $name)) |
|
| 140 | - $this->{$name} = $value; |
|
| 141 | - else |
|
| 142 | - $this->values[$name] = $value; |
|
| 146 | + if (property_exists($this, $name)) { |
|
| 147 | + $this->{$name} = $value; |
|
| 148 | + } else { |
|
| 149 | + $this->values[$name] = $value; |
|
| 150 | + } |
|
| 143 | 151 | } |
| 144 | 152 | |
| 145 | 153 | /** |
@@ -151,14 +159,15 @@ discard block |
||
| 151 | 159 | do { |
| 152 | 160 | $reflectionClass = new \ReflectionClass($class); |
| 153 | 161 | $defaultProperties = $reflectionClass->getDefaultProperties(); |
| 154 | - foreach ($reflectionClass->getProperties() as $property) |
|
| 155 | - if ( |
|
| 162 | + foreach ($reflectionClass->getProperties() as $property) { |
|
| 163 | + if ( |
|
| 156 | 164 | !$property->isStatic() && |
| 157 | 165 | $property->isDefault() && |
| 158 | 166 | preg_match('/^_(.*)$/', $property->name, $matches) && |
| 159 | 167 | !$this->isDefaultSet($matches[1]) |
| 160 | 168 | ) |
| 161 | 169 | $this->setDefault($matches[1], $defaultProperties[$property->name]); |
| 170 | + } |
|
| 162 | 171 | } while ($class = get_parent_class($class)); |
| 163 | 172 | } |
| 164 | 173 | } |