PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/database/model/Domain.php

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