Last active
December 17, 2015 05:59
-
-
Save phcostabh/5561769 to your computer and use it in GitHub Desktop.
MySQL Repository class.
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 Repository | |
| { | |
| /** | |
| * Define o nome da tabela. | |
| * | |
| * @var string | |
| */ | |
| public $table = '`foo`.`musicas_sem_letra`'; | |
| /** | |
| * Receberá a instancia de uma conexão. | |
| * | |
| * @var MySQLi | |
| */ | |
| public $connection; | |
| /** | |
| * Define os campos que são obrigatórios. | |
| * | |
| * @var array | |
| */ | |
| public static $required = array( | |
| 'artista', | |
| 'nome', | |
| 'arquivo' | |
| ); | |
| /** | |
| * Define o contrutor que recebe a instancia de uma conexão com o banco de | |
| * dados. | |
| * | |
| * @param MySQLi | |
| */ | |
| public function __construct(MySQLi $connection){ | |
| $this->connection = $connection; | |
| } | |
| /** | |
| * Extrai o nome das colunas do array de atributos. | |
| * | |
| * @param array | |
| * @return string | |
| */ | |
| final public function columns($attributes) { | |
| return implode(', ', array_keys($attributes)); | |
| } | |
| /** | |
| * Extrai os valores do array de atributos. | |
| * | |
| * @param array | |
| * @return string | |
| */ | |
| final public function values($attributes) { | |
| return $this->parameters(array_values($attributes)); | |
| } | |
| /** | |
| * Cria os parametros da query. | |
| * | |
| * @param array | |
| * @return string | |
| */ | |
| final public function parameters($values) { | |
| return implode(', ', array_map( array($this, 'parameter'), $values)); | |
| } | |
| /** | |
| * Garante que os parametros passem por uma verificação de segurança para | |
| * previnir injection. | |
| * | |
| * @param array | |
| * @return string | |
| */ | |
| final public function parameter($value) { | |
| $parameter = $this->connection->real_escape_string($value); | |
| $wrap_with_quotes = (gettype($value) === 'string') && (!preg_match('/\((.*)?\)/', $value)); | |
| return $wrap_with_quotes ? "'$parameter'" : $parameter; | |
| } | |
| /** | |
| * Gera a query e insere os attributos no banco de dados. | |
| * | |
| * @param array | |
| * @return string | |
| */ | |
| public function insert($attributes){ | |
| if ( ! $this->valid($attributes) ) { | |
| return "Invalid data." . PHP_EOL; | |
| } | |
| $query = "INSERT INTO {$this->table} ({$this->columns($attributes)}) VALUES ({$this->values($attributes)});"; | |
| return ($this->connection->query($query)) ? $query : 'Something went wrong.'; | |
| } | |
| /** | |
| * Verifica se os parametros passados são válidos de acordo com os campos | |
| * obrigatórios. | |
| * | |
| * @param array | |
| * @return bool | |
| */ | |
| final public function valid($attributes){ | |
| foreach (static::$required as $attr) { | |
| if (!isset($attributes[$attr]) || empty($attributes[$attr])) return false; | |
| } | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment