Column.php 8.3 KB

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