File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Kms/Core/Entities/KmsFrontEntity.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma Mediadesign
*/
namespace KommaApp\Kms\Core\Entities;
use Illuminate\Database\Eloquent\Model;
use Validator;
abstract class KmsFrontEntity extends Model{
protected static $entityAttributesData = [];
protected $entityAttributes;
public function __construct(array $attributes = array())
{
$this->parseEntityAttributesData();
parent::__construct($attributes);
}
/**
* Get the title for the KmsEntity
* @return string
*/
abstract public function getEntityTitle();
/**
* Parse the attribute values on fill ing the KmsEntity
* @param array $attributes
* @return $this
*/
public function fill(array $attributes)
{
parent::fill($attributes);
$this->parseEntityAttributesData();
return $this;
}
public function save(array $options = array())
{
$this->transformEntityAttributesValues();
return parent::save($options);
}
/**
* Create a list with KmsAttribute Objects, based on self::$entityAttributes
* @return array
*/
public function parseEntityAttributesData()
{
$errorMessages = $this->validateAttributes()->messages();
$entityAttributes = [];
foreach($this::$entityAttributesData as $attributeKey => $attribute)
{
$this->fillable[] = $attributeKey; // Make the fields in self::$entityAttributes mass assignable
$value = $this[$attributeKey]; // Get the values for the fields
$options = isset($attribute['options']) ? $attribute['options'] : []; // Get the options for the fields
$errors = $this->isDirty() ? $errorMessages->get($attributeKey) : []; // Get the error messages for the fields if $this is filled
$entityAttributes[] = new $attribute['type']($attributeKey, $value, $options, $errors);
}
return $this->entityAttributes = $entityAttributes;
}
/**
* Transform the values in the attributes for saving (eg. Hashing)
*/
public function transformEntityAttributesValues()
{
foreach($this->entityAttributes as $attribute)
{
if(isset($this[$attribute->key]))
{
// If the value returned by getValue == null: get the original value
if($value = $attribute->getValue() != null)
{
// Use value in the $attribute
$this[$attribute->key] = $attribute->getValue();
}else{
// Use original value
$this[$attribute->key] = $this->getOriginal($attribute->key);
}
}
}
}
/**
* Getter for $this->entityAttributes
* @return mixed
*/
public function getEntityAttributes()
{
return $this->entityAttributes;
}
public function validateAttributes()
{
$values = $rules = $messages = [];
foreach($this::$entityAttributesData as $attributeKey => $attribute)
{
if(isset($attribute['options']['validation']))
{
$values[$attributeKey] = $this[$attributeKey];
$rules[$attributeKey] = $this->parseRules($attribute['options']['validation']['rules']);
foreach ($attribute['options']['validation']['messages'] as $messageKey => $message)
{
$messages[$attributeKey . '.' . $messageKey] = $message;
}
}
}
return Validator::make($values, $rules, $messages);
}
protected function parseRules($rules)
{
$rules = str_replace('[,id]', ','.$this->id, $rules);
$rules = str_replace('[id]', $this->id, $rules);
return $rules;
}
}