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