-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Template: magic access via __get/__set replaced with object properties
- Loading branch information
Showing
1 changed file
with
6 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,20 +17,16 @@ | |
*/ | ||
class Template extends LatteTemplate | ||
{ | ||
/** @var array */ | ||
private $params = []; | ||
|
||
|
||
/** | ||
* Adds new template parameter. | ||
* @return static | ||
*/ | ||
public function add(string $name, $value) | ||
{ | ||
if (array_key_exists($name, $this->params)) { | ||
if (property_exists($this, $name)) { | ||
throw new Nette\InvalidStateException("The variable '$name' already exists."); | ||
} | ||
$this->params[$name] = $value; | ||
$this->$name = $value; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mabar
Contributor
|
||
return $this; | ||
} | ||
|
||
|
@@ -41,7 +37,9 @@ public function add(string $name, $value) | |
*/ | ||
public function setParameters(array $params) | ||
{ | ||
$this->params = $params + $this->params; | ||
foreach ($params as $k => $v) { | ||
$this->$k = $v; | ||
} | ||
return $this; | ||
} | ||
|
||
|
@@ -51,47 +49,6 @@ public function setParameters(array $params) | |
*/ | ||
public function getParameters(): array | ||
{ | ||
return $this->params; | ||
} | ||
|
||
|
||
/** | ||
* Sets a template parameter. Do not call directly. | ||
*/ | ||
public function __set(string $name, $value): void | ||
{ | ||
$this->params[$name] = $value; | ||
} | ||
|
||
|
||
/** | ||
* Returns a template parameter. Do not call directly. | ||
* @return mixed value | ||
*/ | ||
public function &__get(string $name) | ||
{ | ||
if (!array_key_exists($name, $this->params)) { | ||
trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE); | ||
} | ||
|
||
return $this->params[$name]; | ||
} | ||
|
||
|
||
/** | ||
* Determines whether parameter is defined. Do not call directly. | ||
*/ | ||
public function __isset(string $name): bool | ||
{ | ||
return isset($this->params[$name]); | ||
} | ||
|
||
|
||
/** | ||
* Removes a template parameter. Do not call directly. | ||
*/ | ||
public function __unset(string $name): void | ||
{ | ||
unset($this->params[$name]); | ||
return get_object_vars($this); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
} |
Template is for magic only, right? property_exists() check disallows define property, only magic properties can be set.