PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/propel/generator/lib/model/Domain.php

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