/src/Propel/Generator/Model/Domain.php

https://github.com/fabienpomerol/Propel2 · PHP · 391 lines · 196 code · 49 blank · 146 comment · 21 complexity · 2f692416a587fb37286d1fd0704c9e79 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. namespace Propel\Generator\Model;
  10. use Propel\Generator\Exception\EngineException;
  11. /**
  12. * A class for holding data about a domain used in the schema.
  13. *
  14. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  15. * @author Martin Poeschl <mpoeschl@marmot.at> (Torque)
  16. * @author Hugo Hamon <webmaster@apprendre-php.com>
  17. */
  18. class Domain extends MappingModel
  19. {
  20. private $name;
  21. private $description;
  22. private $size;
  23. private $scale;
  24. private $mappingType;
  25. private $sqlType;
  26. private $defaultValue;
  27. private $database;
  28. /**
  29. * Creates a new Domain object.
  30. *
  31. * If this domain needs a name, it must be specified manually.
  32. *
  33. * @param string $type Propel type.
  34. * @param string $sqlType SQL type.
  35. * @param string $size
  36. * @param string $scale
  37. */
  38. public function __construct($type = null, $sqlType = null, $size = null, $scale = null)
  39. {
  40. parent::__construct();
  41. if (null !== $type) {
  42. $this->setType($type);
  43. }
  44. if (null !== $size) {
  45. $this->setSize($size);
  46. }
  47. if (null !== $scale) {
  48. $this->setScale($scale);
  49. }
  50. $this->setSqlType(null !== $sqlType ? $sqlType : $type);
  51. }
  52. /**
  53. * Copies the values from current object into passed-in Domain.
  54. *
  55. * @param Domain $domain Domain to copy values into.
  56. */
  57. public function copy(Domain $domain)
  58. {
  59. $this->defaultValue = $domain->getDefaultValue();
  60. $this->description = $domain->getDescription();
  61. $this->name = $domain->getName();
  62. $this->scale = $domain->getScale();
  63. $this->size = $domain->getSize();
  64. $this->sqlType = $domain->getSqlType();
  65. $this->mappingType = $domain->getType();
  66. }
  67. protected function setupObject()
  68. {
  69. $schemaType = strtoupper($this->getAttribute('type'));
  70. $this->copy($this->database->getPlatform()->getDomainForType($schemaType));
  71. //Name
  72. $this->name = $this->getAttribute('name');
  73. // Default value
  74. $defval = $this->getAttribute('defaultValue', $this->getAttribute('default'));
  75. if (null !== $defval) {
  76. $this->setDefaultValue(new ColumnDefaultValue($defval, ColumnDefaultValue::TYPE_VALUE));
  77. } elseif (null !== $this->getAttribute('defaultExpr')) {
  78. $this->setDefaultValue(new ColumnDefaultValue($this->getAttribute('defaultExpr'), ColumnDefaultValue::TYPE_EXPR));
  79. }
  80. $this->size = $this->getAttribute('size');
  81. $this->scale = $this->getAttribute('scale');
  82. $this->description = $this->getAttribute('description');
  83. }
  84. /**
  85. * Sets the owning database object (if this domain is being setup via XML).
  86. *
  87. * @param Database $database
  88. */
  89. public function setDatabase(Database $database)
  90. {
  91. $this->database = $database;
  92. }
  93. /**
  94. * Returns the owning database object (if this domain was setup via XML).
  95. *
  96. * @return Database
  97. */
  98. public function getDatabase()
  99. {
  100. return $this->database;
  101. }
  102. /**
  103. * Returns the domain description.
  104. *
  105. * @return string
  106. */
  107. public function getDescription()
  108. {
  109. return $this->description;
  110. }
  111. /**
  112. * Sets the domain description.
  113. *
  114. * @param string $description
  115. */
  116. public function setDescription($description)
  117. {
  118. $this->description = $description;
  119. }
  120. /**
  121. * Returns the domain description.
  122. *
  123. * @return string
  124. */
  125. public function getName()
  126. {
  127. return $this->name;
  128. }
  129. /**
  130. * Sets the domain name.
  131. *
  132. * @param string $name
  133. */
  134. public function setName($name)
  135. {
  136. $this->name = $name;
  137. }
  138. /**
  139. * Returns the scale value.
  140. *
  141. * @return integer
  142. */
  143. public function getScale()
  144. {
  145. return $this->scale;
  146. }
  147. /**
  148. * Sets the scale value.
  149. *
  150. * @param integer $scale
  151. */
  152. public function setScale($scale)
  153. {
  154. $this->scale = (int) $scale;
  155. }
  156. /**
  157. * Replaces the size if the new value is not null.
  158. *
  159. * @param integer $scale
  160. */
  161. public function replaceScale($scale)
  162. {
  163. if (null !== $scale) {
  164. $this->scale = (int) $scale;
  165. }
  166. }
  167. /**
  168. * Returns the size.
  169. *
  170. * @return integer
  171. */
  172. public function getSize()
  173. {
  174. return $this->size;
  175. }
  176. /**
  177. * Sets the size.
  178. *
  179. * @param integer $size
  180. */
  181. public function setSize($size)
  182. {
  183. $this->size = (int) $size;
  184. }
  185. /**
  186. * Replaces the size if the new value is not null.
  187. *
  188. * @param integer $size
  189. */
  190. public function replaceSize($size)
  191. {
  192. if (null !== $size) {
  193. $this->size = $size;
  194. }
  195. }
  196. /**
  197. * Returns the mapping type.
  198. *
  199. * @return string
  200. */
  201. public function getType()
  202. {
  203. return $this->mappingType;
  204. }
  205. /**
  206. * Sets the mapping type.
  207. *
  208. * @param string $mappingType
  209. */
  210. public function setType($mappingType)
  211. {
  212. $this->mappingType = $mappingType;
  213. }
  214. /**
  215. * Replaces the mapping type if the new value is not null.
  216. *
  217. * @param string $mappingType
  218. */
  219. public function replaceType($mappingType)
  220. {
  221. if (null !== $mappingType) {
  222. $this->mappingType = $mappingType;
  223. }
  224. }
  225. /**
  226. * Returns the default value object.
  227. *
  228. * @return ColumnDefaultValue
  229. */
  230. public function getDefaultValue()
  231. {
  232. return $this->defaultValue;
  233. }
  234. /**
  235. * Returns the default value, type-casted for use in PHP OM.
  236. *
  237. * @return mixed
  238. */
  239. public function getPhpDefaultValue()
  240. {
  241. if (null === $this->defaultValue) {
  242. return null;
  243. }
  244. if ($this->defaultValue->isExpression()) {
  245. throw new EngineException('Cannot get PHP version of default value for default value EXPRESSION.');
  246. }
  247. if (in_array($this->mappingType, array(PropelTypes::BOOLEAN, PropelTypes::BOOLEAN_EMU))) {
  248. return $this->booleanValue($this->defaultValue->getValue());
  249. }
  250. return $this->defaultValue->getValue();
  251. }
  252. /**
  253. * Sets the default value.
  254. *
  255. * @param ColumnDefaultValue $value
  256. */
  257. public function setDefaultValue(ColumnDefaultValue $value)
  258. {
  259. $this->defaultValue = $value;
  260. }
  261. /**
  262. * Replaces the default value if the new value is not null.
  263. *
  264. * @param ColumnDefaultValue $value
  265. */
  266. public function replaceDefaultValue(ColumnDefaultValue $value = null)
  267. {
  268. if (null !== $value) {
  269. $this->defaultValue = $value;
  270. }
  271. }
  272. /**
  273. * Returns the SQL type.
  274. *
  275. * @return string
  276. */
  277. public function getSqlType()
  278. {
  279. return $this->sqlType;
  280. }
  281. /**
  282. * Sets the SQL type.
  283. *
  284. * @param string $sqlType
  285. */
  286. public function setSqlType($sqlType)
  287. {
  288. $this->sqlType = $sqlType;
  289. }
  290. /**
  291. * Replaces the SQL type if the new value is not null.
  292. *
  293. * @param string $sqlType
  294. */
  295. public function replaceSqlType($sqlType)
  296. {
  297. if (null !== $sqlType) {
  298. $this->sqlType = $sqlType;
  299. }
  300. }
  301. /**
  302. * Returns the size and scale in brackets for use in an sql schema.
  303. *
  304. * @return string
  305. */
  306. public function getSizeDefinition()
  307. {
  308. if (null !== $this->size) {
  309. if (null !== $this->scale) {
  310. return sprintf('(%u,%u)', $this->size, $this->scale);
  311. }
  312. return sprintf('(%u)', $this->size);
  313. }
  314. return '';
  315. }
  316. public function appendXml(\DOMNode $node)
  317. {
  318. $doc = ($node instanceof \DOMDocument) ? $node : $node->ownerDocument;
  319. $domainNode = $node->appendChild($doc->createElement('domain'));
  320. $domainNode->setAttribute('type', $this->getType());
  321. $domainNode->setAttribute('name', $this->getName());
  322. if ($this->getType() !== $this->sqlType) {
  323. $domainNode->setAttribute('sqlType', $this->sqlType);
  324. }
  325. if ($def = $this->getDefaultValue()) {
  326. if ($def->isExpression()) {
  327. $domainNode->setAttribute('defaultExpr', $def->getValue());
  328. } else {
  329. $domainNode->setAttribute('defaultValue', $def->getValue());
  330. }
  331. }
  332. if ($this->size) {
  333. $domainNode->setAttribute('size', $this->size);
  334. }
  335. if ($this->scale) {
  336. $domainNode->setAttribute('scale', $this->scale);
  337. }
  338. if ($this->description) {
  339. $domainNode->setAttribute('description', $this->description);
  340. }
  341. }
  342. }