/lighter/models/Model.php

https://github.com/mbegoc/lighter · PHP · 166 lines · 50 code · 27 blank · 89 comment · 6 complexity · 406faa7977c23fbbc444534d5b31d85c MD5 · raw file

  1. <?php
  2. namespace lighter\models;
  3. abstract class Model {
  4. /**
  5. * list of the models related to this one. Almost everytime results of a sql join.
  6. * @var array
  7. */
  8. protected $linkedModels = array();
  9. /**
  10. * the data of this model
  11. * @var array
  12. */
  13. protected $data = array();
  14. /**
  15. * list of errors generated by the validation
  16. * @var array
  17. */
  18. private $errors = array();
  19. /**
  20. * the filter to apply to clean data before returning it in getValues
  21. * @var array
  22. */
  23. private $filter = array();
  24. /**
  25. * set values of this model
  26. *
  27. * @param array $values
  28. * @param string $prefix
  29. */
  30. public function setValues(array $values) {
  31. $this->data = $values;
  32. }
  33. /**
  34. * return an array of the values contained within this model
  35. *
  36. * @param boolean $clean - default true
  37. * if true, only actual data is returned. If false, meta data, if it exists,
  38. * will also be returned. (id should be considered as meta data).
  39. * @return array
  40. */
  41. public function getValues($clean = true) {
  42. if ($clean) {
  43. return array_diff_key($this->data, $this->filter);
  44. } else {
  45. return $this->data;
  46. }
  47. }
  48. /**
  49. * return the errors generated by the validation process.
  50. *
  51. * @return array
  52. */
  53. public function getErrors()
  54. {
  55. return $this->errors;
  56. }
  57. /**
  58. * add an error
  59. *
  60. * @param string $message
  61. * @param string $field
  62. */
  63. protected function addError($message, $field) {
  64. $this->errors[$field] = $message;
  65. }
  66. /**
  67. * intialize the filter to use to clean data into getValues
  68. *
  69. * @param array $filter
  70. * the list of field to filter
  71. */
  72. protected function setFilter(array $filter) {
  73. $this->filter = array_flip($filter);
  74. }
  75. /**
  76. * same as setFilter but old values are kept
  77. *
  78. * @param array $filter
  79. */
  80. protected function addFilter(array $filter) {
  81. $this->filter = array_merge($this->filter, array_flip($filter));
  82. }
  83. /**
  84. * return the unique id of this model
  85. *
  86. * @return string | int
  87. */
  88. abstract public function getId();
  89. /**
  90. * validate data before storing it
  91. *
  92. * @return boolean
  93. */
  94. abstract public function validate();
  95. /**
  96. * a method to be called just before inserting the data in the DB.
  97. * Here you can check data validity, remove, add or change information
  98. *
  99. * @abstract
  100. * @param string $class
  101. */
  102. abstract public function prepareToStore();
  103. /**
  104. * say wether this object already exists in database or not
  105. *
  106. * @return boolean
  107. */
  108. abstract public function exists();
  109. /**
  110. * link another model to this one. Though this link is informal and can be used to a
  111. * variety of goals, it's designed with the sql join representation in mind.
  112. * Each model is added to a list of models depending on the linkid that identified a
  113. * kind of relation.
  114. *
  115. * @param Model $model
  116. * @param string $linkId
  117. */
  118. public function link(Model $model, $linkId)
  119. {
  120. if (isset($this->linkedModels[$linkId])) {
  121. $this->linkedModels[$linkId][] = $model;
  122. } else {
  123. $this->linkedModels[$linkId]= array($model);
  124. }
  125. }
  126. /**
  127. * return the list of model matching the linkid
  128. *
  129. * @param string $linkId
  130. * @return array
  131. */
  132. public function getLinkedModels($linkId = null)
  133. {
  134. if ($linkId == null) {
  135. $linkId = key($this->linkedModels);
  136. }
  137. return $this->linkedModels[$linkId];
  138. }
  139. }