Last active
August 29, 2015 13:57
Revisions
-
feelinc revised this gist
Mar 7, 2014 . 1 changed file with 2 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -31,8 +31,6 @@ public function __construct(Model $model) $this->model = $model; $this->prepareData(); } /** @@ -72,11 +70,10 @@ public function getAttribute($key) * Set a given attribute on the template from model. * * @param string $key * @param mixed $value * @return void */ public function setAttribute($key, $value = null) { if (is_null($value)) { $value = $this->model->$key; @@ -86,15 +83,7 @@ public function setAttribute($key, $newKey = null, $value = null) $value = $value->format('c'); } $this->$key = $value; unset($value); } -
feelinc revised this gist
Mar 7, 2014 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -86,10 +86,7 @@ public function setAttribute($key, $newKey = null, $value = null) $value = $value->format('c'); } if (is_numeric($value)) { $value = (float) $value; } -
feelinc created this gist
Mar 7, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ <?php return Response::json(new SettingTemplate($data)); 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ <?php class SettingTemplate extends Template { /** * Prepare the data to retreive. * * @return void */ public function prepareData() { $this->setAttribute('key'); $this->setAttribute('value'); $this->setAttribute('created_at'); $this->setAttribute('updated_at'); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,116 @@ <?php use Illuminate\Database\Eloquent\Model; use DateTime; abstract class Template { /** * The model. * * @var Model */ protected $model; /** * The attributes. * * @var array */ protected $attributes; /** * Create a new instance. * * @param Model $model * @return void */ public function __construct(Model $model) { $this->model = $model; $this->prepareData(); unset($this->model); } /** * Return the model. * * @return Model */ public function getModel() { return $this->model; } /** * Prepare the data to retreive. * * @return void */ public function prepareData() {} /** * Get an attribute from the template. * * @param string $key * @return mixed */ public function getAttribute($key) { if (isset($this->$key)) { return $this->$key; } return ''; } /** * Set a given attribute on the template from model. * * @param string $key * @param string $newKey * @param mixed $value * @return void */ public function setAttribute($key, $newKey = null, $value = null) { if (is_null($value)) { $value = $this->model->$key; } if ($value instanceof DateTime) { $value = $value->format('c'); } if (is_numeric($value) and $key != 'id' and substr($value, 0, 1) != '+' and $key != 'chmod_folder') { $value = (float) $value; } if (is_null($newKey)) { $this->$key = $value; } else { $this->$newKey = $value; } unset($value); } /** * Dynamically retrieve attributes on the template. * * @param string $key * @return mixed */ public function __get($key) { return $this->getAttribute($key); } }