Column.php 9.8 KB

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