1. sfModelGeneratorConfigurationField.class.php
  2. /** * Model generator field. * * @package symfony * @subpackage generator * @author Fabien Potencier * @version SVN: $Id: sfModelGeneratorConfigurationField.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfModelGeneratorConfigurationField
  4. {
  5. protected
  6. $name = null,
  7. $config = null;
  8. /**
  9. * Constructor.
  10. *
  11. * @param string $name The field name
  12. * @param array $config The configuration for this field
  13. */
  14. public function __construct($name, $config)
  15. {
  16. $this->name = $name;
  17. $this->config = $config;
  18. if (isset($this->config['flag']))
  19. {
  20. $this->setFlag($this->config['flag']);
  21. unset($this->config['flag']);
  22. }
  23. }
  24. /**
  25. * Returns the name of the field.
  26. *
  27. * @return string The field name
  28. */
  29. public function getName()
  30. {
  31. return $this->name;
  32. }
  33. /**
  34. * Returns the configuration value for a given key.
  35. *
  36. * If the key is null, the method returns all the configuration array.
  37. *
  38. * @param string $key A key string
  39. * @param mixed $default The default value if the key does not exist
  40. * @param Boolean $escaped Whether to escape single quote (false by default)
  41. *
  42. * @return mixed The configuration value associated with the key
  43. */
  44. public function getConfig($key = null, $default = null, $escaped = false)
  45. {
  46. if (null === $key)
  47. {
  48. return $this->config;
  49. }
  50. $value = sfModelGeneratorConfiguration::getFieldConfigValue($this->config, $key, $default);
  51. return $escaped ? str_replace("'", "\\'", $value) : $value;
  52. }
  53. /**
  54. * Returns the type of the field.
  55. *
  56. * @return string The field type
  57. */
  58. public function getType()
  59. {
  60. return $this->config['type'];
  61. }
  62. /**
  63. * Returns true if the column maps a database column.
  64. *
  65. * @return boolean true if the column maps a database column, false otherwise
  66. */
  67. public function isReal()
  68. {
  69. return isset($this->config['is_real']) ? $this->config['is_real'] : false;
  70. }
  71. /**
  72. * Returns true if the column is a partial.
  73. *
  74. * @return boolean true if the column is a partial, false otherwise
  75. */
  76. public function isPartial()
  77. {
  78. return isset($this->config['is_partial']) ? $this->config['is_partial'] : false;
  79. }
  80. /**
  81. * Sets or unsets the partial flag.
  82. *
  83. * @param Boolean $boolean true if the field is a partial, false otherwise
  84. */
  85. public function setPartial($boolean)
  86. {
  87. $this->config['is_partial'] = $boolean;
  88. }
  89. /**
  90. * Returns true if the column is a component.
  91. *
  92. * @return boolean true if the column is a component, false otherwise
  93. */
  94. public function isComponent()
  95. {
  96. return isset($this->config['is_component']) ? $this->config['is_component'] : false;
  97. }
  98. /**
  99. * Sets or unsets the component flag.
  100. *
  101. * @param Boolean $boolean true if the field is a component, false otherwise
  102. */
  103. public function setComponent($boolean)
  104. {
  105. $this->config['is_component'] = $boolean;
  106. }
  107. /**
  108. * Returns true if the column has a link.
  109. *
  110. * @return boolean true if the column has a link, false otherwise
  111. */
  112. public function isLink()
  113. {
  114. return isset($this->config['is_link']) ? $this->config['is_link'] : false;
  115. }
  116. /**
  117. * Sets or unsets the link flag.
  118. *
  119. * @param Boolean $boolean true if the field is a link, false otherwise
  120. */
  121. public function setLink($boolean)
  122. {
  123. $this->config['is_link'] = $boolean;
  124. }
  125. /**
  126. * Sets the list renderer for the field.
  127. *
  128. * @param mixed $renderer A PHP callable
  129. */
  130. public function setRenderer($renderer)
  131. {
  132. $this->config['renderer'] = $renderer;
  133. }
  134. /**
  135. * Gets the list renderer for the field.
  136. *
  137. * @return mixed A PHP callable
  138. */
  139. public function getRenderer()
  140. {
  141. return isset($this->config['renderer']) ? $this->config['renderer'] : null;
  142. }
  143. /**
  144. * Sets the list renderer arguments for the field.
  145. *
  146. * @param array $arguments An array of arguments to pass to the renderer
  147. */
  148. public function setRendererArguments(array $arguments)
  149. {
  150. $this->config['renderer_arguments'] = $arguments;
  151. }
  152. /**
  153. * Gets the list renderer arguments for the field.
  154. *
  155. * @return array An array of arguments to pass to the renderer
  156. */
  157. public function getRendererArguments()
  158. {
  159. return isset($this->config['renderer_arguments']) ? $this->config['renderer_arguments'] : array();
  160. }
  161. static public function splitFieldWithFlag($field)
  162. {
  163. if (in_array($flag = $field[0], array('=', '_', '~')))
  164. {
  165. $field = substr($field, 1);
  166. }
  167. else
  168. {
  169. $flag = null;
  170. }
  171. return array($field, $flag);
  172. }
  173. /**
  174. * Sets a flag.
  175. *
  176. * The flag can be =, _, or ~.
  177. *
  178. * @param string $flag The flag
  179. */
  180. public function setFlag($flag)
  181. {
  182. if (null === $flag)
  183. {
  184. return;
  185. }
  186. switch ($flag)
  187. {
  188. case '=':
  189. $this->setLink(true);
  190. break;
  191. case '_':
  192. $this->setPartial(true);
  193. break;
  194. case '~':
  195. $this->setComponent(true);
  196. break;
  197. default:
  198. throw new InvalidArgumentException(sprintf('Flag "%s" does not exist.', $flag));
  199. }
  200. }
  201. /**
  202. * Gets the flag associated with the field.
  203. *
  204. * The flag will be
  205. *
  206. * * = for a link
  207. * * _ for a partial
  208. * * ~ for a component
  209. *
  210. * @return string The flag
  211. */
  212. public function getFlag()
  213. {
  214. if ($this->isLink())
  215. {
  216. return '=';
  217. }
  218. else if ($this->isPartial())
  219. {
  220. return '_';
  221. }
  222. else if ($this->isComponent())
  223. {
  224. return '~';
  225. }
  226. return '';
  227. }
  228. }

Debug toolbar