/PersistentObject/src/object/persistent_object_property.php

https://github.com/F5/zetacomponents · PHP · 277 lines · 133 code · 13 blank · 131 comment · 15 complexity · 2ecb17db0cd9e49136661310cf0a9088 MD5 · raw file

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