Created
July 21, 2016 08:01
-
-
Save Moccine/e7f3f668867a7a92257fdc56289d4be7 to your computer and use it in GitHub Desktop.
MVC Services
This file contains hidden or 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
| <?php | |
| namespace project\Services; | |
| class Dynamizer | |
| { | |
| public $variable; | |
| public $view; | |
| public $parameters; | |
| public $helper; | |
| public $functParam; | |
| public function __construct() | |
| { | |
| } | |
| public function setHelper($helper = '') | |
| { | |
| $helperName = sprintf('project\\Services\\Helpers\\%sHelpers', trim(strtolower($helper))); | |
| $helperName = trim($helperName); | |
| $this->helper = new $helperName(); | |
| } | |
| /** | |
| * @param mixed $parameters | |
| * @return Dynamizer | |
| */ | |
| public function setParameters($parameters) | |
| { | |
| $this->parameters = $parameters; | |
| return $this; | |
| } | |
| /** | |
| * @param mixed $parameters | |
| * @return Dynamizer | |
| */ | |
| /** | |
| * @param mixed $view | |
| * @return Dynamizer | |
| */ | |
| public function setView($view) | |
| { | |
| $this->view = $view; | |
| return $this; | |
| } | |
| /** | |
| * @param mixed $variable | |
| */ | |
| public function setVariable($variable) | |
| { | |
| $this->variable = $variable; | |
| return $this; | |
| } | |
| /** | |
| * | |
| */ | |
| public function dynamise() | |
| { | |
| $this->searchVariables(); | |
| $this->replaceVariable(); | |
| return $this->view; | |
| } | |
| public function searchVariables() | |
| { | |
| preg_match_all('/{{ ([^}]+)}}/', $this->view, $variables); | |
| foreach ($variables[0] as $key => $variable) { | |
| $this->variable[$variable] = trim($variables[1][$key]); | |
| } | |
| } | |
| /** | |
| * @throws \Exception | |
| */ | |
| public function replaceVariable() | |
| { | |
| if (empty($this->variable)) { | |
| return; | |
| } | |
| foreach ($this->variable as $pattern => $key) { | |
| $isObject = preg_match('/(\.)/', $key); | |
| $isFunction = preg_match('/(.*)\((.*)\)/', $key); | |
| switch (true) { | |
| case $isFunction : | |
| preg_match_all('/(.*)\((.*)\)/', $key, $helperParam); | |
| $pattern = preg_replace(array('/\(/', '/\)/'), array('\(', '\)'), $pattern); | |
| $method = array_pop($helperParam[1]); | |
| $functionParam = array_pop($helperParam[2]); | |
| $functionParam = !$functionParam ? array() : explode(',', $functionParam); | |
| $functionParam = array_map('trim', $functionParam); | |
| $value = call_user_func_array(array($this->helper, $method), $functionParam); | |
| break; | |
| case $isObject : | |
| list($table, $property) = explode('.', $key); | |
| $this->verifyKey($table); | |
| $object = $this->parameters[$table]; | |
| $value = $object->$property; | |
| $this->controller = $this->parameters[$table]; | |
| break; | |
| default: | |
| $this->verifyKey($key); | |
| $value = $this->parameters[$key]; | |
| } | |
| $regexPattern = sprintf('/%s/', $pattern); | |
| $this->view = preg_replace($regexPattern, $value, $this->view); | |
| } | |
| } | |
| /** | |
| * @param $key | |
| * @throws \Exception | |
| */ | |
| private function verifyKey($key) | |
| { | |
| if (!array_key_exists($key, $this->parameters)) { | |
| throw new \Exception("$key NOT FOUND); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment