/protected/components/ezcomponents/PersistentObject/src/object/persistent_object_property.php

https://github.com/kamarulismail/kamarul-playground · PHP · 261 lines · 133 code · 13 blank · 115 comment · 15 complexity · 1c0513d01d89472c3c68def0cd612eab MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcPersistentObjectProperty class.
  4. *
  5. * @version 1.7.1
  6. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  7. * @license http://ez.no/licenses/new_bsd New BSD License
  8. * @package PersistentObject
  9. */
  10. /**
  11. * Defines a persistent object field.
  12. *
  13. * An instance of this class is used in a {@link ezcPersisentObjectDefinition}
  14. * object to define a relation between an object property and a database
  15. * column.
  16. *
  17. * @see ezcPersisentObjectDefinition
  18. *
  19. * @property string $columnName
  20. * The name of the database field that stores the value.
  21. * @property string $propertyName
  22. * The name of the PersistentObject property that holds the value in
  23. * the PHP object.
  24. * @property int $propertyType
  25. * The type of the PHP property. See class constants PHP_TYPE_*.
  26. * @property ezcPersistentPropertyConverter|null $converter
  27. * A converter object that will automatically perform converters on
  28. * load and save of a property value.
  29. * @property int $databaseType
  30. * Type of the database column, as defined by PDO constants: {@link
  31. * PDO::PARAM_BOOL}, {@link PDO::PARAM_INT}, {@link PDO::PARAM_STR}
  32. * (default as defined by {@link ezcQuery::bindValue()}) or {@link
  33. * PDO::PARAM_LOB} (important for binary data).
  34. *
  35. * @package PersistentObject
  36. * @version 1.7.1
  37. */
  38. class ezcPersistentObjectProperty
  39. {
  40. const PHP_TYPE_STRING = 1;
  41. const PHP_TYPE_INT = 2;
  42. const PHP_TYPE_FLOAT = 3;
  43. const PHP_TYPE_ARRAY = 4;
  44. const PHP_TYPE_OBJECT = 5;
  45. const PHP_TYPE_BOOL = 6;
  46. /**
  47. * Holds the properties for this class.
  48. *
  49. * @var array
  50. */
  51. private $properties = array(
  52. 'columnName' => null,
  53. 'resultColumnName' => null,
  54. 'propertyName' => null,
  55. 'propertyType' => self::PHP_TYPE_STRING,
  56. 'converter' => null,
  57. 'databaseType' => PDO::PARAM_STR,
  58. );
  59. /**
  60. * Creates a new property definition object.
  61. *
  62. * Creates a new property definition object from the given values. The
  63. * $columnName refers to the name of the database column that, the
  64. * $propertyName to the name of the PHP object property it refers to.
  65. * The $type defines the type of the resulting PHP property, the database
  66. * value will be casted accordingly after load.
  67. *
  68. * @param string $columnName
  69. * @param string $propertyName
  70. * @param int $type
  71. * @param ezcPersistentPropertyConverter $converter
  72. * @param int $databaseType
  73. */
  74. public function __construct(
  75. $columnName = null,
  76. $propertyName = null,
  77. $type = self::PHP_TYPE_STRING,
  78. $converter = null,
  79. $databaseType = PDO::PARAM_STR
  80. )
  81. {
  82. $this->columnName = $columnName;
  83. $this->propertyName = $propertyName;
  84. $this->propertyType = $type;
  85. $this->converter = $converter;
  86. $this->databaseType = $databaseType;
  87. }
  88. /**
  89. * Returns a new instance of this class with the data specified by $array.
  90. *
  91. * $array contains all the data members of this class in the form:
  92. * array('member_name'=>value).
  93. *
  94. * __set_state makes this class exportable with var_export.
  95. * var_export() generates code, that calls this method when it
  96. * is parsed with PHP.
  97. *
  98. * @param array(string=>mixed) $array
  99. * @return ezcPersistentObjectProperty
  100. */
  101. public static function __set_state( array $array )
  102. {
  103. if ( isset( $array['properties'] ) )
  104. {
  105. return new ezcPersistentObjectProperty(
  106. $array['properties']['columnName'],
  107. $array['properties']['propertyName'],
  108. $array['properties']['propertyType'],
  109. ( isset( $array['properties']['converter'] ) ? $array['properties']['converter'] : null ),
  110. ( isset( $array['properties']['databaseType'] ) ? $array['properties']['databaseType'] : PDO::PARAM_STR )
  111. );
  112. }
  113. else
  114. {
  115. // Old style exports
  116. return new ezcPersistentObjectProperty(
  117. $array['columnName'],
  118. $array['propertyName'],
  119. $array['propertyType'],
  120. ( isset( $array['converter'] ) ? $array['converter'] : null ),
  121. ( isset( $array['databaseType'] ) ? $array['databaseType'] : PDO::PARAM_STR )
  122. );
  123. }
  124. }
  125. /**
  126. * Property read access.
  127. *
  128. * @param string $propertyName Name of the property.
  129. * @return mixed Value of the property or null.
  130. *
  131. * @throws ezcBasePropertyNotFoundException
  132. * If the the desired property is not found.
  133. * @ignore
  134. */
  135. public function __get( $propertyName )
  136. {
  137. if ( $this->__isset( $propertyName ) )
  138. {
  139. return $this->properties[$propertyName];
  140. }
  141. throw new ezcBasePropertyNotFoundException( $propertyName );
  142. }
  143. /**
  144. * Property write access.
  145. *
  146. * @param string $propertyName Name of the property.
  147. * @param mixed $propertyValue The value for the property.
  148. *
  149. * @throws ezcBasePropertyNotFoundException
  150. * If a the value for the property options is not an instance of
  151. * @throws ezcBaseValueException
  152. * If a the value for a property is out of range.
  153. * @ignore
  154. */
  155. public function __set( $propertyName, $propertyValue )
  156. {
  157. switch ( $propertyName )
  158. {
  159. case 'columnName':
  160. if ( is_string( $propertyValue ) === false && is_null( $propertyValue ) === false )
  161. {
  162. throw new ezcBaseValueException(
  163. $propertyName,
  164. $propertyValue,
  165. 'string or null'
  166. );
  167. }
  168. $this->properties['resultColumnName'] = ( $propertyValue !== null ) ? strtolower( $propertyValue ) : null;
  169. break;
  170. case 'propertyName':
  171. if ( is_string( $propertyValue ) === false && is_null( $propertyValue ) === false )
  172. {
  173. throw new ezcBaseValueException(
  174. $propertyName,
  175. $propertyValue,
  176. 'string or null'
  177. );
  178. }
  179. break;
  180. case 'propertyType':
  181. if ( is_int( $propertyValue ) === false && is_null( $propertyValue ) === false )
  182. {
  183. throw new ezcBaseValueException(
  184. $propertyName,
  185. $propertyValue,
  186. 'int or null'
  187. );
  188. }
  189. break;
  190. case 'converter':
  191. if ( !( $propertyValue instanceof ezcPersistentPropertyConverter ) && !is_null( $propertyValue ) )
  192. {
  193. throw new ezcBaseValueException(
  194. $propertyName,
  195. $propertyValue,
  196. 'ezcPersistentPropertyConverter or null'
  197. );
  198. }
  199. break;
  200. case 'databaseType':
  201. if ( $propertyValue !== PDO::PARAM_STR && $propertyValue !== PDO::PARAM_LOB && $propertyValue !== PDO::PARAM_INT && $propertyValue !== PDO::PARAM_BOOL )
  202. {
  203. throw new ezcBaseValueException(
  204. $propertyName,
  205. $propertyValue,
  206. 'PDO::PARAM_STR, PDO::PARAM_LOB, PDO::PARAM_INT or PDO::PARAM_BOOL'
  207. );
  208. }
  209. break;
  210. default:
  211. throw new ezcBasePropertyNotFoundException( $propertyName );
  212. break;
  213. }
  214. $this->properties[$propertyName] = $propertyValue;
  215. }
  216. /**
  217. * Property isset access.
  218. *
  219. * @param string $propertyName Name of the property.
  220. * @return bool True is the property is set, otherwise false.
  221. * @ignore
  222. */
  223. public function __isset( $propertyName )
  224. {
  225. return array_key_exists( $propertyName, $this->properties );
  226. }
  227. /**
  228. * @apichange Never used but left for BC reasons. Will be removed on next
  229. * major version.
  230. */
  231. const VISIBILITY_PRIVATE = 1;
  232. /**
  233. * @apichange Never used but left for BC reasons. Will be removed on next
  234. * major version.
  235. */
  236. const VISIBILITY_PROTECTED = 2;
  237. /**
  238. * @apichange Never used but left for BC reasons. Will be removed on next
  239. * major version.
  240. */
  241. const VISIBILITY_PUBLIC = 3;
  242. /**
  243. * @apichange Never used but left for BC reasons. Will be removed on next
  244. * major version.
  245. */
  246. const VISIBILITY_PROPERTY = 4;
  247. }
  248. ?>