Skip to content

Instantly share code, notes, and snippets.

@feelinc
Last active August 29, 2015 13:57

Revisions

  1. feelinc revised this gist Mar 7, 2014. 1 changed file with 2 additions and 13 deletions.
    15 changes: 2 additions & 13 deletions Template.php
    Original file line number Diff line number Diff line change
    @@ -31,8 +31,6 @@ public function __construct(Model $model)
    $this->model = $model;

    $this->prepareData();

    unset($this->model);
    }

    /**
    @@ -72,11 +70,10 @@ public function getAttribute($key)
    * 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)
    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');
    }

    if (is_numeric($value)) {
    $value = (float) $value;
    }

    if (is_null($newKey)) {
    $this->$key = $value;
    } else {
    $this->$newKey = $value;
    }
    $this->$key = $value;

    unset($value);
    }
  2. feelinc revised this gist Mar 7, 2014. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions Template.php
    Original 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)
    and $key != 'id'
    and substr($value, 0, 1) != '+'
    and $key != 'chmod_folder') {
    if (is_numeric($value)) {
    $value = (float) $value;
    }

  3. feelinc created this gist Mar 7, 2014.
    3 changes: 3 additions & 0 deletions Controller.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    <?php

    return Response::json(new SettingTemplate($data));
    19 changes: 19 additions & 0 deletions SettingTemplate.php
    Original 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');
    }

    }
    116 changes: 116 additions & 0 deletions Template.php
    Original 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);
    }

    }