PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/verysimple/Phreeze/Reporter.php

http://github.com/jasonhinkle/phreeze
PHP | 329 lines | 161 code | 46 blank | 122 comment | 20 complexity | 8207424c33e1fe4ea681bfc803d247b5 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. /** @package verysimple::Phreeze */
  3. /**
  4. * Reporter allows creating dynamic objects that do not necessarily reflect
  5. * the structure of the datastore table. This is often useful for reporting
  6. * or returning aggregate data
  7. * @package verysimple::Phreeze
  8. * @author VerySimple Inc. <noreply@verysimple.com>
  9. * @copyright 1997-2005 VerySimple Inc.
  10. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  11. * @version 1.0
  12. */
  13. abstract class Reporter implements Serializable
  14. {
  15. protected $_phreezer;
  16. private $_isLoaded;
  17. private $_isPartiallyLoaded;
  18. private $_cacheLevel = 0;
  19. private $_noCache = false;
  20. /** @var these properties will never be cached */
  21. private static $NoCacheProperties = array("_cache","_phreezer","_val_errors","_base_validation_complete");
  22. /** @var cache of public properties for each type for improved performance when enumerating */
  23. private static $PublicPropCache = array();
  24. /**
  25. * Returns true if the current object has been loaded
  26. * @access public
  27. * @param bool (optional) if provided will change the value
  28. * @return bool
  29. */
  30. public function IsLoaded($value = null)
  31. {
  32. if ($value != null) $this->_isLoaded = $value;
  33. return $this->_isLoaded;
  34. }
  35. /**
  36. * Returns true if the current object has been partially loaded
  37. * @access public
  38. * @param bool (optional) if provided will change the value
  39. * @return bool
  40. */
  41. public function IsPartiallyLoaded($value = null)
  42. {
  43. if ($value != null) $this->_isPartiallyLoaded = $value;
  44. return $this->_isPartiallyLoaded;
  45. }
  46. /**
  47. * Returns 0 if this was loaded from the DB, 1 if from 1st level cache and 2 if 2nd level cache
  48. * @access public
  49. * @param bool (optional) if provided will change the value
  50. * @return bool
  51. */
  52. public function CacheLevel($value = null)
  53. {
  54. if ($value != null) $this->_cacheLevel = $value;
  55. return $this->_cacheLevel;
  56. }
  57. /**
  58. * Returns true if the current object should never be cached
  59. * @access public
  60. * @param bool (optional) if provided will change the value
  61. * @return bool
  62. */
  63. public function NoCache($value = null)
  64. {
  65. if ($value != null) $this->_noCache = $value;
  66. return $this->_noCache;
  67. }
  68. /**
  69. * constructor
  70. *
  71. * @access public
  72. * @param Phreezer $phreezer
  73. * @param Array $row
  74. */
  75. final function __construct(&$phreezer, $row = null)
  76. {
  77. $this->_phreezer = $phreezer;
  78. if ($row)
  79. {
  80. $this->Load($row);
  81. }
  82. }
  83. /**
  84. * When serializing, make sure that we ommit certain properties that
  85. * should never be cached or serialized.
  86. */
  87. function serialize()
  88. {
  89. $propvals = array();
  90. $ro = new ReflectionObject($this);
  91. foreach ($ro->getProperties() as $rp )
  92. {
  93. $propname = $rp->getName();
  94. if (!in_array($propname,self::$NoCacheProperties))
  95. {
  96. if (method_exists($rp,"setAccessible"))
  97. {
  98. $rp->setAccessible(true);
  99. $propvals[$propname] = $rp->getValue($this);
  100. }
  101. elseif (!$rp->isPrivate())
  102. {
  103. // if < php 5.3 we can't serialize private vars
  104. $propvals[$propname] = $rp->getValue($this);
  105. }
  106. }
  107. }
  108. return serialize($propvals);
  109. }
  110. /**
  111. * Reload the object when it awakes from serialization
  112. * @param $data
  113. */
  114. function unserialize($data)
  115. {
  116. $propvals = unserialize($data);
  117. $ro = new ReflectionObject($this);
  118. foreach ($ro->getProperties() as $rp )
  119. {
  120. $propname = $rp->name;
  121. if ( array_key_exists($propname,$propvals) )
  122. {
  123. if (method_exists($rp,"setAccessible"))
  124. {
  125. $rp->setAccessible(true);
  126. $rp->setValue($this,$propvals[$propname]);
  127. }
  128. elseif (!$rp->isPrivate())
  129. {
  130. // if < php 5.3 we can't serialize private vars
  131. $rp->setValue($this,$propvals[$propname]);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Returns an array with all public properties, excluding any internal
  138. * properties used by the Phreeze framework. This is cached for performance
  139. * when enumerating through large numbers of the same class
  140. * @return array
  141. */
  142. public function GetPublicProperties()
  143. {
  144. $className = get_class($this);
  145. if (!array_key_exists($className, self::$PublicPropCache))
  146. {
  147. $props = array();
  148. $ro = new ReflectionObject($this);
  149. foreach ($ro->getProperties() as $rp )
  150. {
  151. $propname = $rp->getName();
  152. if (!in_array($propname,self::$NoCacheProperties))
  153. {
  154. if (!($rp->isPrivate() || $rp->isStatic()))
  155. {
  156. $props[] = $propname;
  157. }
  158. }
  159. }
  160. self::$PublicPropCache[$className] = $props;
  161. }
  162. return self::$PublicPropCache[$className];
  163. }
  164. /**
  165. * Return an object with a limited number of properties from this Phreezable object.
  166. * This can be used if not all properties are necessary, for example rendering as JSON
  167. *
  168. * This can be overriden per class for custom JSON output. the overridden method may accept
  169. * additional option parameters that are not supported by the base Phreezable calss
  170. *
  171. * @param array assoc array of options. This is passed through from Controller->RenderJSON
  172. * props (array) array of props to return (if null then use all public props)
  173. * omit (array) array of props to omit
  174. * camelCase (bool) if true then first letter of each property is made lowercase
  175. * @return stdClass
  176. */
  177. function ToObject($options = null)
  178. {
  179. if ($options === null) $options = array();
  180. $props = array_key_exists('props', $options) ? $options['props'] : $this->GetPublicProperties();
  181. $omit = array_key_exists('omit', $options) ? $options['omit'] : array();
  182. $camelCase = array_key_exists('camelCase', $options) ? $options['camelCase'] : false;
  183. $obj = new stdClass();
  184. foreach ($props as $prop)
  185. {
  186. if (!in_array($prop, $omit))
  187. {
  188. $newProp = ($camelCase) ? lcfirst($prop) : $prop;
  189. $obj->$newProp = $this->$prop;
  190. }
  191. }
  192. return $obj;
  193. }
  194. /**
  195. * Restores the object's connection to the datastore, for example after serialization
  196. * @param $phreezer
  197. * @param $row
  198. */
  199. function Refresh(Phreezer $phreezer, $row = null)
  200. {
  201. $this->_phreezer = $phreezer;
  202. if ($row)
  203. {
  204. $this->Load($row);
  205. }
  206. $this->OnRefresh();
  207. }
  208. /**
  209. * Called after object is refreshed, may be overridden
  210. *
  211. * @access public
  212. */
  213. public function OnRefresh(){}
  214. /**
  215. * This static function can be overridden to populate this object with
  216. * results of a custom query
  217. *
  218. * @access public
  219. * @param Criteria $criteria
  220. * @return string
  221. */
  222. static function GetCustomQuery($criteria)
  223. {
  224. return "";
  225. }
  226. /**
  227. * This may be overridden to return SQL used for counting the number of rows
  228. * in a result. This method is not required, however it will allow
  229. * Phreeze to use an efficient query for counting results. This query
  230. * must return the correct number of results that GetCustomQuery would,
  231. * given the same criteria
  232. *
  233. * The resultant SQL must return only one row with one column named 'counter'
  234. *
  235. * @access public
  236. * @param Criteria $criteria
  237. * @return string
  238. */
  239. static function GetCustomCountQuery($criteria)
  240. {
  241. return "";
  242. }
  243. /**
  244. * Returns this object as an associative array with properties as keys and
  245. * values as values
  246. *
  247. * @access public
  248. * @return array
  249. */
  250. function GetArray()
  251. {
  252. $fms = $this->_phreezer->GetFieldMaps(get_class($this));
  253. $cols = Array();
  254. foreach ($fms as $fm)
  255. {
  256. $prop = $fm->PropertyName;
  257. $cols[$fm->ColumnName] = $this->$prop;
  258. }
  259. return $cols;
  260. }
  261. /**
  262. * Loads the object with data given in the row array.
  263. *
  264. * @access public
  265. * @param Array $row
  266. */
  267. function Load(&$row)
  268. {
  269. $this->_phreezer->Observe("Loading " . get_class($this),OBSERVE_DEBUG);
  270. foreach (array_keys($row) as $prop)
  271. {
  272. $this->$prop = $row[$prop];
  273. }
  274. $this->OnLoad();
  275. }
  276. /**
  277. * Called after object is loaded, may be overridden
  278. *
  279. * @access protected
  280. */
  281. protected function OnLoad(){}
  282. }
  283. ?>