/application/Framework/Abstracts/BaseObject.php

https://github.com/xmeltrut/SocietasPro · PHP · 185 lines · 84 code · 27 blank · 74 comment · 24 complexity · 94e0f53f0f410e27ca5126779bd0173e MD5 · raw file

  1. <?php
  2. /**
  3. * Base object, because coding getters and setters for every single object
  4. * is far too tedious. Instead, we can use generic ones and deal with the
  5. * fall out later.
  6. *
  7. * @author Chris Worfolk <chris@societaspro.org>
  8. * @package SocietasPro
  9. * @subpackage Core
  10. */
  11. namespace Framework\Abstracts;
  12. abstract class BaseObject {
  13. /**
  14. * Array to hold generic data for the object. We also need to hold
  15. * the original data so we can compare in the audit logs.
  16. */
  17. private $data;
  18. private $originalData;
  19. /**
  20. * Variable to hold a return message.
  21. */
  22. private $message;
  23. /**
  24. * Constructor.
  25. *
  26. * @param array $data Array of data to populate the object with
  27. */
  28. function __construct ($data) {
  29. $this->data = $this->originalData = $data;
  30. }
  31. /**
  32. * Magic getter
  33. *
  34. * @param string $key Key
  35. * @return mixed
  36. */
  37. public function __get ($key) {
  38. if (array_key_exists($key, $this->data)) {
  39. return $this->data[$key];
  40. } else {
  41. return false;
  42. }
  43. }
  44. /**
  45. * We need a toString method so we can record it in the audit trail
  46. *
  47. * @return string Data in object
  48. */
  49. public function __toString () {
  50. return json_encode($this->data);
  51. }
  52. /**
  53. * Get all data (used for save functions)
  54. */
  55. public function getAllData () {
  56. return $this->data;
  57. }
  58. /**
  59. * Standard get return message function
  60. *
  61. * @return string|array Message
  62. */
  63. public function getMessage () {
  64. return $this->message;
  65. }
  66. /**
  67. * Check if a data element has been updated
  68. *
  69. * This will return false if the element doesn't exist, except if it exsits in one instance and
  70. * not the other, in which case it will return true. It does not use strict type checking.
  71. *
  72. * @param string $key Key
  73. * @return boolean
  74. */
  75. public function hasChanged ($key = false) {
  76. if ($key) {
  77. // run both checks
  78. $valueA = array_key_exists($key, $this->data);
  79. $valueB = array_key_exists($key, $this->originalData);
  80. if ($valueA || $valueB) {
  81. if ($valueA && $valueB) {
  82. if ($this->data[$key] == $this->originalData[$key]) {
  83. return false;
  84. } else {
  85. return true;
  86. }
  87. } else {
  88. return true;
  89. }
  90. } else {
  91. return false;
  92. }
  93. } else {
  94. $arr = array_diff_assoc($this->data, $this->originalData);
  95. if (count($arr) > 0) {
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. }
  102. /**
  103. * Returns a string representation of the data as it was when the object
  104. * was originally created - perfect for audit trails.
  105. *
  106. * @return string Data
  107. */
  108. public function original () {
  109. if (is_array($this->originalData)) {
  110. if (count($this->originalData) > 0) {
  111. return json_encode($this->originalData);
  112. }
  113. }
  114. return "";
  115. }
  116. /**
  117. * Set a data element. Only available to child objects.
  118. *
  119. * @param string $key Key
  120. * @param string $value Value
  121. */
  122. protected function setData ($key, $value) {
  123. $this->data[$key] = $value;
  124. }
  125. /**
  126. * Set the return message
  127. *
  128. * @param string $msg Message
  129. * @return boolean Success
  130. */
  131. protected function setMessage ($msg) {
  132. if ($msg != "") {
  133. if (is_array($this->message)) {
  134. $this->message[] = $msg;
  135. } elseif ($this->message != "") {
  136. $this->message = array($this->message, $msg);
  137. } else {
  138. $this->message = $msg;
  139. }
  140. return true;
  141. }
  142. return false;
  143. }
  144. /**
  145. * Unset an element from the data array
  146. *
  147. * @param string $key Key to unset
  148. * @return boolean Success
  149. */
  150. protected function unsetData ($key) {
  151. if (array_key_exists($key, $this->data)) {
  152. unset($this->data[$key]);
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. }