/lib/Doctrine/DBAL/Schema/Column.php

https://github.com/kimhemsoe/dbal · PHP · 487 lines · 205 code · 68 blank · 214 comment · 5 complexity · 312c440ad7aa687feb26307bc4a3439a MD5 · raw file

  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. use Doctrine\DBAL\Types\Type;
  21. /**
  22. * Object representation of a database column.
  23. *
  24. * @link www.doctrine-project.org
  25. * @since 2.0
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. class Column extends AbstractAsset
  29. {
  30. /**
  31. * @var Type
  32. */
  33. protected $_type;
  34. /**
  35. * @var integer|null
  36. */
  37. protected $_length = null;
  38. /**
  39. * @var integer
  40. */
  41. protected $_precision = 10;
  42. /**
  43. * @var integer
  44. */
  45. protected $_scale = 0;
  46. /**
  47. * @var boolean
  48. */
  49. protected $_unsigned = false;
  50. /**
  51. * @var boolean
  52. */
  53. protected $_fixed = false;
  54. /**
  55. * @var boolean
  56. */
  57. protected $_notnull = true;
  58. /**
  59. * @var string|null
  60. */
  61. protected $_default = null;
  62. /**
  63. * @var boolean
  64. */
  65. protected $_autoincrement = false;
  66. /**
  67. * @var array
  68. */
  69. protected $_platformOptions = [];
  70. /**
  71. * @var string|null
  72. */
  73. protected $_columnDefinition = null;
  74. /**
  75. * @var string|null
  76. */
  77. protected $_comment = null;
  78. /**
  79. * @var array
  80. */
  81. protected $_customSchemaOptions = [];
  82. /**
  83. * Creates a new Column.
  84. *
  85. * @param string $columnName
  86. * @param Type $type
  87. * @param array $options
  88. */
  89. public function __construct($columnName, Type $type, array $options=[])
  90. {
  91. $this->_setName($columnName);
  92. $this->setType($type);
  93. $this->setOptions($options);
  94. }
  95. /**
  96. * @param array $options
  97. *
  98. * @return Column
  99. */
  100. public function setOptions(array $options)
  101. {
  102. foreach ($options as $name => $value) {
  103. $method = "set".$name;
  104. if (method_exists($this, $method)) {
  105. $this->$method($value);
  106. }
  107. }
  108. return $this;
  109. }
  110. /**
  111. * @param Type $type
  112. *
  113. * @return Column
  114. */
  115. public function setType(Type $type)
  116. {
  117. $this->_type = $type;
  118. return $this;
  119. }
  120. /**
  121. * @param integer|null $length
  122. *
  123. * @return Column
  124. */
  125. public function setLength($length)
  126. {
  127. if ($length !== null) {
  128. $this->_length = (int) $length;
  129. } else {
  130. $this->_length = null;
  131. }
  132. return $this;
  133. }
  134. /**
  135. * @param integer $precision
  136. *
  137. * @return Column
  138. */
  139. public function setPrecision($precision)
  140. {
  141. if (!is_numeric($precision)) {
  142. $precision = 10; // defaults to 10 when no valid precision is given.
  143. }
  144. $this->_precision = (int) $precision;
  145. return $this;
  146. }
  147. /**
  148. * @param integer $scale
  149. *
  150. * @return Column
  151. */
  152. public function setScale($scale)
  153. {
  154. if (!is_numeric($scale)) {
  155. $scale = 0;
  156. }
  157. $this->_scale = (int) $scale;
  158. return $this;
  159. }
  160. /**
  161. * @param boolean $unsigned
  162. *
  163. * @return Column
  164. */
  165. public function setUnsigned($unsigned)
  166. {
  167. $this->_unsigned = (bool) $unsigned;
  168. return $this;
  169. }
  170. /**
  171. * @param boolean $fixed
  172. *
  173. * @return Column
  174. */
  175. public function setFixed($fixed)
  176. {
  177. $this->_fixed = (bool) $fixed;
  178. return $this;
  179. }
  180. /**
  181. * @param boolean $notnull
  182. *
  183. * @return Column
  184. */
  185. public function setNotnull($notnull)
  186. {
  187. $this->_notnull = (bool) $notnull;
  188. return $this;
  189. }
  190. /**
  191. * @param mixed $default
  192. *
  193. * @return Column
  194. */
  195. public function setDefault($default)
  196. {
  197. $this->_default = $default;
  198. return $this;
  199. }
  200. /**
  201. * @param array $platformOptions
  202. *
  203. * @return Column
  204. */
  205. public function setPlatformOptions(array $platformOptions)
  206. {
  207. $this->_platformOptions = $platformOptions;
  208. return $this;
  209. }
  210. /**
  211. * @param string $name
  212. * @param mixed $value
  213. *
  214. * @return Column
  215. */
  216. public function setPlatformOption($name, $value)
  217. {
  218. $this->_platformOptions[$name] = $value;
  219. return $this;
  220. }
  221. /**
  222. * @param string $value
  223. *
  224. * @return Column
  225. */
  226. public function setColumnDefinition($value)
  227. {
  228. $this->_columnDefinition = $value;
  229. return $this;
  230. }
  231. /**
  232. * @return Type
  233. */
  234. public function getType()
  235. {
  236. return $this->_type;
  237. }
  238. /**
  239. * @return integer|null
  240. */
  241. public function getLength()
  242. {
  243. return $this->_length;
  244. }
  245. /**
  246. * @return integer
  247. */
  248. public function getPrecision()
  249. {
  250. return $this->_precision;
  251. }
  252. /**
  253. * @return integer
  254. */
  255. public function getScale()
  256. {
  257. return $this->_scale;
  258. }
  259. /**
  260. * @return boolean
  261. */
  262. public function getUnsigned()
  263. {
  264. return $this->_unsigned;
  265. }
  266. /**
  267. * @return boolean
  268. */
  269. public function getFixed()
  270. {
  271. return $this->_fixed;
  272. }
  273. /**
  274. * @return boolean
  275. */
  276. public function getNotnull()
  277. {
  278. return $this->_notnull;
  279. }
  280. /**
  281. * @return string|null
  282. */
  283. public function getDefault()
  284. {
  285. return $this->_default;
  286. }
  287. /**
  288. * @return array
  289. */
  290. public function getPlatformOptions()
  291. {
  292. return $this->_platformOptions;
  293. }
  294. /**
  295. * @param string $name
  296. *
  297. * @return boolean
  298. */
  299. public function hasPlatformOption($name)
  300. {
  301. return isset($this->_platformOptions[$name]);
  302. }
  303. /**
  304. * @param string $name
  305. *
  306. * @return mixed
  307. */
  308. public function getPlatformOption($name)
  309. {
  310. return $this->_platformOptions[$name];
  311. }
  312. /**
  313. * @return string|null
  314. */
  315. public function getColumnDefinition()
  316. {
  317. return $this->_columnDefinition;
  318. }
  319. /**
  320. * @return boolean
  321. */
  322. public function getAutoincrement()
  323. {
  324. return $this->_autoincrement;
  325. }
  326. /**
  327. * @param boolean $flag
  328. *
  329. * @return Column
  330. */
  331. public function setAutoincrement($flag)
  332. {
  333. $this->_autoincrement = $flag;
  334. return $this;
  335. }
  336. /**
  337. * @param string $comment
  338. *
  339. * @return Column
  340. */
  341. public function setComment($comment)
  342. {
  343. $this->_comment = $comment;
  344. return $this;
  345. }
  346. /**
  347. * @return string|null
  348. */
  349. public function getComment()
  350. {
  351. return $this->_comment;
  352. }
  353. /**
  354. * @param string $name
  355. * @param mixed $value
  356. *
  357. * @return Column
  358. */
  359. public function setCustomSchemaOption($name, $value)
  360. {
  361. $this->_customSchemaOptions[$name] = $value;
  362. return $this;
  363. }
  364. /**
  365. * @param string $name
  366. *
  367. * @return boolean
  368. */
  369. public function hasCustomSchemaOption($name)
  370. {
  371. return isset($this->_customSchemaOptions[$name]);
  372. }
  373. /**
  374. * @param string $name
  375. *
  376. * @return mixed
  377. */
  378. public function getCustomSchemaOption($name)
  379. {
  380. return $this->_customSchemaOptions[$name];
  381. }
  382. /**
  383. * @param array $customSchemaOptions
  384. *
  385. * @return Column
  386. */
  387. public function setCustomSchemaOptions(array $customSchemaOptions)
  388. {
  389. $this->_customSchemaOptions = $customSchemaOptions;
  390. return $this;
  391. }
  392. /**
  393. * @return array
  394. */
  395. public function getCustomSchemaOptions()
  396. {
  397. return $this->_customSchemaOptions;
  398. }
  399. /**
  400. * @return array
  401. */
  402. public function toArray()
  403. {
  404. return array_merge([
  405. 'name' => $this->_name,
  406. 'type' => $this->_type,
  407. 'default' => $this->_default,
  408. 'notnull' => $this->_notnull,
  409. 'length' => $this->_length,
  410. 'precision' => $this->_precision,
  411. 'scale' => $this->_scale,
  412. 'fixed' => $this->_fixed,
  413. 'unsigned' => $this->_unsigned,
  414. 'autoincrement' => $this->_autoincrement,
  415. 'columnDefinition' => $this->_columnDefinition,
  416. 'comment' => $this->_comment,
  417. ], $this->_platformOptions, $this->_customSchemaOptions);
  418. }
  419. }