/PersistentObject/src/object/persistent_object_id_property.php

https://github.com/F5/zetacomponents · PHP · 253 lines · 127 code · 6 blank · 120 comment · 17 complexity · b50e302b3c18a12ca88ffe8d33effb9c MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcPersistentObjectIdProperty 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 id field.
  28. *
  29. * The column should be of type int both in PHP and in the database, usually.
  30. * If you want to use a string ID, you need to use the {@link
  31. * ezcPersistentManualGenerator} and set the ID property of the affected object
  32. * yourself. Alternatively you can implement your own {@link
  33. * ezcPersistentIdentifierGenerator} to perform this operation consistently.
  34. *
  35. * The default value for the ID should be null, since {@link
  36. * ezcPersistentSession::save()} determines if an object was already saved this
  37. * way for integer IDs.
  38. *
  39. * For descriptions for some the constants used in this class see:
  40. * {@link ezcPersisentObjectProperty}
  41. *
  42. * @property string $columnName The name of the database field that stores the
  43. * value.
  44. * @property string $propertyName The name of the PersistentObject property
  45. * that holds the value in the PHP object.
  46. * @property int $propertyType The type of the PHP property. See class
  47. * constants ezcPersistentObjectProperty::PHP_TYPE_*.
  48. * @property int $visibility The visibility of the property. This property is deprecated!
  49. * @property ezcPersistentGeneratorDefinition $generator
  50. * The type of generator to use for the identifier.
  51. * The identifier generator must be an object that extends the
  52. * abstract class ezcPersistentIdentifierGenerator. The current
  53. * options that are part of this package are:
  54. * - ezcPersistentSequenceGenerator
  55. * - ezcPersistentManualGenerator
  56. * - ezcPersistentNativeGenerator
  57. * @property int $databaseType
  58. * Type of the database column, as defined by PDO constants: {@link
  59. * PDO::PARAM_BOOL}, {@link PDO::PARAM_INT}, {@link PDO::PARAM_STR}
  60. * (default as defined by {@link ezcQuery::bindValue()}) or {@link
  61. * PDO::PARAM_LOB} (important for binary data).
  62. *
  63. * @package PersistentObject
  64. * @version //autogen//
  65. */
  66. class ezcPersistentObjectIdProperty
  67. {
  68. /**
  69. * Holds the properties for this class.
  70. * @var array
  71. */
  72. private $properties = array(
  73. 'columnName' => null,
  74. 'resultColumnName' => null,
  75. 'propertyName' => null,
  76. 'propertyType' => ezcPersistentObjectProperty::PHP_TYPE_INT,
  77. 'generator' => null,
  78. 'visibility' => null,
  79. 'databaseType' => PDO::PARAM_STR,
  80. );
  81. /**
  82. * Constructs a new PersistentObjectField
  83. *
  84. * @param string $columnName The name of the database field that stores the value.
  85. * @param string $propertyName The name of the class member
  86. * @param int $visibility See {@link $visibility} for possible values.
  87. * @param ezcPersistentGeneratorDefinition $generator Definition of the identifier generator
  88. * @param int $propertyType See {@link ezcPersistentObjectProperty} for possible values.
  89. * @param int $databaseType See {@link PDO::*} for possible values.
  90. */
  91. public function __construct(
  92. $columnName = null,
  93. $propertyName = null,
  94. $visibility = null,
  95. ezcPersistentGeneratorDefinition $generator = null,
  96. $propertyType = ezcPersistentObjectProperty::PHP_TYPE_INT,
  97. $databaseType = PDO::PARAM_STR
  98. )
  99. {
  100. $this->columnName = $columnName;
  101. $this->propertyName = $propertyName;
  102. $this->visibility = $visibility;
  103. $this->generator = $generator;
  104. $this->propertyType = $propertyType;
  105. $this->databaseType = $databaseType;
  106. }
  107. /**
  108. * Property read access.
  109. *
  110. * @param string $propertyName Name of the property.
  111. * @return mixed Value of the property or null.
  112. *
  113. * @throws ezcBasePropertyNotFoundException
  114. * If the the desired property is not found.
  115. * @ignore
  116. */
  117. public function __get( $propertyName )
  118. {
  119. if ( $this->__isset( $propertyName ) )
  120. {
  121. return $this->properties[$propertyName];
  122. }
  123. throw new ezcBasePropertyNotFoundException( $propertyName );
  124. }
  125. /**
  126. * Property write access.
  127. *
  128. * @param string $propertyName Name of the property.
  129. * @param mixed $propertyValue The value for the property.
  130. *
  131. * @throws ezcBasePropertyNotFoundException
  132. * If a the value for the property options is not an instance of
  133. * @throws ezcBaseValueException
  134. * If a the value for a property is out of range.
  135. * @ignore
  136. */
  137. public function __set( $propertyName, $propertyValue )
  138. {
  139. switch ( $propertyName )
  140. {
  141. case 'columnName':
  142. if ( is_string( $propertyValue ) === false && is_null( $propertyValue ) === false )
  143. {
  144. throw new ezcBaseValueException(
  145. $propertyName,
  146. $propertyValue,
  147. 'string or null'
  148. );
  149. }
  150. $this->properties['resultColumnName'] = ( $propertyValue !== null ) ? strtolower( $propertyValue ) : null;
  151. break;
  152. case 'propertyName':
  153. if ( is_string( $propertyValue ) === false && is_null( $propertyValue ) === false )
  154. {
  155. throw new ezcBaseValueException(
  156. $propertyName,
  157. $propertyValue,
  158. 'string or null'
  159. );
  160. }
  161. break;
  162. case 'propertyType':
  163. case 'visibility':
  164. if ( is_int( $propertyValue ) === false && is_null( $propertyValue ) === false )
  165. {
  166. throw new ezcBaseValueException(
  167. $propertyName,
  168. $propertyValue,
  169. 'int or null'
  170. );
  171. }
  172. break;
  173. case 'generator':
  174. if ( ( is_object( $propertyValue ) === false || ( $propertyValue instanceof ezcPersistentGeneratorDefinition ) === false ) && is_null( $propertyValue ) === false )
  175. {
  176. throw new ezcBaseValueException(
  177. $propertyName,
  178. $propertyValue,
  179. 'ezcPersistentGeneratorDefinition or null'
  180. );
  181. }
  182. break;
  183. case 'databaseType':
  184. if ( $propertyValue !== PDO::PARAM_STR && $propertyValue !== PDO::PARAM_LOB && $propertyValue !== PDO::PARAM_INT && PDO::PARAM_BOOL )
  185. {
  186. throw new ezcBaseValueException(
  187. $propertyName,
  188. $propertyValue,
  189. 'PDO::PARAM_STR, PDO::PARAM_LOB, PDO::PARAM_INT or PDO::PARAM_BOOL'
  190. );
  191. }
  192. break;
  193. default:
  194. throw new ezcBasePropertyNotFoundException( $propertyName );
  195. break;
  196. }
  197. $this->properties[$propertyName] = $propertyValue;
  198. }
  199. /**
  200. * Property isset access.
  201. *
  202. * @param string $propertyName Name of the property.
  203. * @return bool True is the property is set, otherwise false.
  204. * @ignore
  205. */
  206. public function __isset( $propertyName )
  207. {
  208. return array_key_exists( $propertyName, $this->properties );
  209. }
  210. /**
  211. * Returns a new instance of this class with the data specified by $array.
  212. *
  213. * $array contains all the data members of this class in the form:
  214. * array('member_name'=>value).
  215. *
  216. * __set_state makes this class exportable with var_export.
  217. * var_export() generates code, that calls this method when it
  218. * is parsed with PHP.
  219. *
  220. * @param array(string=>mixed) $array
  221. * @return ezcPersistentObjectIdProperty
  222. */
  223. public static function __set_state( array $array )
  224. {
  225. if ( isset( $array['properties'] ) && count( $array ) === 1 )
  226. {
  227. return new ezcPersistentObjectIdProperty(
  228. $array['properties']['columnName'],
  229. $array['properties']['propertyName'],
  230. $array['properties']['visibility'],
  231. $array['properties']['generator'],
  232. ( isset( $array['properties']['databaseType'] ) ? $array['properties']['databaseType'] : PDO::PARAM_STR )
  233. );
  234. }
  235. else
  236. {
  237. return new ezcPersistentObjectIdProperty(
  238. $array['columnName'],
  239. $array['propertyName'],
  240. $array['visibility'],
  241. $array['generator'],
  242. ( isset( $array['databaseType'] ) ? $array['databaseType'] : PDO::PARAM_STR )
  243. );
  244. }
  245. }
  246. }
  247. ?>