Created
June 6, 2014 12:54
-
-
Save RodolfoSilva/66c7ded1fda8caad27cf to your computer and use it in GitHub Desktop.
Cria uma class para geração dinâmica dos métodos
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 | |
class Model | |
{ | |
/* armazena os dados retornado do banco de dados */ | |
private $data; | |
/* construtor */ | |
public function __construct($data) | |
{ | |
$this->data = $data; | |
} | |
/* dynamic function server */ | |
public function __call($method, $argumentos = null) | |
{ | |
// Recupera o prefixo do método com tamanho maximo de 3 caracteres | |
$metodo = strtolower(substr($method, 0, 3)); | |
if ($metodo === 'get' || $metodo === 'set') { | |
$str = substr($method, 3); | |
/* uncamelcaser: http://www.paulferrett.com/2009/php-camel-case-functions/ */ | |
$str[0] = strtolower($str[0]); | |
$func = create_function('$c', 'return "_" . strtolower($c[1]);'); | |
$campo = preg_replace_callback('/([A-Z])/', $func, $str); | |
/* Verifica se o campo requerido existe */ | |
if (array_key_exists($campo, $this->data)) { | |
if ($metodo === 'get') { | |
return $this->data[$campo]; | |
} elseif (isset($argumentos[0])) { | |
$this->data[$campo] = $argumentos[0]; | |
return $this; | |
} | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment