Skip to content

Instantly share code, notes, and snippets.

@angelmartz
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save angelmartz/cfa41a2730a634a659ca to your computer and use it in GitHub Desktop.

Select an option

Save angelmartz/cfa41a2730a634a659ca to your computer and use it in GitHub Desktop.
<?php
class Elegant extends Eloquent
{
public $blacklist = ['id','created_at','updated_at'];
public $errors = [];
protected $rules = [];
protected $v = null;
public function validate($data = null,$save = false,$safety = true) {
$data = is_null($data) ? Input::all() : $data;
$this->v = Validator::make($data, $this->rules);
$failed = $this->v->fails() ? true : false;
if ($failed) {
$this->errors = $this->v->errors();
}
if (!$failed && $save) {
$this->saveWithInput($data,$safety);
}
return $failed;
}
public function saveWithInput($data = null,$safety = true) {
$data = is_null($data) ? Input::except('_token') : $data;
$fields = $safety ? $this->getDbColumns() : $this->rules;
$data = array_only(array_keys($fields),$data);
$data = is_array($data) ? $data : [$data];
foreach($data as $key => $value) $this->$key = $value;
return $this->save();
}
private function getDbColumns() {
$fields = DB::select("SHOW COLUMNS FROM ".$this->table);
$returns = [];
foreach ($fields as $field) {
if(!in_array($field->Field,$this->blacklist)){
$returns[] = $field->Field;
}
}
return $returns;
}
public function reset() {
$this->blacklist = ['id','created_at','updated_at'];
$this->errors = [];
}
public function lazy(){
return $this->validate(Input::all(),true);
}
}
if (false) {
$program = new Program;
/*
* Zero way to do it, Love it, haha
*/
$program->lazy();
/*
* First way to do it, handle it yourself (for the most part)
*/
if ($program->validate($data)) {
$program->title = $data['title'];
$program->content = $data['content'];
$program->header = $data['header'];
$program->save();
}
/*
* Second way to do it, Check all input and filter against the columns in the database,
* (we will exclude id, created_at and updated_at columns)
* This will save all input that comes through and the key is a column in the database
* (Won't miss anything)
*/
if ($program->validate($data)) {
$program->saveWithInput($data);
}
/*
* Third way to do it, Don't use the columns from the DB to check against,
* Just use the rules defined in the $rules column of the model
* This will not capture $program->header like the others above
* Maybe you didn't want to update a particular field.
*/
if ($program->validate($data)) {
$program->saveWithInput($data,false);
}
/*
* Fourth way to do it, Just like step #2, (checks database for valid fields and saves if valid)
*/
if ($program->validate($data,true)) {
return 'Program has been validated and saved';
}
/*
* Fifth way to do it, Just like step #3, validate against the rules in your model only.
*/
if ($program->validate($data,true,false)) {
return 'Program has been validated and saved';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment