/lighter/models/Model.php
https://github.com/mbegoc/lighter · PHP · 166 lines · 50 code · 27 blank · 89 comment · 6 complexity · 406faa7977c23fbbc444534d5b31d85c MD5 · raw file
- <?php
- namespace lighter\models;
- abstract class Model {
- /**
- * list of the models related to this one. Almost everytime results of a sql join.
- * @var array
- */
- protected $linkedModels = array();
- /**
- * the data of this model
- * @var array
- */
- protected $data = array();
- /**
- * list of errors generated by the validation
- * @var array
- */
- private $errors = array();
- /**
- * the filter to apply to clean data before returning it in getValues
- * @var array
- */
- private $filter = array();
- /**
- * set values of this model
- *
- * @param array $values
- * @param string $prefix
- */
- public function setValues(array $values) {
- $this->data = $values;
- }
- /**
- * return an array of the values contained within this model
- *
- * @param boolean $clean - default true
- * if true, only actual data is returned. If false, meta data, if it exists,
- * will also be returned. (id should be considered as meta data).
- * @return array
- */
- public function getValues($clean = true) {
- if ($clean) {
- return array_diff_key($this->data, $this->filter);
- } else {
- return $this->data;
- }
- }
- /**
- * return the errors generated by the validation process.
- *
- * @return array
- */
- public function getErrors()
- {
- return $this->errors;
- }
- /**
- * add an error
- *
- * @param string $message
- * @param string $field
- */
- protected function addError($message, $field) {
- $this->errors[$field] = $message;
- }
- /**
- * intialize the filter to use to clean data into getValues
- *
- * @param array $filter
- * the list of field to filter
- */
- protected function setFilter(array $filter) {
- $this->filter = array_flip($filter);
- }
- /**
- * same as setFilter but old values are kept
- *
- * @param array $filter
- */
- protected function addFilter(array $filter) {
- $this->filter = array_merge($this->filter, array_flip($filter));
- }
- /**
- * return the unique id of this model
- *
- * @return string | int
- */
- abstract public function getId();
- /**
- * validate data before storing it
- *
- * @return boolean
- */
- abstract public function validate();
- /**
- * a method to be called just before inserting the data in the DB.
- * Here you can check data validity, remove, add or change information
- *
- * @abstract
- * @param string $class
- */
- abstract public function prepareToStore();
- /**
- * say wether this object already exists in database or not
- *
- * @return boolean
- */
- abstract public function exists();
- /**
- * link another model to this one. Though this link is informal and can be used to a
- * variety of goals, it's designed with the sql join representation in mind.
- * Each model is added to a list of models depending on the linkid that identified a
- * kind of relation.
- *
- * @param Model $model
- * @param string $linkId
- */
- public function link(Model $model, $linkId)
- {
- if (isset($this->linkedModels[$linkId])) {
- $this->linkedModels[$linkId][] = $model;
- } else {
- $this->linkedModels[$linkId]= array($model);
- }
- }
- /**
- * return the list of model matching the linkid
- *
- * @param string $linkId
- * @return array
- */
- public function getLinkedModels($linkId = null)
- {
- if ($linkId == null) {
- $linkId = key($this->linkedModels);
- }
- return $this->linkedModels[$linkId];
- }
- }