PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/db/CDFDataColumn.php

https://bitbucket.org/cleardemon/cdf
PHP | 439 lines | 200 code | 38 blank | 201 comment | 10 complexity | b22021b1fea21dd2090dbb84395701a0 MD5 | raw file
  1. <?php
  2. /**
  3. * CDFDataColumn and all the implementations define the columns for a CDFDataObject, representing table column structure.
  4. * @package CDF
  5. */
  6. require_once 'CDFIDataConnection.php';
  7. require_once dirname(__FILE__) . '/../core/CDFDataHelper.php';
  8. require_once dirname(__FILE__) . '/../core/CDFExceptions.php';
  9. // Please use the constants and not the values directly!
  10. /**
  11. * Constants to use for specifying options to CDFDataColumns.
  12. */
  13. interface CDFDataColumnOption
  14. {
  15. // if true, value is required (strings, timestamp)
  16. const IsRequired = 'req';
  17. // if true, value not allowed to be null
  18. const NotNull = 'nn';
  19. // maximum length of the value (string, data)
  20. const MaxLength = 'Ml';
  21. // minimum length of the value (string, data)
  22. const MinLength = 'ml';
  23. // maximum range for the value (int, float, timestamp)
  24. const MaxRange = 'Mr';
  25. // minimum range for the value (int, float, timestamp)
  26. const MinRange = 'mr';
  27. // for DateTime fields, if unset will default to GMT
  28. const TimeZone = 'tz';
  29. }
  30. /**
  31. * Defines a column inside a CDFDataObject.
  32. * @throws CDFInvalidArgumentException
  33. */
  34. abstract class CDFDataColumn
  35. {
  36. /** @var int */
  37. private $_dataType; // value of CDFSqlDataType
  38. /** @var string */
  39. private $_name; // column name
  40. /** @var mixed */
  41. protected $_value; // column data
  42. /** @var bool */
  43. private $_optionNotNull = false; // if true, value not allowed to be null
  44. /** @var bool */
  45. private $_optionIsRequired = false; // if true, value is required (strings, timestamp)
  46. /** @var int */
  47. private $_optionMaxLength = 0; // maximum length of the value (string, data)
  48. /** @var int */
  49. private $_optionMinLength = 0; // minimum length of the value (string, data)
  50. /** @var float */
  51. private $_optionMinRange = 0; // minimum range for the value (int, float, timestamp)
  52. /** @var float */
  53. private $_optionMaxRange = 0; // maximum range for the value (int, float, timestamp)
  54. /**
  55. * Initialises the column.
  56. * @param int $dataType A value from CDFSqlDataType
  57. * @param string $name Name of the column
  58. * @param mixed|null $value Default value for the column or null.
  59. * @param array|null $opts A list of options to set, based on CDFDataColumnOption
  60. * @throws CDFInvalidArgumentException
  61. */
  62. protected function __construct($dataType, $name, $value = null, $opts = null)
  63. {
  64. if(!is_int($dataType) || !is_string($name))
  65. throw new CDFInvalidArgumentException();
  66. $this->_dataType = $dataType;
  67. $this->_name = $name;
  68. $this->setValue($value);
  69. $this->parseOptions($opts);
  70. }
  71. /**
  72. * Sets the value of the column.
  73. * @abstract
  74. * @param mixed|null $value
  75. * @return void
  76. * Implementations should do whatever special handling required for the defined data type of the column.
  77. */
  78. abstract public function setValue($value);
  79. /**
  80. * Gets the value of the column.
  81. * @abstract
  82. * @return mixed|null
  83. */
  84. abstract public function getValue();
  85. /**
  86. * Returns the name of the column.
  87. * @return string
  88. */
  89. final public function getName()
  90. {
  91. return $this->_name;
  92. }
  93. /**
  94. * Returns the CDFSqlDataType for this column.
  95. * @return int
  96. */
  97. final public function getDataType()
  98. {
  99. return $this->_dataType;
  100. }
  101. //
  102. // Options
  103. //
  104. /**
  105. * Parses options specified for the column.
  106. * Use a keyed array to pass options.
  107. * <code>
  108. * $this->parseOptions(array(
  109. * CDFDataColumnOption::IsRequired => true,
  110. * CDFDataColumnOption::MaxLength => 250
  111. * ));
  112. * </code>
  113. * @param array $opts
  114. * @return void
  115. */
  116. final protected function parseOptions($opts)
  117. {
  118. if(is_null($opts) || !is_array($opts))
  119. return;
  120. foreach($opts as $key => $value)
  121. {
  122. switch($key)
  123. {
  124. case CDFDataColumnOption::IsRequired:
  125. $this->_optionIsRequired = $value;
  126. break;
  127. case CDFDataColumnOption::MaxLength:
  128. $this->_optionMaxLength = $value;
  129. break;
  130. case CDFDataColumnOption::MaxRange:
  131. $this->_optionMaxRange = $value;
  132. break;
  133. case CDFDataColumnOption::MinLength:
  134. $this->_optionMinLength = $value;
  135. break;
  136. case CDFDataColumnOption::MinRange:
  137. $this->_optionMinRange = $value;
  138. break;
  139. case CDFDataColumnOption::NotNull:
  140. $this->_optionNotNull = $value;
  141. break;
  142. }
  143. }
  144. }
  145. /**
  146. * @return boolean
  147. */
  148. final public function getIsRequired()
  149. {
  150. return $this->_optionIsRequired;
  151. }
  152. /**
  153. * @return int
  154. */
  155. final public function getMaxLength()
  156. {
  157. return $this->_optionMaxLength;
  158. }
  159. /**
  160. * @return float
  161. */
  162. final public function getMaxRange()
  163. {
  164. return $this->_optionMaxRange;
  165. }
  166. /**
  167. * @return int
  168. */
  169. final public function getMinLength()
  170. {
  171. return $this->_optionMinLength;
  172. }
  173. /**
  174. * @return float
  175. */
  176. final public function getMinRange()
  177. {
  178. return $this->_optionMinRange;
  179. }
  180. /**
  181. * @return boolean
  182. */
  183. final public function getIsNotNull()
  184. {
  185. return $this->_optionNotNull;
  186. }
  187. }
  188. //
  189. // Implementations
  190. //
  191. /**
  192. * Base class for all string-related column types (string, text, data)
  193. * @throws CDFColumnDataException
  194. */
  195. abstract class CDFDataColumnStringBase extends CDFDataColumn
  196. {
  197. /**
  198. * @param string|null $value
  199. * @throws CDFColumnDataException
  200. * @return void
  201. */
  202. final public function setValue($value)
  203. {
  204. if(!is_null($value) && !is_string($value))
  205. throw new CDFColumnDataException($this->getName(), 'Value is not string');
  206. $this->_value = $value;
  207. }
  208. /**
  209. * Gets the string value of the column.
  210. * @return string
  211. */
  212. public function getValue()
  213. {
  214. return $this->_value;
  215. }
  216. }
  217. /**
  218. * Defines the String data column. (VARCHAR, etc)
  219. */
  220. final class CDFDataColumnString extends CDFDataColumnStringBase
  221. {
  222. /**
  223. * @param string $name
  224. * @param string|null $value
  225. * @param array|null $opts
  226. */
  227. public function __construct($name, $value = null, $opts = null)
  228. {
  229. parent::__construct(CDFSqlDataType::String, $name, $value, $opts);
  230. }
  231. }
  232. /**
  233. * Defines a Text data column. (TEXT, etc)
  234. */
  235. final class CDFDataColumnText extends CDFDataColumnStringBase
  236. {
  237. /**
  238. * @param string $name
  239. * @param string|null $value
  240. * @param array|null $opts
  241. */
  242. public function __construct($name, $value = null, $opts = null)
  243. {
  244. parent::__construct(CDFSqlDataType::Text, $name, $value, $opts);
  245. }
  246. }
  247. /**
  248. * Defines a Data column. (BLOB, BINARY, etc)
  249. */
  250. final class CDFDataColumnData extends CDFDataColumnStringBase
  251. {
  252. /**
  253. * @param string $name
  254. * @param string|null $value
  255. * @param array|null $opts
  256. */
  257. public function __construct($name, $value = null, $opts = null)
  258. {
  259. parent::__construct(CDFSqlDataType::Data, $name, $value, $opts);
  260. }
  261. }
  262. /**
  263. * Defines an Integer column. (INT, etc)
  264. * @throws CDFColumnDataException
  265. */
  266. final class CDFDataColumnInteger extends CDFDataColumn
  267. {
  268. /**
  269. * @param string $name
  270. * @param int|null $value
  271. * @param array|null $opts
  272. */
  273. public function __construct($name, $value = null, $opts = null)
  274. {
  275. parent::__construct(CDFSqlDataType::Integer, $name, $value, $opts);
  276. }
  277. /**
  278. * @param int|null $value
  279. * @return void
  280. * @throws CDFColumnDataException
  281. */
  282. public function setValue($value)
  283. {
  284. $this->_value = $value === null ? null : CDFDataHelper::AsInt($value); // force everything to become an integer
  285. }
  286. /**
  287. * Gets the integer value of the column.
  288. * @return int|null
  289. */
  290. public function getValue()
  291. {
  292. return $this->_value;
  293. }
  294. }
  295. /**
  296. * Defines a Float column. (FLOAT, DOUBLE, etc)
  297. * @throws CDFColumnDataException
  298. */
  299. final class CDFDataColumnFloat extends CDFDataColumn
  300. {
  301. /**
  302. * @param string $name
  303. * @param float|null $value
  304. * @param array|null $opts
  305. */
  306. public function __construct($name, $value = null, $opts = null)
  307. {
  308. parent::__construct(CDFSqlDataType::Float, $name, $value, $opts);
  309. }
  310. /**
  311. * @param float|null $value
  312. * @return void
  313. * @throws CDFColumnDataException
  314. */
  315. public function setValue($value)
  316. {
  317. $this->_value = $value === null ? null : CDFDataHelper::AsFloat($value); // force everything to float
  318. }
  319. /**
  320. * Gets the float value of the column.
  321. * @return float|null
  322. */
  323. public function getValue()
  324. {
  325. return $this->_value;
  326. }
  327. }
  328. /**
  329. * Defines a Timestamp column. (DATETIME, TIMESTAMP, etc)
  330. */
  331. final class CDFDataColumnTimestamp extends CDFDataColumn
  332. {
  333. private $_timeZone;
  334. /**
  335. * @param string $name
  336. * @param DateTime|null $value
  337. * @param array|null $opts
  338. */
  339. public function __construct($name, $value = null, $opts = null)
  340. {
  341. // parse options
  342. $this->_timeZone = 'GMT';
  343. if($opts != null && isset($opts[CDFDataColumnOption::TimeZone]))
  344. {
  345. // use the specified timezone
  346. $tz = $opts[CDFDataColumnOption::TimeZone];
  347. if(is_string($tz))
  348. $this->_timeZone = $tz;
  349. }
  350. parent::__construct(CDFSqlDataType::Timestamp, $name, CDFDataHelper::AsDateTime($value, $this->_timeZone), $opts);
  351. }
  352. /**
  353. * @param int|DateTime|null $value
  354. * @return void
  355. */
  356. public function setValue($value)
  357. {
  358. $this->_value = $value === null ? null : CDFDataHelper::AsDateTime($value, $this->_timeZone);
  359. }
  360. /**
  361. * Gets the value of the column.
  362. * @return DateTime
  363. */
  364. public function getValue()
  365. {
  366. return $this->_value;
  367. }
  368. }
  369. /**
  370. * Defines a Bool column. (BIT, TINYINT, etc)
  371. * @throws CDFColumnDataException
  372. */
  373. final class CDFDataColumnBool extends CDFDataColumn
  374. {
  375. /**
  376. * @param string $name
  377. * @param bool|null $value
  378. * @param array|null $opts
  379. */
  380. public function __construct($name, $value = null, $opts = null)
  381. {
  382. parent::__construct(CDFSqlDataType::Bool, $name, $value, $opts);
  383. }
  384. /**
  385. * @param bool|null $value
  386. * @return void
  387. * @throws CDFColumnDataException
  388. */
  389. public function setValue($value)
  390. {
  391. $this->_value = $value === null ? null : CDFDataHelper::AsBool($value); // force to boolean
  392. }
  393. /**
  394. * Gets the boolean value of the column.
  395. * @return bool|null
  396. */
  397. public function getValue()
  398. {
  399. return $this->_value;
  400. }
  401. }