FieldBuilder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Mapping\Builder;
  20. /**
  21. * Field Builder
  22. *
  23. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  24. * @link www.doctrine-project.com
  25. * @since 2.2
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. class FieldBuilder
  29. {
  30. /**
  31. * @var ClassMetadataBuilder
  32. */
  33. private $builder;
  34. /**
  35. * @var array
  36. */
  37. private $mapping;
  38. /**
  39. * @var bool
  40. */
  41. private $version;
  42. /**
  43. * @var string
  44. */
  45. private $generatedValue;
  46. /**
  47. * @var array
  48. */
  49. private $sequenceDef;
  50. /**
  51. *
  52. * @param ClassMetadataBuilder $builder
  53. * @param array $mapping
  54. */
  55. public function __construct(ClassMetadataBuilder $builder, array $mapping)
  56. {
  57. $this->builder = $builder;
  58. $this->mapping = $mapping;
  59. }
  60. /**
  61. * Set length.
  62. *
  63. * @param int $length
  64. * @return FieldBuilder
  65. */
  66. public function length($length)
  67. {
  68. $this->mapping['length'] = $length;
  69. return $this;
  70. }
  71. /**
  72. * Set nullable
  73. *
  74. * @param bool
  75. * @return FieldBuilder
  76. */
  77. public function nullable($flag = true)
  78. {
  79. $this->mapping['nullable'] = (bool)$flag;
  80. return $this;
  81. }
  82. /**
  83. * Set Unique
  84. *
  85. * @param bool
  86. * @return FieldBuilder
  87. */
  88. public function unique($flag = true)
  89. {
  90. $this->mapping['unique'] = (bool)$flag;
  91. return $this;
  92. }
  93. /**
  94. * Set column name
  95. *
  96. * @param string $name
  97. * @return FieldBuilder
  98. */
  99. public function columnName($name)
  100. {
  101. $this->mapping['columnName'] = $name;
  102. return $this;
  103. }
  104. /**
  105. * Set Precision
  106. *
  107. * @param int $p
  108. * @return FieldBuilder
  109. */
  110. public function precision($p)
  111. {
  112. $this->mapping['precision'] = $p;
  113. return $this;
  114. }
  115. /**
  116. * Set scale.
  117. *
  118. * @param int $s
  119. * @return FieldBuilder
  120. */
  121. public function scale($s)
  122. {
  123. $this->mapping['scale'] = $s;
  124. return $this;
  125. }
  126. /**
  127. * Set field as primary key.
  128. *
  129. * @return FieldBuilder
  130. */
  131. public function isPrimaryKey()
  132. {
  133. $this->mapping['id'] = true;
  134. return $this;
  135. }
  136. /**
  137. * @param int $strategy
  138. * @return FieldBuilder
  139. */
  140. public function generatedValue($strategy = 'AUTO')
  141. {
  142. $this->generatedValue = $strategy;
  143. return $this;
  144. }
  145. /**
  146. * Set field versioned
  147. *
  148. * @return FieldBuilder
  149. */
  150. public function isVersionField()
  151. {
  152. $this->version = true;
  153. return $this;
  154. }
  155. /**
  156. * Set Sequence Generator
  157. *
  158. * @param string $sequenceName
  159. * @param int $allocationSize
  160. * @param int $initialValue
  161. * @return FieldBuilder
  162. */
  163. public function setSequenceGenerator($sequenceName, $allocationSize = 1, $initialValue = 1)
  164. {
  165. $this->sequenceDef = array(
  166. 'sequenceName' => $sequenceName,
  167. 'allocationSize' => $allocationSize,
  168. 'initialValue' => $initialValue,
  169. );
  170. return $this;
  171. }
  172. /**
  173. * Set column definition.
  174. *
  175. * @param string $def
  176. * @return FieldBuilder
  177. */
  178. public function columnDefinition($def)
  179. {
  180. $this->mapping['columnDefinition'] = $def;
  181. return $this;
  182. }
  183. /**
  184. * Finalize this field and attach it to the ClassMetadata.
  185. *
  186. * Without this call a FieldBuilder has no effect on the ClassMetadata.
  187. *
  188. * @return ClassMetadataBuilder
  189. */
  190. public function build()
  191. {
  192. $cm = $this->builder->getClassMetadata();
  193. if ($this->generatedValue) {
  194. $cm->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $this->generatedValue));
  195. }
  196. if ($this->version) {
  197. $cm->setVersionMapping($this->mapping);
  198. }
  199. $cm->mapField($this->mapping);
  200. if ($this->sequenceDef) {
  201. $cm->setSequenceGeneratorDefinition($this->sequenceDef);
  202. }
  203. return $this->builder;
  204. }
  205. }