Column.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. /**
  3. * Phinx
  4. *
  5. * (The MIT license)
  6. * Copyright (c) 2015 Rob Morgan
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated * documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. *
  26. * @package Phinx
  27. * @subpackage Phinx\Db
  28. */
  29. namespace BBDDL\Db\Table;
  30. /**
  31. *
  32. * This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
  33. */
  34. class Column
  35. {
  36. /**
  37. * @var string
  38. */
  39. protected $name;
  40. /**
  41. * @var string
  42. */
  43. protected $type;
  44. /**
  45. * @var integer
  46. */
  47. protected $limit = null;
  48. /**
  49. * @var boolean
  50. */
  51. protected $null = false;
  52. /**
  53. * @var mixed
  54. */
  55. protected $default = null;
  56. /**
  57. * @var boolean
  58. */
  59. protected $identity = false;
  60. /**
  61. * @var integer
  62. */
  63. protected $precision;
  64. /**
  65. * @var integer
  66. */
  67. protected $scale;
  68. /**
  69. * @var string
  70. */
  71. protected $after;
  72. /**
  73. * @var string
  74. */
  75. protected $update;
  76. /**
  77. * @var string
  78. */
  79. protected $comment;
  80. /**
  81. * @var boolean
  82. */
  83. protected $signed = true;
  84. /**
  85. * @var boolean
  86. */
  87. protected $timezone = false;
  88. /**
  89. * @var array
  90. */
  91. protected $properties = array();
  92. /**
  93. * @var array
  94. */
  95. protected $values;
  96. /**
  97. * Sets the column name.
  98. *
  99. * @param string $name
  100. * @return Column
  101. */
  102. public function setName($name)
  103. {
  104. $this->name = $name;
  105. return $this;
  106. }
  107. /**
  108. * Gets the column name.
  109. *
  110. * @return string
  111. */
  112. public function getName()
  113. {
  114. return $this->name;
  115. }
  116. /**
  117. * Sets the column type.
  118. *
  119. * @param string $type
  120. * @return Column
  121. */
  122. public function setType($type)
  123. {
  124. $this->type = $type;
  125. return $this;
  126. }
  127. /**
  128. * Gets the column type.
  129. *
  130. * @return string
  131. */
  132. public function getType()
  133. {
  134. return $this->type;
  135. }
  136. /**
  137. * Sets the column limit.
  138. *
  139. * @param integer $limit
  140. * @return Column
  141. */
  142. public function setLimit($limit)
  143. {
  144. $this->limit = $limit;
  145. return $this;
  146. }
  147. /**
  148. * Gets the column limit.
  149. *
  150. * @return integer
  151. */
  152. public function getLimit()
  153. {
  154. return $this->limit;
  155. }
  156. /**
  157. * Sets whether the column allows nulls.
  158. *
  159. * @param boolean $null
  160. * @return Column
  161. */
  162. public function setNull($null)
  163. {
  164. $this->null = (bool) $null;
  165. return $this;
  166. }
  167. /**
  168. * Gets whether the column allows nulls.
  169. *
  170. * @return boolean
  171. */
  172. public function getNull()
  173. {
  174. return $this->null;
  175. }
  176. /**
  177. * Does the column allow nulls?
  178. *
  179. * @return boolean
  180. */
  181. public function isNull()
  182. {
  183. return $this->getNull();
  184. }
  185. /**
  186. * Sets the default column value.
  187. *
  188. * @param mixed $default
  189. * @return Column
  190. */
  191. public function setDefault($default)
  192. {
  193. $this->default = $default;
  194. return $this;
  195. }
  196. /**
  197. * Gets the default column value.
  198. *
  199. * @return mixed
  200. */
  201. public function getDefault()
  202. {
  203. return $this->default;
  204. }
  205. /**
  206. * Sets whether or not the column is an identity column.
  207. *
  208. * @param boolean $identity
  209. * @return Column
  210. */
  211. public function setIdentity($identity)
  212. {
  213. $this->identity = $identity;
  214. return $this;
  215. }
  216. /**
  217. * Gets whether or not the column is an identity column.
  218. *
  219. * @return boolean
  220. */
  221. public function getIdentity()
  222. {
  223. return $this->identity;
  224. }
  225. /**
  226. * Is the column an identity column?
  227. *
  228. * @return boolean
  229. */
  230. public function isIdentity()
  231. {
  232. return $this->getIdentity();
  233. }
  234. /**
  235. * Sets the name of the column to add this column after.
  236. *
  237. * @param string $after After
  238. * @return Column
  239. */
  240. public function setAfter($after)
  241. {
  242. $this->after = $after;
  243. return $this;
  244. }
  245. /**
  246. * Returns the name of the column to add this column after.
  247. *
  248. * @return string
  249. */
  250. public function getAfter()
  251. {
  252. return $this->after;
  253. }
  254. /**
  255. * Sets the 'ON UPDATE' mysql column function.
  256. *
  257. * @param string $update On Update function
  258. * @return Column
  259. */
  260. public function setUpdate($update)
  261. {
  262. $this->update = $update;
  263. return $this;
  264. }
  265. /**
  266. * Returns the value of the ON UPDATE column function.
  267. *
  268. * @return string
  269. */
  270. public function getUpdate()
  271. {
  272. return $this->update;
  273. }
  274. /**
  275. * Sets the column precision for decimal.
  276. *
  277. * @param integer $precision
  278. * @return Column
  279. */
  280. public function setPrecision($precision)
  281. {
  282. $this->precision = $precision;
  283. return $this;
  284. }
  285. /**
  286. * Gets the column precision for decimal.
  287. *
  288. * @return integer
  289. */
  290. public function getPrecision()
  291. {
  292. return $this->precision;
  293. }
  294. /**
  295. * Sets the column scale for decimal.
  296. *
  297. * @param integer $scale
  298. * @return Column
  299. */
  300. public function setScale($scale)
  301. {
  302. $this->scale = $scale;
  303. return $this;
  304. }
  305. /**
  306. * Gets the column scale for decimal.
  307. *
  308. * @return integer
  309. */
  310. public function getScale()
  311. {
  312. return $this->scale;
  313. }
  314. /**
  315. * Sets the column comment.
  316. *
  317. * @param string $comment
  318. * @return Column
  319. */
  320. public function setComment($comment)
  321. {
  322. $this->comment = $comment;
  323. return $this;
  324. }
  325. /**
  326. * Gets the column comment.
  327. *
  328. * @return string
  329. */
  330. public function getComment()
  331. {
  332. return $this->comment;
  333. }
  334. /**
  335. * Sets whether field should be signed.
  336. *
  337. * @param bool $signed
  338. * @return Column
  339. */
  340. public function setSigned($signed)
  341. {
  342. $this->signed = (bool) $signed;
  343. return $this;
  344. }
  345. /**
  346. * Gets whether field should be signed.
  347. *
  348. * @return string
  349. */
  350. public function getSigned()
  351. {
  352. return $this->signed;
  353. }
  354. /**
  355. * Should the column be signed?
  356. *
  357. * @return boolean
  358. */
  359. public function isSigned()
  360. {
  361. return $this->getSigned();
  362. }
  363. /**
  364. * Sets whether the field should have a timezone identifier.
  365. * Used for date/time columns only!
  366. *
  367. * @param bool $timezone
  368. * @return Column
  369. */
  370. public function setTimezone($timezone)
  371. {
  372. $this->timezone = (bool) $timezone;
  373. return $this;
  374. }
  375. /**
  376. * Gets whether field has a timezone identifier.
  377. *
  378. * @return boolean
  379. */
  380. public function getTimezone()
  381. {
  382. return $this->timezone;
  383. }
  384. /**
  385. * Should the column have a timezone?
  386. *
  387. * @return boolean
  388. */
  389. public function isTimezone()
  390. {
  391. return $this->getTimezone();
  392. }
  393. /**
  394. * Sets field properties.
  395. *
  396. * @param array $properties
  397. *
  398. * @return Column
  399. */
  400. public function setProperties($properties)
  401. {
  402. $this->properties = $properties;
  403. return $this;
  404. }
  405. /**
  406. * Gets field properties
  407. *
  408. * @return array
  409. */
  410. public function getProperties()
  411. {
  412. return $this->properties;
  413. }
  414. /**
  415. * Sets field values.
  416. *
  417. * @param mixed (array|string) $values
  418. *
  419. * @return Column
  420. */
  421. public function setValues($values)
  422. {
  423. if (!is_array($values)) {
  424. $values = preg_split('/,\s*/', $values);
  425. }
  426. $this->values = $values;
  427. return $this;
  428. }
  429. /**
  430. * Gets field values
  431. *
  432. * @return string
  433. */
  434. public function getValues()
  435. {
  436. return $this->values;
  437. }
  438. /**
  439. * Gets all allowed options. Each option must have a corresponding `setFoo` method.
  440. *
  441. * @return array
  442. */
  443. protected function getValidOptions()
  444. {
  445. return array(
  446. 'limit',
  447. 'default',
  448. 'null',
  449. 'identity',
  450. 'precision',
  451. 'scale',
  452. 'after',
  453. 'update',
  454. 'comment',
  455. 'signed',
  456. 'timezone',
  457. 'properties',
  458. 'values',
  459. );
  460. }
  461. /**
  462. * Gets all aliased options. Each alias must reference a valid option.
  463. *
  464. * @return array
  465. */
  466. protected function getAliasedOptions()
  467. {
  468. return array(
  469. 'length' => 'limit',
  470. );
  471. }
  472. /**
  473. * Utility method that maps an array of column options to this objects methods.
  474. *
  475. * @param array $options Options
  476. * @return Column
  477. */
  478. public function setOptions($options)
  479. {
  480. $validOptions = $this->getValidOptions();
  481. $aliasOptions = $this->getAliasedOptions();
  482. foreach ($options as $option => $value) {
  483. if (isset($aliasOptions[$option])) {
  484. // proxy alias -> option
  485. $option = $aliasOptions[$option];
  486. }
  487. if (!in_array($option, $validOptions, true)) {
  488. throw new \RuntimeException(sprintf('"%s" is not a valid column option.', $option));
  489. }
  490. $method = 'set' . ucfirst($option);
  491. $this->$method($value);
  492. }
  493. return $this;
  494. }
  495. }