123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- /**
- * Model generator field.
- *
- * @package symfony
- * @subpackage generator
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfModelGeneratorConfigurationField.class.php 16440 2009-03-19 15:18:36Z hartym $
- */
- class sfModelGeneratorConfigurationField
- {
- protected
- $name = null,
- $config = null;
- /**
- * Constructor.
- *
- * @param string $name The field name
- * @param array $config The configuration for this field
- */
- public function __construct($name, $config)
- {
- $this->name = $name;
- $this->config = $config;
- if (isset($this->config['flag']))
- {
- $this->setFlag($this->config['flag']);
- unset($this->config['flag']);
- }
- }
- /**
- * Returns the name of the field.
- *
- * @return string The field name
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * Returns the configuration value for a given key.
- *
- * If the key is null, the method returns all the configuration array.
- *
- * @param string $key A key string
- * @param mixed $default The default value if the key does not exist
- * @param Boolean $escaped Whether to escape single quote (false by default)
- *
- * @return mixed The configuration value associated with the key
- */
- public function getConfig($key = null, $default = null, $escaped = false)
- {
- if (is_null($key))
- {
- return $this->config;
- }
- $value = sfModelGeneratorConfiguration::getFieldConfigValue($this->config, $key, $default);
- return $escaped ? str_replace("'", "\\'", $value) : $value;
- }
- /**
- * Returns the type of the field.
- *
- * @return string The field type
- */
- public function getType()
- {
- return $this->config['type'];
- }
- /**
- * Returns true if the column maps a database column.
- *
- * @return boolean true if the column maps a database column, false otherwise
- */
- public function isReal()
- {
- return isset($this->config['is_real']) ? $this->config['is_real'] : false;
- }
- /**
- * Returns true if the column is a partial.
- *
- * @return boolean true if the column is a partial, false otherwise
- */
- public function isPartial()
- {
- return isset($this->config['is_partial']) ? $this->config['is_partial'] : false;
- }
- /**
- * Sets or unsets the partial flag.
- *
- * @param Boolean $boolean true if the field is a partial, false otherwise
- */
- public function setPartial($boolean)
- {
- $this->config['is_partial'] = $boolean;
- }
- /**
- * Returns true if the column is a component.
- *
- * @return boolean true if the column is a component, false otherwise
- */
- public function isComponent()
- {
- return isset($this->config['is_component']) ? $this->config['is_component'] : false;
- }
- /**
- * Sets or unsets the component flag.
- *
- * @param Boolean $boolean true if the field is a component, false otherwise
- */
- public function setComponent($boolean)
- {
- $this->config['is_component'] = $boolean;
- }
- /**
- * Returns true if the column has a link.
- *
- * @return boolean true if the column has a link, false otherwise
- */
- public function isLink()
- {
- return isset($this->config['is_link']) ? $this->config['is_link'] : false;
- }
- /**
- * Sets or unsets the link flag.
- *
- * @param Boolean $boolean true if the field is a link, false otherwise
- */
- public function setLink($boolean)
- {
- $this->config['is_link'] = $boolean;
- }
- /**
- * Sets the list renderer for the field.
- *
- * @param mixed A PHP callable
- */
- public function setRenderer($renderer)
- {
- $this->config['renderer'] = $renderer;
- }
- /**
- * Gets the list renderer for the field.
- *
- * @return mixed A PHP callable
- */
- public function getRenderer()
- {
- return isset($this->config['renderer']) ? $this->config['renderer'] : null;
- }
- /**
- * Sets the list renderer arguments for the field.
- *
- * @param array An array of arguments to pass to the renderer
- */
- public function setRendererArguments(array $arguments)
- {
- $this->config['renderer_arguments'] = $arguments;
- }
- /**
- * Gets the list renderer arguments for the field.
- *
- * @return array An array of arguments to pass to the renderer
- */
- public function getRendererArguments()
- {
- return isset($this->config['renderer_arguments']) ? $this->config['renderer_arguments'] : array();
- }
- static public function splitFieldWithFlag($field)
- {
- if (in_array($flag = $field[0], array('=', '_', '~')))
- {
- $field = substr($field, 1);
- }
- else
- {
- $flag = null;
- }
- return array($field, $flag);
- }
- /**
- * Sets a flag.
- *
- * The flag can be =, _, or ~.
- *
- * @param string The flag
- */
- public function setFlag($flag)
- {
- if (is_null($flag))
- {
- return;
- }
- switch ($flag)
- {
- case '=':
- $this->setLink(true);
- break;
- case '_':
- $this->setPartial(true);
- break;
- case '~':
- $this->setComponent(true);
- break;
- default:
- throw new InvalidArgumentException(sprintf('Flag "%s" does not exist.', $flag));
- }
- }
- /**
- * Gets the flag associated with the field.
- *
- * The flag will be
- *
- * * = for a link
- * * _ for a partial
- * * ~ for a component
- *
- * @return string The flag
- */
- public function getFlag()
- {
- if ($this->isLink())
- {
- return '=';
- }
- else if ($this->isPartial())
- {
- return '_';
- }
- else if ($this->isComponent())
- {
- return '~';
- }
- return '';
- }
- }
|