sfModelGeneratorConfigurationField.class.php 5.5 KB

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