@@ -170,7 +170,7 @@ |
||
| 170 | 170 | * Set the select options |
| 171 | 171 | * |
| 172 | 172 | * @param array $_options The options as an array |
| 173 | - * @param mixed $selected Facultative selected entry |
|
| 173 | + * @param null|string $selected Facultative selected entry |
|
| 174 | 174 | * @param boolean $valuesAsKeys Whether the array's values should be used as |
| 175 | 175 | * the option's values instead of the array's keys |
| 176 | 176 | */ |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $this->name .= '[]'; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if ( ! $this->value instanceOf \ArrayAccess) { |
|
| 93 | + if (!$this->value instanceOf \ArrayAccess) { |
|
| 94 | 94 | $this->value = (array) $this->value; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | foreach ($parent->getChildren() as $child) { |
| 133 | 133 | // Search by value |
| 134 | 134 | |
| 135 | - if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int)$value ) { |
|
| 135 | + if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int) $value) { |
|
| 136 | 136 | $child->selected('selected'); |
| 137 | 137 | } |
| 138 | 138 | |
@@ -12,297 +12,297 @@ |
||
| 12 | 12 | class Select extends Field |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The select's placeholder |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - private $placeholder = null; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The Select's options |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - protected $options; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The select's element |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $element = 'select'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The select's self-closing state |
|
| 38 | - * |
|
| 39 | - * @var boolean |
|
| 40 | - */ |
|
| 41 | - protected $isSelfClosing = false; |
|
| 42 | - |
|
| 43 | - //////////////////////////////////////////////////////////////////// |
|
| 44 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 45 | - //////////////////////////////////////////////////////////////////// |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Easier arguments order for selects |
|
| 49 | - * |
|
| 50 | - * @param Container $app The Container instance |
|
| 51 | - * @param string $type select |
|
| 52 | - * @param string $name Field name |
|
| 53 | - * @param string $label Its label |
|
| 54 | - * @param array $options The select's options |
|
| 55 | - * @param string $selected The selected option |
|
| 56 | - * @param array $attributes Attributes |
|
| 57 | - */ |
|
| 58 | - public function __construct(Container $app, $type, $name, $label, $options, $selected, $attributes) |
|
| 59 | - { |
|
| 60 | - if ($selected) { |
|
| 61 | - $this->value = $selected; |
|
| 62 | - } |
|
| 63 | - if ($options) { |
|
| 64 | - $this->options($options); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 68 | - |
|
| 69 | - // Nested models population |
|
| 70 | - if (str_contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 71 | - $this->fromQuery($this->value); |
|
| 72 | - $this->value = $selected ?: null; |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Renders the select |
|
| 78 | - * |
|
| 79 | - * @return string A <select> tag |
|
| 80 | - */ |
|
| 81 | - public function render() |
|
| 82 | - { |
|
| 83 | - // Multiselects |
|
| 84 | - if ($this->isOfType('multiselect')) { |
|
| 85 | - if (!isset($this->attributes['id'])) { |
|
| 86 | - $this->setAttribute('id', $this->name); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $this->multiple(); |
|
| 90 | - $this->name .= '[]'; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - if ( ! $this->value instanceOf \ArrayAccess) { |
|
| 94 | - $this->value = (array) $this->value; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Mark selected values as selected |
|
| 98 | - if ($this->hasChildren() and !empty($this->value)) { |
|
| 99 | - foreach ($this->value as $value) { |
|
| 100 | - if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 101 | - $value = $value->getKey(); |
|
| 102 | - } |
|
| 103 | - $this->selectValue($value); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // Add placeholder text if any |
|
| 108 | - if ($placeholder = $this->getPlaceholder()) { |
|
| 109 | - array_unshift($this->children, $placeholder); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $this->value = null; |
|
| 113 | - |
|
| 114 | - return parent::render(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Select a value in the field's children |
|
| 119 | - * |
|
| 120 | - * @param mixed $value |
|
| 121 | - * @param Element $parent |
|
| 122 | - * |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - protected function selectValue($value, $parent = null) |
|
| 126 | - { |
|
| 127 | - // If no parent element defined, use direct children |
|
| 128 | - if (!$parent) { |
|
| 129 | - $parent = $this; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - foreach ($parent->getChildren() as $child) { |
|
| 133 | - // Search by value |
|
| 134 | - |
|
| 135 | - if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int)$value ) { |
|
| 136 | - $child->selected('selected'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Else iterate over subchilds |
|
| 140 | - if ($child->hasChildren()) { |
|
| 141 | - $this->selectValue($value, $child); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the Select's placeholder |
|
| 148 | - * |
|
| 149 | - * @return Element |
|
| 150 | - */ |
|
| 151 | - protected function getPlaceholder() |
|
| 152 | - { |
|
| 153 | - if (!$this->placeholder) { |
|
| 154 | - return false; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 158 | - if (!$this->value) { |
|
| 159 | - $attributes['selected'] = 'selected'; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return Element::create('option', $this->placeholder, $attributes); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - //////////////////////////////////////////////////////////////////// |
|
| 166 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 167 | - //////////////////////////////////////////////////////////////////// |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Set the select options |
|
| 171 | - * |
|
| 172 | - * @param array $_options The options as an array |
|
| 173 | - * @param mixed $selected Facultative selected entry |
|
| 174 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 175 | - * the option's values instead of the array's keys |
|
| 176 | - */ |
|
| 177 | - public function options($_options, $selected = null, $valuesAsKeys = false) |
|
| 178 | - { |
|
| 179 | - $options = array(); |
|
| 180 | - |
|
| 181 | - // If valuesAsKeys is true, use the values as keys |
|
| 182 | - if ($valuesAsKeys) { |
|
| 183 | - foreach ($_options as $v) { |
|
| 184 | - $options[$v] = $v; |
|
| 185 | - } |
|
| 186 | - } else { |
|
| 187 | - $options = $_options; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - // Add the various options |
|
| 191 | - foreach ($options as $value => $text) { |
|
| 192 | - if (is_array($text) and isset($text['value'])) { |
|
| 193 | - $attributes = $text; |
|
| 194 | - $text = $value; |
|
| 195 | - $value = null; |
|
| 196 | - } else { |
|
| 197 | - $attributes = array(); |
|
| 198 | - } |
|
| 199 | - $this->addOption($text, $value, $attributes); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // Set the selected value |
|
| 203 | - if (!is_null($selected)) { |
|
| 204 | - $this->select($selected); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $this; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Creates a list of options from a range |
|
| 212 | - * |
|
| 213 | - * @param integer $from |
|
| 214 | - * @param integer $to |
|
| 215 | - * @param integer $step |
|
| 216 | - */ |
|
| 217 | - public function range($from, $to, $step = 1) |
|
| 218 | - { |
|
| 219 | - $range = range($from, $to, $step); |
|
| 220 | - $this->options($range, null, true); |
|
| 221 | - |
|
| 222 | - return $this; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Add an option to the Select's options |
|
| 227 | - * |
|
| 228 | - * @param array|string $text It's value or an array of values |
|
| 229 | - * @param string $value It's text |
|
| 230 | - * @param array $attributes The option's attributes |
|
| 231 | - */ |
|
| 232 | - public function addOption($text = null, $value = null, $attributes = array()) |
|
| 233 | - { |
|
| 234 | - // Get the option's value |
|
| 235 | - $childrenKey = !is_null($value) ? $value : sizeof($this->children); |
|
| 236 | - |
|
| 237 | - // If we passed an options group |
|
| 238 | - if (is_array($text)) { |
|
| 239 | - $this->children[$childrenKey] = Element::create('optgroup')->label($value); |
|
| 240 | - foreach ($text as $key => $value) { |
|
| 241 | - $option = Element::create('option', $value)->setAttribute('value', $key); |
|
| 242 | - $this->children[$childrenKey]->nest($option); |
|
| 243 | - } |
|
| 244 | - // Else if it's a simple option |
|
| 245 | - } else { |
|
| 246 | - if (!isset($attributes['value'])) { |
|
| 247 | - $attributes['value'] = $value; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $this->children[$attributes['value']] = Element::create('option', $text)->setAttributes($attributes); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $this; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Use the results from a Fluent/Eloquent query as options |
|
| 258 | - * |
|
| 259 | - * @param array $results An array of Eloquent models |
|
| 260 | - * @param string|function $text The value to use as text |
|
| 261 | - * @param string|array $attributes The data to use as attributes |
|
| 262 | - * @param string $selected The default selected item |
|
| 263 | - */ |
|
| 264 | - public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 265 | - { |
|
| 266 | - $this->options(Helpers::queryToArray($results, $text, $attributes), $selected); |
|
| 267 | - |
|
| 268 | - return $this; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Select a particular list item |
|
| 273 | - * |
|
| 274 | - * @param mixed $selected Selected item |
|
| 275 | - */ |
|
| 276 | - public function select($selected) |
|
| 277 | - { |
|
| 278 | - $this->value = $selected; |
|
| 279 | - |
|
| 280 | - return $this; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Add a placeholder to the current select |
|
| 285 | - * |
|
| 286 | - * @param string $placeholder The placeholder text |
|
| 287 | - */ |
|
| 288 | - public function placeholder($placeholder) |
|
| 289 | - { |
|
| 290 | - $this->placeholder = Helpers::translate($placeholder); |
|
| 291 | - |
|
| 292 | - return $this; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - //////////////////////////////////////////////////////////////////// |
|
| 296 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 297 | - //////////////////////////////////////////////////////////////////// |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Returns the current options in memory for manipulations |
|
| 301 | - * |
|
| 302 | - * @return array The current options array |
|
| 303 | - */ |
|
| 304 | - public function getOptions() |
|
| 305 | - { |
|
| 306 | - return $this->children; |
|
| 307 | - } |
|
| 15 | + /** |
|
| 16 | + * The select's placeholder |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + private $placeholder = null; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The Select's options |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + protected $options; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The select's element |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $element = 'select'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The select's self-closing state |
|
| 38 | + * |
|
| 39 | + * @var boolean |
|
| 40 | + */ |
|
| 41 | + protected $isSelfClosing = false; |
|
| 42 | + |
|
| 43 | + //////////////////////////////////////////////////////////////////// |
|
| 44 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 45 | + //////////////////////////////////////////////////////////////////// |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Easier arguments order for selects |
|
| 49 | + * |
|
| 50 | + * @param Container $app The Container instance |
|
| 51 | + * @param string $type select |
|
| 52 | + * @param string $name Field name |
|
| 53 | + * @param string $label Its label |
|
| 54 | + * @param array $options The select's options |
|
| 55 | + * @param string $selected The selected option |
|
| 56 | + * @param array $attributes Attributes |
|
| 57 | + */ |
|
| 58 | + public function __construct(Container $app, $type, $name, $label, $options, $selected, $attributes) |
|
| 59 | + { |
|
| 60 | + if ($selected) { |
|
| 61 | + $this->value = $selected; |
|
| 62 | + } |
|
| 63 | + if ($options) { |
|
| 64 | + $this->options($options); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 68 | + |
|
| 69 | + // Nested models population |
|
| 70 | + if (str_contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 71 | + $this->fromQuery($this->value); |
|
| 72 | + $this->value = $selected ?: null; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Renders the select |
|
| 78 | + * |
|
| 79 | + * @return string A <select> tag |
|
| 80 | + */ |
|
| 81 | + public function render() |
|
| 82 | + { |
|
| 83 | + // Multiselects |
|
| 84 | + if ($this->isOfType('multiselect')) { |
|
| 85 | + if (!isset($this->attributes['id'])) { |
|
| 86 | + $this->setAttribute('id', $this->name); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $this->multiple(); |
|
| 90 | + $this->name .= '[]'; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + if ( ! $this->value instanceOf \ArrayAccess) { |
|
| 94 | + $this->value = (array) $this->value; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Mark selected values as selected |
|
| 98 | + if ($this->hasChildren() and !empty($this->value)) { |
|
| 99 | + foreach ($this->value as $value) { |
|
| 100 | + if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 101 | + $value = $value->getKey(); |
|
| 102 | + } |
|
| 103 | + $this->selectValue($value); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // Add placeholder text if any |
|
| 108 | + if ($placeholder = $this->getPlaceholder()) { |
|
| 109 | + array_unshift($this->children, $placeholder); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $this->value = null; |
|
| 113 | + |
|
| 114 | + return parent::render(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Select a value in the field's children |
|
| 119 | + * |
|
| 120 | + * @param mixed $value |
|
| 121 | + * @param Element $parent |
|
| 122 | + * |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + protected function selectValue($value, $parent = null) |
|
| 126 | + { |
|
| 127 | + // If no parent element defined, use direct children |
|
| 128 | + if (!$parent) { |
|
| 129 | + $parent = $this; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + foreach ($parent->getChildren() as $child) { |
|
| 133 | + // Search by value |
|
| 134 | + |
|
| 135 | + if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int)$value ) { |
|
| 136 | + $child->selected('selected'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Else iterate over subchilds |
|
| 140 | + if ($child->hasChildren()) { |
|
| 141 | + $this->selectValue($value, $child); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the Select's placeholder |
|
| 148 | + * |
|
| 149 | + * @return Element |
|
| 150 | + */ |
|
| 151 | + protected function getPlaceholder() |
|
| 152 | + { |
|
| 153 | + if (!$this->placeholder) { |
|
| 154 | + return false; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 158 | + if (!$this->value) { |
|
| 159 | + $attributes['selected'] = 'selected'; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return Element::create('option', $this->placeholder, $attributes); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + //////////////////////////////////////////////////////////////////// |
|
| 166 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 167 | + //////////////////////////////////////////////////////////////////// |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Set the select options |
|
| 171 | + * |
|
| 172 | + * @param array $_options The options as an array |
|
| 173 | + * @param mixed $selected Facultative selected entry |
|
| 174 | + * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 175 | + * the option's values instead of the array's keys |
|
| 176 | + */ |
|
| 177 | + public function options($_options, $selected = null, $valuesAsKeys = false) |
|
| 178 | + { |
|
| 179 | + $options = array(); |
|
| 180 | + |
|
| 181 | + // If valuesAsKeys is true, use the values as keys |
|
| 182 | + if ($valuesAsKeys) { |
|
| 183 | + foreach ($_options as $v) { |
|
| 184 | + $options[$v] = $v; |
|
| 185 | + } |
|
| 186 | + } else { |
|
| 187 | + $options = $_options; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + // Add the various options |
|
| 191 | + foreach ($options as $value => $text) { |
|
| 192 | + if (is_array($text) and isset($text['value'])) { |
|
| 193 | + $attributes = $text; |
|
| 194 | + $text = $value; |
|
| 195 | + $value = null; |
|
| 196 | + } else { |
|
| 197 | + $attributes = array(); |
|
| 198 | + } |
|
| 199 | + $this->addOption($text, $value, $attributes); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // Set the selected value |
|
| 203 | + if (!is_null($selected)) { |
|
| 204 | + $this->select($selected); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $this; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Creates a list of options from a range |
|
| 212 | + * |
|
| 213 | + * @param integer $from |
|
| 214 | + * @param integer $to |
|
| 215 | + * @param integer $step |
|
| 216 | + */ |
|
| 217 | + public function range($from, $to, $step = 1) |
|
| 218 | + { |
|
| 219 | + $range = range($from, $to, $step); |
|
| 220 | + $this->options($range, null, true); |
|
| 221 | + |
|
| 222 | + return $this; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Add an option to the Select's options |
|
| 227 | + * |
|
| 228 | + * @param array|string $text It's value or an array of values |
|
| 229 | + * @param string $value It's text |
|
| 230 | + * @param array $attributes The option's attributes |
|
| 231 | + */ |
|
| 232 | + public function addOption($text = null, $value = null, $attributes = array()) |
|
| 233 | + { |
|
| 234 | + // Get the option's value |
|
| 235 | + $childrenKey = !is_null($value) ? $value : sizeof($this->children); |
|
| 236 | + |
|
| 237 | + // If we passed an options group |
|
| 238 | + if (is_array($text)) { |
|
| 239 | + $this->children[$childrenKey] = Element::create('optgroup')->label($value); |
|
| 240 | + foreach ($text as $key => $value) { |
|
| 241 | + $option = Element::create('option', $value)->setAttribute('value', $key); |
|
| 242 | + $this->children[$childrenKey]->nest($option); |
|
| 243 | + } |
|
| 244 | + // Else if it's a simple option |
|
| 245 | + } else { |
|
| 246 | + if (!isset($attributes['value'])) { |
|
| 247 | + $attributes['value'] = $value; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $this->children[$attributes['value']] = Element::create('option', $text)->setAttributes($attributes); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $this; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Use the results from a Fluent/Eloquent query as options |
|
| 258 | + * |
|
| 259 | + * @param array $results An array of Eloquent models |
|
| 260 | + * @param string|function $text The value to use as text |
|
| 261 | + * @param string|array $attributes The data to use as attributes |
|
| 262 | + * @param string $selected The default selected item |
|
| 263 | + */ |
|
| 264 | + public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 265 | + { |
|
| 266 | + $this->options(Helpers::queryToArray($results, $text, $attributes), $selected); |
|
| 267 | + |
|
| 268 | + return $this; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Select a particular list item |
|
| 273 | + * |
|
| 274 | + * @param mixed $selected Selected item |
|
| 275 | + */ |
|
| 276 | + public function select($selected) |
|
| 277 | + { |
|
| 278 | + $this->value = $selected; |
|
| 279 | + |
|
| 280 | + return $this; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Add a placeholder to the current select |
|
| 285 | + * |
|
| 286 | + * @param string $placeholder The placeholder text |
|
| 287 | + */ |
|
| 288 | + public function placeholder($placeholder) |
|
| 289 | + { |
|
| 290 | + $this->placeholder = Helpers::translate($placeholder); |
|
| 291 | + |
|
| 292 | + return $this; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + //////////////////////////////////////////////////////////////////// |
|
| 296 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 297 | + //////////////////////////////////////////////////////////////////// |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Returns the current options in memory for manipulations |
|
| 301 | + * |
|
| 302 | + * @return array The current options array |
|
| 303 | + */ |
|
| 304 | + public function getOptions() |
|
| 305 | + { |
|
| 306 | + return $this->children; |
|
| 307 | + } |
|
| 308 | 308 | } |
@@ -351,9 +351,9 @@ |
||
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | /** |
| 354 | - * @param $name |
|
| 354 | + * @param string $name |
|
| 355 | 355 | * @param $params |
| 356 | - * @param $type |
|
| 356 | + * @param string $type |
|
| 357 | 357 | * |
| 358 | 358 | * @return $this |
| 359 | 359 | */ |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | $this->url = $url; |
| 97 | 97 | $this->populator = $populator; |
| 98 | 98 | |
| 99 | - $this->app->singleton('former.form.framework', function ($app) { |
|
| 99 | + $this->app->singleton('former.form.framework', function($app) { |
|
| 100 | 100 | return clone $app['former.framework']; |
| 101 | 101 | }); |
| 102 | 102 | } |
@@ -402,7 +402,7 @@ discard block |
||
| 402 | 402 | |
| 403 | 403 | // If raw form |
| 404 | 404 | if ($type == 'raw') { |
| 405 | - $this->app->bind('former.form.framework', function ($app) { |
|
| 405 | + $this->app->bind('former.form.framework', function($app) { |
|
| 406 | 406 | return $app['former']->getFrameworkInstance('Nude'); |
| 407 | 407 | }); |
| 408 | 408 | } |
@@ -11,407 +11,407 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Form extends FormerObject |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * The IoC Container |
|
| 16 | - * |
|
| 17 | - * @var Container |
|
| 18 | - */ |
|
| 19 | - protected $app; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The URL generator |
|
| 23 | - * |
|
| 24 | - * @var UrlGenerator |
|
| 25 | - */ |
|
| 26 | - protected $url; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The Populator |
|
| 30 | - * |
|
| 31 | - * @var Populator |
|
| 32 | - */ |
|
| 33 | - protected $populator; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The Form type |
|
| 37 | - * |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - protected $type = null; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The destination of the current form |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $action; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The form method |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - protected $method; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Whether the form should be secured or not |
|
| 58 | - * |
|
| 59 | - * @var boolean |
|
| 60 | - */ |
|
| 61 | - protected $secure; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * The form element |
|
| 65 | - * |
|
| 66 | - * @var string |
|
| 67 | - */ |
|
| 68 | - protected $element = 'form'; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * A list of injected properties |
|
| 72 | - * |
|
| 73 | - * @var array |
|
| 74 | - */ |
|
| 75 | - protected $injectedProperties = array('method', 'action'); |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Whether a form is opened or not |
|
| 79 | - * |
|
| 80 | - * @var boolean |
|
| 81 | - */ |
|
| 82 | - protected static $opened = false; |
|
| 83 | - |
|
| 84 | - //////////////////////////////////////////////////////////////////// |
|
| 85 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 86 | - //////////////////////////////////////////////////////////////////// |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Build a new Form instance |
|
| 90 | - * |
|
| 91 | - * @param UrlGenerator $url |
|
| 92 | - */ |
|
| 93 | - public function __construct(Container $app, $url, Populator $populator) |
|
| 94 | - { |
|
| 95 | - $this->app = $app; |
|
| 96 | - $this->url = $url; |
|
| 97 | - $this->populator = $populator; |
|
| 98 | - |
|
| 99 | - $this->app->singleton('former.form.framework', function ($app) { |
|
| 100 | - return clone $app['former.framework']; |
|
| 101 | - }); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Opens up magically a form |
|
| 106 | - * |
|
| 107 | - * @param string $type The form type asked |
|
| 108 | - * @param array $parameters Parameters passed |
|
| 109 | - * |
|
| 110 | - * @return Form A form opening tag |
|
| 111 | - */ |
|
| 112 | - public function openForm($type, $parameters) |
|
| 113 | - { |
|
| 114 | - $action = array_get($parameters, 0); |
|
| 115 | - $method = array_get($parameters, 1, 'POST'); |
|
| 116 | - $attributes = array_get($parameters, 2, array()); |
|
| 117 | - $secure = array_get($parameters, 3, null); |
|
| 118 | - |
|
| 119 | - // Fetch errors if asked for |
|
| 120 | - if ($this->app['former']->getOption('fetch_errors')) { |
|
| 121 | - $this->app['former']->withErrors(); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - // Open the form |
|
| 125 | - $this->action($action); |
|
| 126 | - $this->attributes = $attributes; |
|
| 127 | - $this->method = strtoupper($method); |
|
| 128 | - $this->secure = $secure; |
|
| 129 | - |
|
| 130 | - // Add any effect of the form type |
|
| 131 | - $type = Str::snake($type); |
|
| 132 | - $this->type = $this->applyType($type); |
|
| 133 | - |
|
| 134 | - // Add enctype |
|
| 135 | - if (!array_key_exists('accept-charset', $attributes)) { |
|
| 136 | - $this->attributes['accept-charset'] = 'utf-8'; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Add supplementary classes |
|
| 140 | - if ($this->type !== 'raw') { |
|
| 141 | - $this->addClass($this->app['former.form.framework']->getFormClasses($this->type)); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return $this; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Closes a Form |
|
| 149 | - * |
|
| 150 | - * @return string A closing <form> tag |
|
| 151 | - */ |
|
| 152 | - public function close() |
|
| 153 | - { |
|
| 154 | - static::$opened = false; |
|
| 155 | - |
|
| 156 | - // Add token if necessary |
|
| 157 | - $closing = '</form>'; |
|
| 158 | - if ($this->method != 'GET') { |
|
| 159 | - $closing = $this->app['former']->token().$closing; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return $closing; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - //////////////////////////////////////////////////////////////////// |
|
| 166 | - //////////////////////////// STATIC HELPERS //////////////////////// |
|
| 167 | - //////////////////////////////////////////////////////////////////// |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Whether a form is currently opened or not |
|
| 171 | - * |
|
| 172 | - * @return boolean |
|
| 173 | - */ |
|
| 174 | - public static function hasInstanceOpened() |
|
| 175 | - { |
|
| 176 | - return static::$opened; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - //////////////////////////////////////////////////////////////////// |
|
| 180 | - /////////////////////////////// SETTER ///////////////////////////// |
|
| 181 | - //////////////////////////////////////////////////////////////////// |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Change the form's action |
|
| 185 | - * |
|
| 186 | - * @param string $action The new action |
|
| 187 | - * |
|
| 188 | - * @return $this |
|
| 189 | - */ |
|
| 190 | - public function action($action) |
|
| 191 | - { |
|
| 192 | - $this->action = $action ? $this->url->to($action, array(), $this->secure) : null; |
|
| 193 | - |
|
| 194 | - return $this; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Change the form's method |
|
| 199 | - * |
|
| 200 | - * @param string $method The method to use |
|
| 201 | - * |
|
| 202 | - * @return $this |
|
| 203 | - */ |
|
| 204 | - public function method($method) |
|
| 205 | - { |
|
| 206 | - $this->method = strtoupper($method); |
|
| 207 | - |
|
| 208 | - return $this; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Whether the form should be secure |
|
| 213 | - * |
|
| 214 | - * @param boolean $secure Secure or not |
|
| 215 | - * |
|
| 216 | - * @return $this |
|
| 217 | - */ |
|
| 218 | - public function secure($secure = true) |
|
| 219 | - { |
|
| 220 | - $this->secure = $secure; |
|
| 221 | - |
|
| 222 | - return $this; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Change the form's action and method to a route |
|
| 227 | - * |
|
| 228 | - * @param string $name The name of the route to use |
|
| 229 | - * @param array $params Any route parameters |
|
| 230 | - * |
|
| 231 | - * @return Form |
|
| 232 | - */ |
|
| 233 | - public function route($name, $params = array()) |
|
| 234 | - { |
|
| 235 | - return $this->setRouteOrAction($name, $params, 'route'); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Change the form's action to a controller method |
|
| 240 | - * |
|
| 241 | - * @param string $name The controller and method |
|
| 242 | - * @param array $params Any method parameters |
|
| 243 | - * |
|
| 244 | - * @return Form |
|
| 245 | - */ |
|
| 246 | - public function controller($name, $params = array()) |
|
| 247 | - { |
|
| 248 | - return $this->setRouteOrAction($name, $params, 'action'); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Outputs the current form opened |
|
| 253 | - * |
|
| 254 | - * @return string A <form> opening tag |
|
| 255 | - */ |
|
| 256 | - public function __toString() |
|
| 257 | - { |
|
| 258 | - // Mark the form as opened |
|
| 259 | - static::$opened = true; |
|
| 260 | - |
|
| 261 | - // Add name to attributes |
|
| 262 | - $this->attributes['name'] = $this->name; |
|
| 263 | - |
|
| 264 | - // Add spoof method |
|
| 265 | - if (in_array($this->method, array('PUT', 'PATCH', 'DELETE'))) { |
|
| 266 | - $spoof = $this->app['former']->hidden('_method', $this->method); |
|
| 267 | - $this->method = 'POST'; |
|
| 268 | - } else { |
|
| 269 | - $spoof = null; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return $this->open().$spoof; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - //////////////////////////////////////////////////////////////////// |
|
| 276 | - ////////////////////////// PUBLIC HELPERS ////////////////////////// |
|
| 277 | - //////////////////////////////////////////////////////////////////// |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Alias for $this->app['former']->withRules |
|
| 281 | - */ |
|
| 282 | - public function rules() |
|
| 283 | - { |
|
| 284 | - call_user_func_array(array($this->app['former'], 'withRules'), func_get_args()); |
|
| 285 | - |
|
| 286 | - return $this; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Populate a form with specific values |
|
| 291 | - * |
|
| 292 | - * @param array|object $values |
|
| 293 | - * |
|
| 294 | - * @return $this |
|
| 295 | - */ |
|
| 296 | - public function populate($values) |
|
| 297 | - { |
|
| 298 | - $this->populator->replace($values); |
|
| 299 | - |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Get the Populator binded to the Form |
|
| 305 | - * |
|
| 306 | - * @return Populator |
|
| 307 | - */ |
|
| 308 | - public function getPopulator() |
|
| 309 | - { |
|
| 310 | - return $this->populator; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - //////////////////////////////////////////////////////////////////// |
|
| 314 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 315 | - //////////////////////////////////////////////////////////////////// |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Find the method of a route by its _uses or name |
|
| 319 | - * |
|
| 320 | - * @param string $name |
|
| 321 | - * |
|
| 322 | - * @return string |
|
| 323 | - */ |
|
| 324 | - protected function findRouteMethod($name) |
|
| 325 | - { |
|
| 326 | - if (!$this->app->bound('router')) { |
|
| 327 | - return; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - // Get string by name |
|
| 331 | - if (!Str::contains($name, '@')) { |
|
| 332 | - $routes = $this->app['router']->getRoutes(); |
|
| 333 | - $route = method_exists($routes, 'getByName') ? $routes->getByName($name) : $routes->get($name); |
|
| 334 | - // Get string by uses |
|
| 335 | - } else { |
|
| 336 | - foreach ($this->app['router']->getRoutes() as $route) { |
|
| 337 | - $routeUses = method_exists($route, 'getOption') ? $route->getOption('_uses') : array_get($route->getAction(), 'controller'); |
|
| 338 | - if ($action = $routeUses) { |
|
| 339 | - if ($action == $name) { |
|
| 340 | - break; |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - // Get method |
|
| 347 | - $methods = method_exists($route, 'getMethods') ? $route->getMethods() : $route->methods(); |
|
| 348 | - $method = array_get($methods, 0); |
|
| 349 | - |
|
| 350 | - return $method; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @param $name |
|
| 355 | - * @param $params |
|
| 356 | - * @param $type |
|
| 357 | - * |
|
| 358 | - * @return $this |
|
| 359 | - */ |
|
| 360 | - protected function setRouteOrAction($name, $params, $type) |
|
| 361 | - { |
|
| 362 | - // Set the form action |
|
| 363 | - $this->action = $this->url->$type($name, $params); |
|
| 364 | - |
|
| 365 | - // Set the proper method |
|
| 366 | - if ($method = $this->findRouteMethod($name)) { |
|
| 367 | - $this->method($method); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return $this; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Apply various parameters according to form type |
|
| 375 | - * |
|
| 376 | - * @param string $type The original form type provided |
|
| 377 | - * |
|
| 378 | - * @return string The final form type |
|
| 379 | - */ |
|
| 380 | - private function applyType($type) |
|
| 381 | - { |
|
| 382 | - // If classic form |
|
| 383 | - if ($type == 'open') { |
|
| 384 | - return $this->app['former']->getOption('default_form_type'); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - // Look for HTTPS form |
|
| 388 | - if (Str::contains($type, 'secure')) { |
|
| 389 | - $type = str_replace('secure', '', $type); |
|
| 390 | - $this->secure = true; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - // Look for file form |
|
| 394 | - if (Str::contains($type, 'for_files')) { |
|
| 395 | - $type = str_replace('for_files', '', $type); |
|
| 396 | - $this->attributes['enctype'] = 'multipart/form-data'; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - // Calculate form type |
|
| 400 | - $type = str_replace('open', '', $type); |
|
| 401 | - $type = trim($type, '_'); |
|
| 402 | - |
|
| 403 | - // If raw form |
|
| 404 | - if ($type == 'raw') { |
|
| 405 | - $this->app->bind('former.form.framework', function ($app) { |
|
| 406 | - return $app['former']->getFrameworkInstance('Nude'); |
|
| 407 | - }); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - // Use default form type if the one provided is invalid |
|
| 411 | - if ($type !== 'raw' and !in_array($type, $this->app['former.form.framework']->availableTypes())) { |
|
| 412 | - $type = $this->app['former']->getOption('default_form_type'); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - return $type; |
|
| 416 | - } |
|
| 14 | + /** |
|
| 15 | + * The IoC Container |
|
| 16 | + * |
|
| 17 | + * @var Container |
|
| 18 | + */ |
|
| 19 | + protected $app; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The URL generator |
|
| 23 | + * |
|
| 24 | + * @var UrlGenerator |
|
| 25 | + */ |
|
| 26 | + protected $url; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The Populator |
|
| 30 | + * |
|
| 31 | + * @var Populator |
|
| 32 | + */ |
|
| 33 | + protected $populator; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The Form type |
|
| 37 | + * |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + protected $type = null; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The destination of the current form |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $action; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The form method |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + protected $method; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Whether the form should be secured or not |
|
| 58 | + * |
|
| 59 | + * @var boolean |
|
| 60 | + */ |
|
| 61 | + protected $secure; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * The form element |
|
| 65 | + * |
|
| 66 | + * @var string |
|
| 67 | + */ |
|
| 68 | + protected $element = 'form'; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * A list of injected properties |
|
| 72 | + * |
|
| 73 | + * @var array |
|
| 74 | + */ |
|
| 75 | + protected $injectedProperties = array('method', 'action'); |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Whether a form is opened or not |
|
| 79 | + * |
|
| 80 | + * @var boolean |
|
| 81 | + */ |
|
| 82 | + protected static $opened = false; |
|
| 83 | + |
|
| 84 | + //////////////////////////////////////////////////////////////////// |
|
| 85 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 86 | + //////////////////////////////////////////////////////////////////// |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Build a new Form instance |
|
| 90 | + * |
|
| 91 | + * @param UrlGenerator $url |
|
| 92 | + */ |
|
| 93 | + public function __construct(Container $app, $url, Populator $populator) |
|
| 94 | + { |
|
| 95 | + $this->app = $app; |
|
| 96 | + $this->url = $url; |
|
| 97 | + $this->populator = $populator; |
|
| 98 | + |
|
| 99 | + $this->app->singleton('former.form.framework', function ($app) { |
|
| 100 | + return clone $app['former.framework']; |
|
| 101 | + }); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Opens up magically a form |
|
| 106 | + * |
|
| 107 | + * @param string $type The form type asked |
|
| 108 | + * @param array $parameters Parameters passed |
|
| 109 | + * |
|
| 110 | + * @return Form A form opening tag |
|
| 111 | + */ |
|
| 112 | + public function openForm($type, $parameters) |
|
| 113 | + { |
|
| 114 | + $action = array_get($parameters, 0); |
|
| 115 | + $method = array_get($parameters, 1, 'POST'); |
|
| 116 | + $attributes = array_get($parameters, 2, array()); |
|
| 117 | + $secure = array_get($parameters, 3, null); |
|
| 118 | + |
|
| 119 | + // Fetch errors if asked for |
|
| 120 | + if ($this->app['former']->getOption('fetch_errors')) { |
|
| 121 | + $this->app['former']->withErrors(); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + // Open the form |
|
| 125 | + $this->action($action); |
|
| 126 | + $this->attributes = $attributes; |
|
| 127 | + $this->method = strtoupper($method); |
|
| 128 | + $this->secure = $secure; |
|
| 129 | + |
|
| 130 | + // Add any effect of the form type |
|
| 131 | + $type = Str::snake($type); |
|
| 132 | + $this->type = $this->applyType($type); |
|
| 133 | + |
|
| 134 | + // Add enctype |
|
| 135 | + if (!array_key_exists('accept-charset', $attributes)) { |
|
| 136 | + $this->attributes['accept-charset'] = 'utf-8'; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Add supplementary classes |
|
| 140 | + if ($this->type !== 'raw') { |
|
| 141 | + $this->addClass($this->app['former.form.framework']->getFormClasses($this->type)); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return $this; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Closes a Form |
|
| 149 | + * |
|
| 150 | + * @return string A closing <form> tag |
|
| 151 | + */ |
|
| 152 | + public function close() |
|
| 153 | + { |
|
| 154 | + static::$opened = false; |
|
| 155 | + |
|
| 156 | + // Add token if necessary |
|
| 157 | + $closing = '</form>'; |
|
| 158 | + if ($this->method != 'GET') { |
|
| 159 | + $closing = $this->app['former']->token().$closing; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return $closing; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + //////////////////////////////////////////////////////////////////// |
|
| 166 | + //////////////////////////// STATIC HELPERS //////////////////////// |
|
| 167 | + //////////////////////////////////////////////////////////////////// |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Whether a form is currently opened or not |
|
| 171 | + * |
|
| 172 | + * @return boolean |
|
| 173 | + */ |
|
| 174 | + public static function hasInstanceOpened() |
|
| 175 | + { |
|
| 176 | + return static::$opened; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + //////////////////////////////////////////////////////////////////// |
|
| 180 | + /////////////////////////////// SETTER ///////////////////////////// |
|
| 181 | + //////////////////////////////////////////////////////////////////// |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Change the form's action |
|
| 185 | + * |
|
| 186 | + * @param string $action The new action |
|
| 187 | + * |
|
| 188 | + * @return $this |
|
| 189 | + */ |
|
| 190 | + public function action($action) |
|
| 191 | + { |
|
| 192 | + $this->action = $action ? $this->url->to($action, array(), $this->secure) : null; |
|
| 193 | + |
|
| 194 | + return $this; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Change the form's method |
|
| 199 | + * |
|
| 200 | + * @param string $method The method to use |
|
| 201 | + * |
|
| 202 | + * @return $this |
|
| 203 | + */ |
|
| 204 | + public function method($method) |
|
| 205 | + { |
|
| 206 | + $this->method = strtoupper($method); |
|
| 207 | + |
|
| 208 | + return $this; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Whether the form should be secure |
|
| 213 | + * |
|
| 214 | + * @param boolean $secure Secure or not |
|
| 215 | + * |
|
| 216 | + * @return $this |
|
| 217 | + */ |
|
| 218 | + public function secure($secure = true) |
|
| 219 | + { |
|
| 220 | + $this->secure = $secure; |
|
| 221 | + |
|
| 222 | + return $this; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Change the form's action and method to a route |
|
| 227 | + * |
|
| 228 | + * @param string $name The name of the route to use |
|
| 229 | + * @param array $params Any route parameters |
|
| 230 | + * |
|
| 231 | + * @return Form |
|
| 232 | + */ |
|
| 233 | + public function route($name, $params = array()) |
|
| 234 | + { |
|
| 235 | + return $this->setRouteOrAction($name, $params, 'route'); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Change the form's action to a controller method |
|
| 240 | + * |
|
| 241 | + * @param string $name The controller and method |
|
| 242 | + * @param array $params Any method parameters |
|
| 243 | + * |
|
| 244 | + * @return Form |
|
| 245 | + */ |
|
| 246 | + public function controller($name, $params = array()) |
|
| 247 | + { |
|
| 248 | + return $this->setRouteOrAction($name, $params, 'action'); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Outputs the current form opened |
|
| 253 | + * |
|
| 254 | + * @return string A <form> opening tag |
|
| 255 | + */ |
|
| 256 | + public function __toString() |
|
| 257 | + { |
|
| 258 | + // Mark the form as opened |
|
| 259 | + static::$opened = true; |
|
| 260 | + |
|
| 261 | + // Add name to attributes |
|
| 262 | + $this->attributes['name'] = $this->name; |
|
| 263 | + |
|
| 264 | + // Add spoof method |
|
| 265 | + if (in_array($this->method, array('PUT', 'PATCH', 'DELETE'))) { |
|
| 266 | + $spoof = $this->app['former']->hidden('_method', $this->method); |
|
| 267 | + $this->method = 'POST'; |
|
| 268 | + } else { |
|
| 269 | + $spoof = null; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return $this->open().$spoof; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + //////////////////////////////////////////////////////////////////// |
|
| 276 | + ////////////////////////// PUBLIC HELPERS ////////////////////////// |
|
| 277 | + //////////////////////////////////////////////////////////////////// |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Alias for $this->app['former']->withRules |
|
| 281 | + */ |
|
| 282 | + public function rules() |
|
| 283 | + { |
|
| 284 | + call_user_func_array(array($this->app['former'], 'withRules'), func_get_args()); |
|
| 285 | + |
|
| 286 | + return $this; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Populate a form with specific values |
|
| 291 | + * |
|
| 292 | + * @param array|object $values |
|
| 293 | + * |
|
| 294 | + * @return $this |
|
| 295 | + */ |
|
| 296 | + public function populate($values) |
|
| 297 | + { |
|
| 298 | + $this->populator->replace($values); |
|
| 299 | + |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Get the Populator binded to the Form |
|
| 305 | + * |
|
| 306 | + * @return Populator |
|
| 307 | + */ |
|
| 308 | + public function getPopulator() |
|
| 309 | + { |
|
| 310 | + return $this->populator; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + //////////////////////////////////////////////////////////////////// |
|
| 314 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 315 | + //////////////////////////////////////////////////////////////////// |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Find the method of a route by its _uses or name |
|
| 319 | + * |
|
| 320 | + * @param string $name |
|
| 321 | + * |
|
| 322 | + * @return string |
|
| 323 | + */ |
|
| 324 | + protected function findRouteMethod($name) |
|
| 325 | + { |
|
| 326 | + if (!$this->app->bound('router')) { |
|
| 327 | + return; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + // Get string by name |
|
| 331 | + if (!Str::contains($name, '@')) { |
|
| 332 | + $routes = $this->app['router']->getRoutes(); |
|
| 333 | + $route = method_exists($routes, 'getByName') ? $routes->getByName($name) : $routes->get($name); |
|
| 334 | + // Get string by uses |
|
| 335 | + } else { |
|
| 336 | + foreach ($this->app['router']->getRoutes() as $route) { |
|
| 337 | + $routeUses = method_exists($route, 'getOption') ? $route->getOption('_uses') : array_get($route->getAction(), 'controller'); |
|
| 338 | + if ($action = $routeUses) { |
|
| 339 | + if ($action == $name) { |
|
| 340 | + break; |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + // Get method |
|
| 347 | + $methods = method_exists($route, 'getMethods') ? $route->getMethods() : $route->methods(); |
|
| 348 | + $method = array_get($methods, 0); |
|
| 349 | + |
|
| 350 | + return $method; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @param $name |
|
| 355 | + * @param $params |
|
| 356 | + * @param $type |
|
| 357 | + * |
|
| 358 | + * @return $this |
|
| 359 | + */ |
|
| 360 | + protected function setRouteOrAction($name, $params, $type) |
|
| 361 | + { |
|
| 362 | + // Set the form action |
|
| 363 | + $this->action = $this->url->$type($name, $params); |
|
| 364 | + |
|
| 365 | + // Set the proper method |
|
| 366 | + if ($method = $this->findRouteMethod($name)) { |
|
| 367 | + $this->method($method); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return $this; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Apply various parameters according to form type |
|
| 375 | + * |
|
| 376 | + * @param string $type The original form type provided |
|
| 377 | + * |
|
| 378 | + * @return string The final form type |
|
| 379 | + */ |
|
| 380 | + private function applyType($type) |
|
| 381 | + { |
|
| 382 | + // If classic form |
|
| 383 | + if ($type == 'open') { |
|
| 384 | + return $this->app['former']->getOption('default_form_type'); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + // Look for HTTPS form |
|
| 388 | + if (Str::contains($type, 'secure')) { |
|
| 389 | + $type = str_replace('secure', '', $type); |
|
| 390 | + $this->secure = true; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + // Look for file form |
|
| 394 | + if (Str::contains($type, 'for_files')) { |
|
| 395 | + $type = str_replace('for_files', '', $type); |
|
| 396 | + $this->attributes['enctype'] = 'multipart/form-data'; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + // Calculate form type |
|
| 400 | + $type = str_replace('open', '', $type); |
|
| 401 | + $type = trim($type, '_'); |
|
| 402 | + |
|
| 403 | + // If raw form |
|
| 404 | + if ($type == 'raw') { |
|
| 405 | + $this->app->bind('former.form.framework', function ($app) { |
|
| 406 | + return $app['former']->getFrameworkInstance('Nude'); |
|
| 407 | + }); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + // Use default form type if the one provided is invalid |
|
| 411 | + if ($type !== 'raw' and !in_array($type, $this->app['former.form.framework']->availableTypes())) { |
|
| 412 | + $type = $this->app['former']->getOption('default_form_type'); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + return $type; |
|
| 416 | + } |
|
| 417 | 417 | } |
@@ -442,7 +442,7 @@ |
||
| 442 | 442 | /** |
| 443 | 443 | * Format the field with prepended/appended elements |
| 444 | 444 | * |
| 445 | - * @param Field $field The field to format |
|
| 445 | + * @param \Former\Traits\Field $field The field to format |
|
| 446 | 446 | * |
| 447 | 447 | * @return string Field plus supplementary elements |
| 448 | 448 | */ |
@@ -12,461 +12,461 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Group extends Tag |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * The Container |
|
| 17 | - * |
|
| 18 | - * @var Container |
|
| 19 | - */ |
|
| 20 | - protected $app; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The current state of the group |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $state = null; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Whether the field should be displayed raw or not |
|
| 31 | - * |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 34 | - protected $raw = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The group label |
|
| 38 | - * |
|
| 39 | - * @var Element |
|
| 40 | - */ |
|
| 41 | - protected $label; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The group help |
|
| 45 | - * |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 48 | - protected $help = array(); |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * An array of elements to preprend the field |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - protected $prepend = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * An array of elements to append the field |
|
| 59 | - * |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - protected $append = array(); |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The field validations to be checked for errors |
|
| 66 | - * |
|
| 67 | - * @var array |
|
| 68 | - */ |
|
| 69 | - protected $validations = array(); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The group's element |
|
| 73 | - * |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - protected $element = 'div'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Whether a custom group is opened or not |
|
| 80 | - * |
|
| 81 | - * @var boolean |
|
| 82 | - */ |
|
| 83 | - public static $opened = false; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * The custom group that is open |
|
| 87 | - * |
|
| 88 | - * @var Former\Form\Group |
|
| 89 | - */ |
|
| 90 | - public static $openGroup = null; |
|
| 91 | - |
|
| 92 | - //////////////////////////////////////////////////////////////////// |
|
| 93 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 94 | - //////////////////////////////////////////////////////////////////// |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Creates a group |
|
| 98 | - * |
|
| 99 | - * @param string $label Its label |
|
| 100 | - */ |
|
| 101 | - public function __construct(Container $app, $label, $validations = null) |
|
| 102 | - { |
|
| 103 | - // Get special classes |
|
| 104 | - $this->app = $app; |
|
| 105 | - $this->addClass($this->app['former.framework']->getGroupClasses()); |
|
| 106 | - |
|
| 107 | - // Invisible if Nude |
|
| 108 | - if ($this->app['former.framework']->is('Nude')) { |
|
| 109 | - $this->element = ''; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // Set group label |
|
| 113 | - if ($label) { |
|
| 114 | - $this->setLabel($label); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - // Set validations used to override groups own conclusions |
|
| 118 | - $this->validations = (array) $validations; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Prints out the opening of the Control Group |
|
| 123 | - * |
|
| 124 | - * @return string A control group opening tag |
|
| 125 | - */ |
|
| 126 | - public function __toString() |
|
| 127 | - { |
|
| 128 | - return $this->open().$this->getFormattedLabel(); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Opens a group |
|
| 133 | - * |
|
| 134 | - * @return string Opening tag |
|
| 135 | - */ |
|
| 136 | - public function open() |
|
| 137 | - { |
|
| 138 | - if ($this->getErrors()) { |
|
| 139 | - $this->state($this->app['former.framework']->errorState()); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // Retrieve state and append it to classes |
|
| 143 | - if ($this->state) { |
|
| 144 | - $this->addClass($this->state); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // Required state |
|
| 148 | - if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) { |
|
| 149 | - $this->addClass($this->app['former']->getOption('required_class')); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return parent::open(); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Set the contents of the current group |
|
| 157 | - * |
|
| 158 | - * @param string $contents The group contents |
|
| 159 | - * |
|
| 160 | - * @return string A group |
|
| 161 | - */ |
|
| 162 | - public function contents($contents) |
|
| 163 | - { |
|
| 164 | - return $this->wrap($contents, $this->getFormattedLabel()); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Wrap a Field with the current group |
|
| 169 | - * |
|
| 170 | - * @param \Former\Traits\Field $field A Field instance |
|
| 171 | - * |
|
| 172 | - * @return string A group |
|
| 173 | - */ |
|
| 174 | - public function wrapField($field) |
|
| 175 | - { |
|
| 176 | - $label = $this->getLabel($field); |
|
| 177 | - $field = $this->prependAppend($field); |
|
| 178 | - $field .= $this->getHelp(); |
|
| 179 | - |
|
| 180 | - return $this->wrap($field, $label); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - //////////////////////////////////////////////////////////////////// |
|
| 184 | - //////////////////////////// FIELD METHODS ///////////////////////// |
|
| 185 | - //////////////////////////////////////////////////////////////////// |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Set the state of the group |
|
| 189 | - * |
|
| 190 | - * @param string $state A Bootstrap state class |
|
| 191 | - */ |
|
| 192 | - public function state($state) |
|
| 193 | - { |
|
| 194 | - // Filter state |
|
| 195 | - $state = $this->app['former.framework']->filterState($state); |
|
| 196 | - |
|
| 197 | - $this->state = $state; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Set a class on the Group |
|
| 202 | - * |
|
| 203 | - * @param string $class The class to add |
|
| 204 | - */ |
|
| 205 | - public function addGroupClass($class) |
|
| 206 | - { |
|
| 207 | - $this->addClass($class); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Adds a label to the group |
|
| 212 | - * |
|
| 213 | - * @param string $label A label |
|
| 214 | - */ |
|
| 215 | - public function setLabel($label) |
|
| 216 | - { |
|
| 217 | - if (!$label instanceof Element) { |
|
| 218 | - $label = Helpers::translate($label); |
|
| 219 | - $label = Element::create('label', $label)->for($label); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $this->label = $label; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Get the formatted group label |
|
| 227 | - * |
|
| 228 | - * @return string|null |
|
| 229 | - */ |
|
| 230 | - public function getFormattedLabel() |
|
| 231 | - { |
|
| 232 | - if (!$this->label) { |
|
| 233 | - return false; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - return $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Disables the control group for the current field |
|
| 241 | - */ |
|
| 242 | - public function raw() |
|
| 243 | - { |
|
| 244 | - $this->raw = true; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Check if the current group is to be displayed or not |
|
| 249 | - * |
|
| 250 | - * @return boolean |
|
| 251 | - */ |
|
| 252 | - public function isRaw() |
|
| 253 | - { |
|
| 254 | - return (bool) $this->raw; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - //////////////////////////////////////////////////////////////////// |
|
| 258 | - ///////////////////////////// HELP BLOCKS ////////////////////////// |
|
| 259 | - //////////////////////////////////////////////////////////////////// |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Alias for inlineHelp |
|
| 263 | - * |
|
| 264 | - * @param string $help The help text |
|
| 265 | - * @param array $attributes Facultative attributes |
|
| 266 | - */ |
|
| 267 | - public function help($help, $attributes = array()) |
|
| 268 | - { |
|
| 269 | - return $this->inlineHelp($help, $attributes); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Add an inline help |
|
| 274 | - * |
|
| 275 | - * @param string $help The help text |
|
| 276 | - * @param array $attributes Facultative attributes |
|
| 277 | - */ |
|
| 278 | - public function inlineHelp($help, $attributes = array()) |
|
| 279 | - { |
|
| 280 | - // If no help text, do nothing |
|
| 281 | - if (!$help) { |
|
| 282 | - return false; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Add an block help |
|
| 290 | - * |
|
| 291 | - * @param string $help The help text |
|
| 292 | - * @param array $attributes Facultative attributes |
|
| 293 | - */ |
|
| 294 | - public function blockHelp($help, $attributes = array()) |
|
| 295 | - { |
|
| 296 | - // Reserved method |
|
| 297 | - if ($this->app['former.framework']->isnt('TwitterBootstrap') && $this->app['former.framework']->isnt('TwitterBootstrap3')) { |
|
| 298 | - throw new BadMethodCallException('This method is only available on the Bootstrap framework'); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - // If no help text, do nothing |
|
| 302 | - if (!$help) { |
|
| 303 | - return false; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - //////////////////////////////////////////////////////////////////// |
|
| 310 | - ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
|
| 311 | - //////////////////////////////////////////////////////////////////// |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Prepend elements to the field |
|
| 315 | - */ |
|
| 316 | - public function prepend() |
|
| 317 | - { |
|
| 318 | - $this->placeAround(func_get_args(), 'prepend'); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Append elements to the field |
|
| 323 | - */ |
|
| 324 | - public function append() |
|
| 325 | - { |
|
| 326 | - $this->placeAround(func_get_args(), 'append'); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Prepends an icon to a field |
|
| 331 | - * |
|
| 332 | - * @param string $icon The icon to prepend |
|
| 333 | - * @param array $attributes Its attributes |
|
| 334 | - */ |
|
| 335 | - public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 336 | - { |
|
| 337 | - $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 338 | - |
|
| 339 | - $this->prepend($icon); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Append an icon to a field |
|
| 344 | - * |
|
| 345 | - * @param string $icon The icon to prepend |
|
| 346 | - * @param array $attributes Its attributes |
|
| 347 | - */ |
|
| 348 | - public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 349 | - { |
|
| 350 | - $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 351 | - |
|
| 352 | - $this->append($icon); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - //////////////////////////////////////////////////////////////////// |
|
| 356 | - //////////////////////////////// HELPERS /////////////////////////// |
|
| 357 | - //////////////////////////////////////////////////////////////////// |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Get the errors for the group |
|
| 361 | - * |
|
| 362 | - * @return string |
|
| 363 | - */ |
|
| 364 | - public function getErrors() |
|
| 365 | - { |
|
| 366 | - $errors = ''; |
|
| 367 | - |
|
| 368 | - if (!self::$opened) { |
|
| 369 | - |
|
| 370 | - // for non-custom groups, normal error handling applies |
|
| 371 | - $errors = $this->app['former']->getErrors(); |
|
| 372 | - } elseif (!empty($this->validations)) { |
|
| 373 | - |
|
| 374 | - // error handling only when validations specified for custom groups |
|
| 375 | - foreach ($this->validations as $validation) { |
|
| 376 | - $errors .= $this->app['former']->getErrors($validation); |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - return $errors; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Wraps content in a group |
|
| 385 | - * |
|
| 386 | - * @param string $contents The content |
|
| 387 | - * @param string $label The label to add |
|
| 388 | - * |
|
| 389 | - * @return string A group |
|
| 390 | - */ |
|
| 391 | - public function wrap($contents, $label = null) |
|
| 392 | - { |
|
| 393 | - $group = $this->open(); |
|
| 394 | - $group .= $label; |
|
| 395 | - $group .= $this->app['former.framework']->wrapField($contents); |
|
| 396 | - $group .= $this->close(); |
|
| 397 | - |
|
| 398 | - return $group; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Prints out the current label |
|
| 403 | - * |
|
| 404 | - * @param string $field The field to create a label for |
|
| 405 | - * |
|
| 406 | - * @return string A <label> tag |
|
| 407 | - */ |
|
| 408 | - protected function getLabel($field = null) |
|
| 409 | - { |
|
| 410 | - // Don't create a label if none exist |
|
| 411 | - if (!$field or !$this->label) { |
|
| 412 | - return null; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - // Wrap label in framework classes |
|
| 416 | - $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 417 | - $this->label = $this->app['former.framework']->createLabelOf($field, $this->label); |
|
| 418 | - $this->label = $this->app['former.framework']->wrapLabel($this->label); |
|
| 419 | - |
|
| 420 | - return $this->label; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Prints out the current help |
|
| 425 | - * |
|
| 426 | - * @return string A .help-block or .help-inline |
|
| 427 | - */ |
|
| 428 | - protected function getHelp() |
|
| 429 | - { |
|
| 430 | - $inline = array_get($this->help, 'inline'); |
|
| 431 | - $block = array_get($this->help, 'block'); |
|
| 432 | - |
|
| 433 | - // Replace help text with error if any found |
|
| 434 | - $errors = $this->app['former']->getErrors(); |
|
| 435 | - if ($errors and $this->app['former']->getOption('error_messages')) { |
|
| 436 | - $inline = $this->app['former.framework']->createHelp($errors); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return join(null, array($inline, $block)); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Format the field with prepended/appended elements |
|
| 444 | - * |
|
| 445 | - * @param Field $field The field to format |
|
| 446 | - * |
|
| 447 | - * @return string Field plus supplementary elements |
|
| 448 | - */ |
|
| 449 | - protected function prependAppend($field) |
|
| 450 | - { |
|
| 451 | - if (!$this->prepend and !$this->append) { |
|
| 452 | - return $field->render(); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Place elements around the field |
|
| 460 | - * |
|
| 461 | - * @param array $items An array of items to place |
|
| 462 | - * @param string $place Where they should end up (prepend|append) |
|
| 463 | - */ |
|
| 464 | - protected function placeAround($items, $place) |
|
| 465 | - { |
|
| 466 | - // Iterate over the items and place them where they should |
|
| 467 | - foreach ((array) $items as $item) { |
|
| 468 | - $item = $this->app['former.framework']->placeAround($item); |
|
| 469 | - $this->{$place}[] = $item; |
|
| 470 | - } |
|
| 471 | - } |
|
| 15 | + /** |
|
| 16 | + * The Container |
|
| 17 | + * |
|
| 18 | + * @var Container |
|
| 19 | + */ |
|
| 20 | + protected $app; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The current state of the group |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $state = null; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Whether the field should be displayed raw or not |
|
| 31 | + * |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | + protected $raw = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The group label |
|
| 38 | + * |
|
| 39 | + * @var Element |
|
| 40 | + */ |
|
| 41 | + protected $label; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The group help |
|
| 45 | + * |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | + protected $help = array(); |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * An array of elements to preprend the field |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + protected $prepend = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * An array of elements to append the field |
|
| 59 | + * |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + protected $append = array(); |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The field validations to be checked for errors |
|
| 66 | + * |
|
| 67 | + * @var array |
|
| 68 | + */ |
|
| 69 | + protected $validations = array(); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The group's element |
|
| 73 | + * |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + protected $element = 'div'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Whether a custom group is opened or not |
|
| 80 | + * |
|
| 81 | + * @var boolean |
|
| 82 | + */ |
|
| 83 | + public static $opened = false; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * The custom group that is open |
|
| 87 | + * |
|
| 88 | + * @var Former\Form\Group |
|
| 89 | + */ |
|
| 90 | + public static $openGroup = null; |
|
| 91 | + |
|
| 92 | + //////////////////////////////////////////////////////////////////// |
|
| 93 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 94 | + //////////////////////////////////////////////////////////////////// |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Creates a group |
|
| 98 | + * |
|
| 99 | + * @param string $label Its label |
|
| 100 | + */ |
|
| 101 | + public function __construct(Container $app, $label, $validations = null) |
|
| 102 | + { |
|
| 103 | + // Get special classes |
|
| 104 | + $this->app = $app; |
|
| 105 | + $this->addClass($this->app['former.framework']->getGroupClasses()); |
|
| 106 | + |
|
| 107 | + // Invisible if Nude |
|
| 108 | + if ($this->app['former.framework']->is('Nude')) { |
|
| 109 | + $this->element = ''; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // Set group label |
|
| 113 | + if ($label) { |
|
| 114 | + $this->setLabel($label); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + // Set validations used to override groups own conclusions |
|
| 118 | + $this->validations = (array) $validations; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Prints out the opening of the Control Group |
|
| 123 | + * |
|
| 124 | + * @return string A control group opening tag |
|
| 125 | + */ |
|
| 126 | + public function __toString() |
|
| 127 | + { |
|
| 128 | + return $this->open().$this->getFormattedLabel(); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Opens a group |
|
| 133 | + * |
|
| 134 | + * @return string Opening tag |
|
| 135 | + */ |
|
| 136 | + public function open() |
|
| 137 | + { |
|
| 138 | + if ($this->getErrors()) { |
|
| 139 | + $this->state($this->app['former.framework']->errorState()); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // Retrieve state and append it to classes |
|
| 143 | + if ($this->state) { |
|
| 144 | + $this->addClass($this->state); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // Required state |
|
| 148 | + if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) { |
|
| 149 | + $this->addClass($this->app['former']->getOption('required_class')); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return parent::open(); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Set the contents of the current group |
|
| 157 | + * |
|
| 158 | + * @param string $contents The group contents |
|
| 159 | + * |
|
| 160 | + * @return string A group |
|
| 161 | + */ |
|
| 162 | + public function contents($contents) |
|
| 163 | + { |
|
| 164 | + return $this->wrap($contents, $this->getFormattedLabel()); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Wrap a Field with the current group |
|
| 169 | + * |
|
| 170 | + * @param \Former\Traits\Field $field A Field instance |
|
| 171 | + * |
|
| 172 | + * @return string A group |
|
| 173 | + */ |
|
| 174 | + public function wrapField($field) |
|
| 175 | + { |
|
| 176 | + $label = $this->getLabel($field); |
|
| 177 | + $field = $this->prependAppend($field); |
|
| 178 | + $field .= $this->getHelp(); |
|
| 179 | + |
|
| 180 | + return $this->wrap($field, $label); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + //////////////////////////////////////////////////////////////////// |
|
| 184 | + //////////////////////////// FIELD METHODS ///////////////////////// |
|
| 185 | + //////////////////////////////////////////////////////////////////// |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Set the state of the group |
|
| 189 | + * |
|
| 190 | + * @param string $state A Bootstrap state class |
|
| 191 | + */ |
|
| 192 | + public function state($state) |
|
| 193 | + { |
|
| 194 | + // Filter state |
|
| 195 | + $state = $this->app['former.framework']->filterState($state); |
|
| 196 | + |
|
| 197 | + $this->state = $state; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Set a class on the Group |
|
| 202 | + * |
|
| 203 | + * @param string $class The class to add |
|
| 204 | + */ |
|
| 205 | + public function addGroupClass($class) |
|
| 206 | + { |
|
| 207 | + $this->addClass($class); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Adds a label to the group |
|
| 212 | + * |
|
| 213 | + * @param string $label A label |
|
| 214 | + */ |
|
| 215 | + public function setLabel($label) |
|
| 216 | + { |
|
| 217 | + if (!$label instanceof Element) { |
|
| 218 | + $label = Helpers::translate($label); |
|
| 219 | + $label = Element::create('label', $label)->for($label); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $this->label = $label; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Get the formatted group label |
|
| 227 | + * |
|
| 228 | + * @return string|null |
|
| 229 | + */ |
|
| 230 | + public function getFormattedLabel() |
|
| 231 | + { |
|
| 232 | + if (!$this->label) { |
|
| 233 | + return false; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + return $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Disables the control group for the current field |
|
| 241 | + */ |
|
| 242 | + public function raw() |
|
| 243 | + { |
|
| 244 | + $this->raw = true; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Check if the current group is to be displayed or not |
|
| 249 | + * |
|
| 250 | + * @return boolean |
|
| 251 | + */ |
|
| 252 | + public function isRaw() |
|
| 253 | + { |
|
| 254 | + return (bool) $this->raw; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + //////////////////////////////////////////////////////////////////// |
|
| 258 | + ///////////////////////////// HELP BLOCKS ////////////////////////// |
|
| 259 | + //////////////////////////////////////////////////////////////////// |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Alias for inlineHelp |
|
| 263 | + * |
|
| 264 | + * @param string $help The help text |
|
| 265 | + * @param array $attributes Facultative attributes |
|
| 266 | + */ |
|
| 267 | + public function help($help, $attributes = array()) |
|
| 268 | + { |
|
| 269 | + return $this->inlineHelp($help, $attributes); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Add an inline help |
|
| 274 | + * |
|
| 275 | + * @param string $help The help text |
|
| 276 | + * @param array $attributes Facultative attributes |
|
| 277 | + */ |
|
| 278 | + public function inlineHelp($help, $attributes = array()) |
|
| 279 | + { |
|
| 280 | + // If no help text, do nothing |
|
| 281 | + if (!$help) { |
|
| 282 | + return false; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Add an block help |
|
| 290 | + * |
|
| 291 | + * @param string $help The help text |
|
| 292 | + * @param array $attributes Facultative attributes |
|
| 293 | + */ |
|
| 294 | + public function blockHelp($help, $attributes = array()) |
|
| 295 | + { |
|
| 296 | + // Reserved method |
|
| 297 | + if ($this->app['former.framework']->isnt('TwitterBootstrap') && $this->app['former.framework']->isnt('TwitterBootstrap3')) { |
|
| 298 | + throw new BadMethodCallException('This method is only available on the Bootstrap framework'); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + // If no help text, do nothing |
|
| 302 | + if (!$help) { |
|
| 303 | + return false; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + //////////////////////////////////////////////////////////////////// |
|
| 310 | + ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
|
| 311 | + //////////////////////////////////////////////////////////////////// |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Prepend elements to the field |
|
| 315 | + */ |
|
| 316 | + public function prepend() |
|
| 317 | + { |
|
| 318 | + $this->placeAround(func_get_args(), 'prepend'); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Append elements to the field |
|
| 323 | + */ |
|
| 324 | + public function append() |
|
| 325 | + { |
|
| 326 | + $this->placeAround(func_get_args(), 'append'); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Prepends an icon to a field |
|
| 331 | + * |
|
| 332 | + * @param string $icon The icon to prepend |
|
| 333 | + * @param array $attributes Its attributes |
|
| 334 | + */ |
|
| 335 | + public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 336 | + { |
|
| 337 | + $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 338 | + |
|
| 339 | + $this->prepend($icon); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Append an icon to a field |
|
| 344 | + * |
|
| 345 | + * @param string $icon The icon to prepend |
|
| 346 | + * @param array $attributes Its attributes |
|
| 347 | + */ |
|
| 348 | + public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 349 | + { |
|
| 350 | + $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 351 | + |
|
| 352 | + $this->append($icon); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + //////////////////////////////////////////////////////////////////// |
|
| 356 | + //////////////////////////////// HELPERS /////////////////////////// |
|
| 357 | + //////////////////////////////////////////////////////////////////// |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Get the errors for the group |
|
| 361 | + * |
|
| 362 | + * @return string |
|
| 363 | + */ |
|
| 364 | + public function getErrors() |
|
| 365 | + { |
|
| 366 | + $errors = ''; |
|
| 367 | + |
|
| 368 | + if (!self::$opened) { |
|
| 369 | + |
|
| 370 | + // for non-custom groups, normal error handling applies |
|
| 371 | + $errors = $this->app['former']->getErrors(); |
|
| 372 | + } elseif (!empty($this->validations)) { |
|
| 373 | + |
|
| 374 | + // error handling only when validations specified for custom groups |
|
| 375 | + foreach ($this->validations as $validation) { |
|
| 376 | + $errors .= $this->app['former']->getErrors($validation); |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + return $errors; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Wraps content in a group |
|
| 385 | + * |
|
| 386 | + * @param string $contents The content |
|
| 387 | + * @param string $label The label to add |
|
| 388 | + * |
|
| 389 | + * @return string A group |
|
| 390 | + */ |
|
| 391 | + public function wrap($contents, $label = null) |
|
| 392 | + { |
|
| 393 | + $group = $this->open(); |
|
| 394 | + $group .= $label; |
|
| 395 | + $group .= $this->app['former.framework']->wrapField($contents); |
|
| 396 | + $group .= $this->close(); |
|
| 397 | + |
|
| 398 | + return $group; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Prints out the current label |
|
| 403 | + * |
|
| 404 | + * @param string $field The field to create a label for |
|
| 405 | + * |
|
| 406 | + * @return string A <label> tag |
|
| 407 | + */ |
|
| 408 | + protected function getLabel($field = null) |
|
| 409 | + { |
|
| 410 | + // Don't create a label if none exist |
|
| 411 | + if (!$field or !$this->label) { |
|
| 412 | + return null; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + // Wrap label in framework classes |
|
| 416 | + $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 417 | + $this->label = $this->app['former.framework']->createLabelOf($field, $this->label); |
|
| 418 | + $this->label = $this->app['former.framework']->wrapLabel($this->label); |
|
| 419 | + |
|
| 420 | + return $this->label; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Prints out the current help |
|
| 425 | + * |
|
| 426 | + * @return string A .help-block or .help-inline |
|
| 427 | + */ |
|
| 428 | + protected function getHelp() |
|
| 429 | + { |
|
| 430 | + $inline = array_get($this->help, 'inline'); |
|
| 431 | + $block = array_get($this->help, 'block'); |
|
| 432 | + |
|
| 433 | + // Replace help text with error if any found |
|
| 434 | + $errors = $this->app['former']->getErrors(); |
|
| 435 | + if ($errors and $this->app['former']->getOption('error_messages')) { |
|
| 436 | + $inline = $this->app['former.framework']->createHelp($errors); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return join(null, array($inline, $block)); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Format the field with prepended/appended elements |
|
| 444 | + * |
|
| 445 | + * @param Field $field The field to format |
|
| 446 | + * |
|
| 447 | + * @return string Field plus supplementary elements |
|
| 448 | + */ |
|
| 449 | + protected function prependAppend($field) |
|
| 450 | + { |
|
| 451 | + if (!$this->prepend and !$this->append) { |
|
| 452 | + return $field->render(); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Place elements around the field |
|
| 460 | + * |
|
| 461 | + * @param array $items An array of items to place |
|
| 462 | + * @param string $place Where they should end up (prepend|append) |
|
| 463 | + */ |
|
| 464 | + protected function placeAround($items, $place) |
|
| 465 | + { |
|
| 466 | + // Iterate over the items and place them where they should |
|
| 467 | + foreach ((array) $items as $item) { |
|
| 468 | + $item = $this->app['former.framework']->placeAround($item); |
|
| 469 | + $this->{$place}[] = $item; |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | 472 | } |
@@ -156,7 +156,7 @@ |
||
| 156 | 156 | /** |
| 157 | 157 | * Get all of the configuration files for the application. |
| 158 | 158 | * |
| 159 | - * @param $app |
|
| 159 | + * @param Container $app |
|
| 160 | 160 | * @return array |
| 161 | 161 | */ |
| 162 | 162 | protected function getConfigurationFiles($app) |
@@ -90,15 +90,15 @@ discard block |
||
| 90 | 90 | // Session and request |
| 91 | 91 | ////////////////////////////////////////////////////////////////// |
| 92 | 92 | |
| 93 | - $app->bindIf('session.manager', function ($app) { |
|
| 93 | + $app->bindIf('session.manager', function($app) { |
|
| 94 | 94 | return new SessionManager($app); |
| 95 | 95 | }); |
| 96 | 96 | |
| 97 | - $app->bindIf('session', function ($app) { |
|
| 97 | + $app->bindIf('session', function($app) { |
|
| 98 | 98 | return $app['session.manager']->driver('array'); |
| 99 | 99 | }, true); |
| 100 | 100 | |
| 101 | - $app->bindIf('request', function ($app) { |
|
| 101 | + $app->bindIf('request', function($app) { |
|
| 102 | 102 | $request = Request::createFromGlobals(); |
| 103 | 103 | if (method_exists($request, 'setSessionStore')) { |
| 104 | 104 | $request->setSessionStore($app['session']); |
@@ -112,11 +112,11 @@ discard block |
||
| 112 | 112 | // Config |
| 113 | 113 | ////////////////////////////////////////////////////////////////// |
| 114 | 114 | |
| 115 | - $app->bindIf('path.config', function ($app) { |
|
| 116 | - return __DIR__ . '/../config/'; |
|
| 115 | + $app->bindIf('path.config', function($app) { |
|
| 116 | + return __DIR__.'/../config/'; |
|
| 117 | 117 | }, true); |
| 118 | 118 | |
| 119 | - $app->bindIf('config', function ($app) { |
|
| 119 | + $app->bindIf('config', function($app) { |
|
| 120 | 120 | $config = new Repository; |
| 121 | 121 | $this->loadConfigurationFiles($app, $config); |
| 122 | 122 | return $config; |
@@ -125,11 +125,11 @@ discard block |
||
| 125 | 125 | // Localization |
| 126 | 126 | ////////////////////////////////////////////////////////////////// |
| 127 | 127 | |
| 128 | - $app->bindIf('translation.loader', function ($app) { |
|
| 128 | + $app->bindIf('translation.loader', function($app) { |
|
| 129 | 129 | return new FileLoader($app['files'], 'src/config'); |
| 130 | 130 | }); |
| 131 | 131 | |
| 132 | - $app->bindIf('translator', function ($app) { |
|
| 132 | + $app->bindIf('translator', function($app) { |
|
| 133 | 133 | $loader = new FileLoader($app['files'], 'lang'); |
| 134 | 134 | |
| 135 | 135 | return new Translator($loader, 'fr'); |
@@ -181,25 +181,25 @@ discard block |
||
| 181 | 181 | public function bindFormer(Container $app) |
| 182 | 182 | { |
| 183 | 183 | // Add config namespace |
| 184 | - $configPath = __DIR__ . '/../config/former.php'; |
|
| 184 | + $configPath = __DIR__.'/../config/former.php'; |
|
| 185 | 185 | $this->mergeConfigFrom($configPath, 'former'); |
| 186 | - $this->publishes([$configPath => $app['path.config'] . '/former.php']); |
|
| 186 | + $this->publishes([$configPath => $app['path.config'].'/former.php']); |
|
| 187 | 187 | |
| 188 | 188 | $framework = $app['config']->get('former.framework'); |
| 189 | 189 | |
| 190 | - $app->bind('former.framework', function ($app) { |
|
| 190 | + $app->bind('former.framework', function($app) { |
|
| 191 | 191 | return $app['former']->getFrameworkInstance($app['config']->get('former.framework')); |
| 192 | 192 | }); |
| 193 | 193 | |
| 194 | - $app->singleton('former.populator', function ($app) { |
|
| 194 | + $app->singleton('former.populator', function($app) { |
|
| 195 | 195 | return new Populator(); |
| 196 | 196 | }); |
| 197 | 197 | |
| 198 | - $app->singleton('former.dispatcher', function ($app) { |
|
| 198 | + $app->singleton('former.dispatcher', function($app) { |
|
| 199 | 199 | return new MethodDispatcher($app, Former::FIELDSPACE); |
| 200 | 200 | }); |
| 201 | 201 | |
| 202 | - $app->singleton('former', function ($app) { |
|
| 202 | + $app->singleton('former', function($app) { |
|
| 203 | 203 | return new Former($app, $app->make('former.dispatcher')); |
| 204 | 204 | }); |
| 205 | 205 | $app->alias('former', 'Former\Former'); |
@@ -15,197 +15,197 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class FormerServiceProvider extends ServiceProvider |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Indicates if loading of the provider is deferred. |
|
| 20 | - * |
|
| 21 | - * @var bool |
|
| 22 | - */ |
|
| 23 | - protected $defer = true; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Register Former's package with Laravel |
|
| 27 | - * |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - public function register() |
|
| 31 | - { |
|
| 32 | - $this->app = static::make($this->app); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Get the services provided by the provider. |
|
| 37 | - * |
|
| 38 | - * @return string[] |
|
| 39 | - */ |
|
| 40 | - public function provides() |
|
| 41 | - { |
|
| 42 | - return array('former', 'Former\Former'); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - //////////////////////////////////////////////////////////////////// |
|
| 46 | - /////////////////////////// CLASS BINDINGS ///////////////////////// |
|
| 47 | - //////////////////////////////////////////////////////////////////// |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Create a Former container |
|
| 51 | - * |
|
| 52 | - * @param Container $app |
|
| 53 | - * |
|
| 54 | - * @return Container |
|
| 55 | - */ |
|
| 56 | - public static function make($app = null) |
|
| 57 | - { |
|
| 58 | - if (!$app) { |
|
| 59 | - $app = new Container(); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - // Bind classes to container |
|
| 63 | - $provider = new static($app); |
|
| 64 | - $app = $provider->bindCoreClasses($app); |
|
| 65 | - $app = $provider->bindFormer($app); |
|
| 66 | - |
|
| 67 | - return $app; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Bind the core classes to the Container |
|
| 72 | - * |
|
| 73 | - * @param Container $app |
|
| 74 | - * |
|
| 75 | - * @return Container |
|
| 76 | - */ |
|
| 77 | - public function bindCoreClasses(Container $app) |
|
| 78 | - { |
|
| 79 | - // Cancel if in the scope of a Laravel application |
|
| 80 | - if ($app->bound('events')) { |
|
| 81 | - return $app; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // Core classes |
|
| 85 | - ////////////////////////////////////////////////////////////////// |
|
| 86 | - |
|
| 87 | - $app->bindIf('files', 'Illuminate\Filesystem\Filesystem'); |
|
| 88 | - $app->bindIf('url', 'Illuminate\Routing\UrlGenerator'); |
|
| 89 | - |
|
| 90 | - // Session and request |
|
| 91 | - ////////////////////////////////////////////////////////////////// |
|
| 92 | - |
|
| 93 | - $app->bindIf('session.manager', function ($app) { |
|
| 94 | - return new SessionManager($app); |
|
| 95 | - }); |
|
| 96 | - |
|
| 97 | - $app->bindIf('session', function ($app) { |
|
| 98 | - return $app['session.manager']->driver('array'); |
|
| 99 | - }, true); |
|
| 100 | - |
|
| 101 | - $app->bindIf('request', function ($app) { |
|
| 102 | - $request = Request::createFromGlobals(); |
|
| 103 | - if (method_exists($request, 'setSessionStore')) { |
|
| 104 | - $request->setSessionStore($app['session']); |
|
| 105 | - } else { |
|
| 106 | - $request->setLaravelSession($app['session']); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return $request; |
|
| 110 | - }, true); |
|
| 111 | - |
|
| 112 | - // Config |
|
| 113 | - ////////////////////////////////////////////////////////////////// |
|
| 114 | - |
|
| 115 | - $app->bindIf('path.config', function ($app) { |
|
| 116 | - return __DIR__ . '/../config/'; |
|
| 117 | - }, true); |
|
| 118 | - |
|
| 119 | - $app->bindIf('config', function ($app) { |
|
| 120 | - $config = new Repository; |
|
| 121 | - $this->loadConfigurationFiles($app, $config); |
|
| 122 | - return $config; |
|
| 123 | - }, true); |
|
| 124 | - |
|
| 125 | - // Localization |
|
| 126 | - ////////////////////////////////////////////////////////////////// |
|
| 127 | - |
|
| 128 | - $app->bindIf('translation.loader', function ($app) { |
|
| 129 | - return new FileLoader($app['files'], 'src/config'); |
|
| 130 | - }); |
|
| 131 | - |
|
| 132 | - $app->bindIf('translator', function ($app) { |
|
| 133 | - $loader = new FileLoader($app['files'], 'lang'); |
|
| 134 | - |
|
| 135 | - return new Translator($loader, 'fr'); |
|
| 136 | - }); |
|
| 137 | - |
|
| 138 | - return $app; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Load the configuration items from all of the files. |
|
| 143 | - * |
|
| 144 | - * @param Container $app |
|
| 145 | - * @param Repository $config |
|
| 146 | - * @return void |
|
| 147 | - */ |
|
| 148 | - protected function loadConfigurationFiles($app, Repository $config) |
|
| 149 | - { |
|
| 150 | - foreach ($this->getConfigurationFiles($app) as $key => $path) |
|
| 151 | - { |
|
| 152 | - $config->set($key, require $path); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Get all of the configuration files for the application. |
|
| 158 | - * |
|
| 159 | - * @param $app |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - protected function getConfigurationFiles($app) |
|
| 163 | - { |
|
| 164 | - $files = array(); |
|
| 165 | - |
|
| 166 | - foreach (Finder::create()->files()->name('*.php')->in($app['path.config']) as $file) |
|
| 167 | - { |
|
| 168 | - $files[basename($file->getRealPath(), '.php')] = $file->getRealPath(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return $files; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Bind Former classes to the container |
|
| 176 | - * |
|
| 177 | - * @param Container $app |
|
| 178 | - * |
|
| 179 | - * @return Container |
|
| 180 | - */ |
|
| 181 | - public function bindFormer(Container $app) |
|
| 182 | - { |
|
| 183 | - // Add config namespace |
|
| 184 | - $configPath = __DIR__ . '/../config/former.php'; |
|
| 185 | - $this->mergeConfigFrom($configPath, 'former'); |
|
| 186 | - $this->publishes([$configPath => $app['path.config'] . '/former.php']); |
|
| 18 | + /** |
|
| 19 | + * Indicates if loading of the provider is deferred. |
|
| 20 | + * |
|
| 21 | + * @var bool |
|
| 22 | + */ |
|
| 23 | + protected $defer = true; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Register Former's package with Laravel |
|
| 27 | + * |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + public function register() |
|
| 31 | + { |
|
| 32 | + $this->app = static::make($this->app); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Get the services provided by the provider. |
|
| 37 | + * |
|
| 38 | + * @return string[] |
|
| 39 | + */ |
|
| 40 | + public function provides() |
|
| 41 | + { |
|
| 42 | + return array('former', 'Former\Former'); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + //////////////////////////////////////////////////////////////////// |
|
| 46 | + /////////////////////////// CLASS BINDINGS ///////////////////////// |
|
| 47 | + //////////////////////////////////////////////////////////////////// |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Create a Former container |
|
| 51 | + * |
|
| 52 | + * @param Container $app |
|
| 53 | + * |
|
| 54 | + * @return Container |
|
| 55 | + */ |
|
| 56 | + public static function make($app = null) |
|
| 57 | + { |
|
| 58 | + if (!$app) { |
|
| 59 | + $app = new Container(); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + // Bind classes to container |
|
| 63 | + $provider = new static($app); |
|
| 64 | + $app = $provider->bindCoreClasses($app); |
|
| 65 | + $app = $provider->bindFormer($app); |
|
| 66 | + |
|
| 67 | + return $app; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Bind the core classes to the Container |
|
| 72 | + * |
|
| 73 | + * @param Container $app |
|
| 74 | + * |
|
| 75 | + * @return Container |
|
| 76 | + */ |
|
| 77 | + public function bindCoreClasses(Container $app) |
|
| 78 | + { |
|
| 79 | + // Cancel if in the scope of a Laravel application |
|
| 80 | + if ($app->bound('events')) { |
|
| 81 | + return $app; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // Core classes |
|
| 85 | + ////////////////////////////////////////////////////////////////// |
|
| 86 | + |
|
| 87 | + $app->bindIf('files', 'Illuminate\Filesystem\Filesystem'); |
|
| 88 | + $app->bindIf('url', 'Illuminate\Routing\UrlGenerator'); |
|
| 89 | + |
|
| 90 | + // Session and request |
|
| 91 | + ////////////////////////////////////////////////////////////////// |
|
| 92 | + |
|
| 93 | + $app->bindIf('session.manager', function ($app) { |
|
| 94 | + return new SessionManager($app); |
|
| 95 | + }); |
|
| 96 | + |
|
| 97 | + $app->bindIf('session', function ($app) { |
|
| 98 | + return $app['session.manager']->driver('array'); |
|
| 99 | + }, true); |
|
| 100 | + |
|
| 101 | + $app->bindIf('request', function ($app) { |
|
| 102 | + $request = Request::createFromGlobals(); |
|
| 103 | + if (method_exists($request, 'setSessionStore')) { |
|
| 104 | + $request->setSessionStore($app['session']); |
|
| 105 | + } else { |
|
| 106 | + $request->setLaravelSession($app['session']); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return $request; |
|
| 110 | + }, true); |
|
| 111 | + |
|
| 112 | + // Config |
|
| 113 | + ////////////////////////////////////////////////////////////////// |
|
| 114 | + |
|
| 115 | + $app->bindIf('path.config', function ($app) { |
|
| 116 | + return __DIR__ . '/../config/'; |
|
| 117 | + }, true); |
|
| 118 | + |
|
| 119 | + $app->bindIf('config', function ($app) { |
|
| 120 | + $config = new Repository; |
|
| 121 | + $this->loadConfigurationFiles($app, $config); |
|
| 122 | + return $config; |
|
| 123 | + }, true); |
|
| 124 | + |
|
| 125 | + // Localization |
|
| 126 | + ////////////////////////////////////////////////////////////////// |
|
| 127 | + |
|
| 128 | + $app->bindIf('translation.loader', function ($app) { |
|
| 129 | + return new FileLoader($app['files'], 'src/config'); |
|
| 130 | + }); |
|
| 131 | + |
|
| 132 | + $app->bindIf('translator', function ($app) { |
|
| 133 | + $loader = new FileLoader($app['files'], 'lang'); |
|
| 134 | + |
|
| 135 | + return new Translator($loader, 'fr'); |
|
| 136 | + }); |
|
| 137 | + |
|
| 138 | + return $app; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Load the configuration items from all of the files. |
|
| 143 | + * |
|
| 144 | + * @param Container $app |
|
| 145 | + * @param Repository $config |
|
| 146 | + * @return void |
|
| 147 | + */ |
|
| 148 | + protected function loadConfigurationFiles($app, Repository $config) |
|
| 149 | + { |
|
| 150 | + foreach ($this->getConfigurationFiles($app) as $key => $path) |
|
| 151 | + { |
|
| 152 | + $config->set($key, require $path); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Get all of the configuration files for the application. |
|
| 158 | + * |
|
| 159 | + * @param $app |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + protected function getConfigurationFiles($app) |
|
| 163 | + { |
|
| 164 | + $files = array(); |
|
| 165 | + |
|
| 166 | + foreach (Finder::create()->files()->name('*.php')->in($app['path.config']) as $file) |
|
| 167 | + { |
|
| 168 | + $files[basename($file->getRealPath(), '.php')] = $file->getRealPath(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return $files; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Bind Former classes to the container |
|
| 176 | + * |
|
| 177 | + * @param Container $app |
|
| 178 | + * |
|
| 179 | + * @return Container |
|
| 180 | + */ |
|
| 181 | + public function bindFormer(Container $app) |
|
| 182 | + { |
|
| 183 | + // Add config namespace |
|
| 184 | + $configPath = __DIR__ . '/../config/former.php'; |
|
| 185 | + $this->mergeConfigFrom($configPath, 'former'); |
|
| 186 | + $this->publishes([$configPath => $app['path.config'] . '/former.php']); |
|
| 187 | 187 | |
| 188 | - $framework = $app['config']->get('former.framework'); |
|
| 188 | + $framework = $app['config']->get('former.framework'); |
|
| 189 | 189 | |
| 190 | - $app->bind('former.framework', function ($app) { |
|
| 191 | - return $app['former']->getFrameworkInstance($app['config']->get('former.framework')); |
|
| 192 | - }); |
|
| 190 | + $app->bind('former.framework', function ($app) { |
|
| 191 | + return $app['former']->getFrameworkInstance($app['config']->get('former.framework')); |
|
| 192 | + }); |
|
| 193 | 193 | |
| 194 | - $app->singleton('former.populator', function ($app) { |
|
| 195 | - return new Populator(); |
|
| 196 | - }); |
|
| 194 | + $app->singleton('former.populator', function ($app) { |
|
| 195 | + return new Populator(); |
|
| 196 | + }); |
|
| 197 | 197 | |
| 198 | - $app->singleton('former.dispatcher', function ($app) { |
|
| 199 | - return new MethodDispatcher($app, Former::FIELDSPACE); |
|
| 200 | - }); |
|
| 198 | + $app->singleton('former.dispatcher', function ($app) { |
|
| 199 | + return new MethodDispatcher($app, Former::FIELDSPACE); |
|
| 200 | + }); |
|
| 201 | 201 | |
| 202 | - $app->singleton('former', function ($app) { |
|
| 203 | - return new Former($app, $app->make('former.dispatcher')); |
|
| 204 | - }); |
|
| 205 | - $app->alias('former', 'Former\Former'); |
|
| 202 | + $app->singleton('former', function ($app) { |
|
| 203 | + return new Former($app, $app->make('former.dispatcher')); |
|
| 204 | + }); |
|
| 205 | + $app->alias('former', 'Former\Former'); |
|
| 206 | 206 | |
| 207 | - Helpers::setApp($app); |
|
| 207 | + Helpers::setApp($app); |
|
| 208 | 208 | |
| 209 | - return $app; |
|
| 210 | - } |
|
| 209 | + return $app; |
|
| 210 | + } |
|
| 211 | 211 | } |
@@ -126,7 +126,7 @@ |
||
| 126 | 126 | * |
| 127 | 127 | * @param Field $field |
| 128 | 128 | * |
| 129 | - * @return Element |
|
| 129 | + * @return Input |
|
| 130 | 130 | */ |
| 131 | 131 | public function createPlainTextField(Field $field) |
| 132 | 132 | { |
@@ -14,182 +14,182 @@ |
||
| 14 | 14 | class Nude extends Framework implements FrameworkInterface |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The field states available |
|
| 19 | - * |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - protected $states = array( |
|
| 23 | - 'error', |
|
| 24 | - ); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Create a new Nude instance |
|
| 28 | - * |
|
| 29 | - * @param Container $app |
|
| 30 | - */ |
|
| 31 | - public function __construct(Container $app) |
|
| 32 | - { |
|
| 33 | - $this->app = $app; |
|
| 34 | - $this->setFrameworkDefaults(); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - //////////////////////////////////////////////////////////////////// |
|
| 38 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 39 | - //////////////////////////////////////////////////////////////////// |
|
| 40 | - |
|
| 41 | - public function filterButtonClasses($classes) |
|
| 42 | - { |
|
| 43 | - return $classes; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function filterFieldClasses($classes) |
|
| 47 | - { |
|
| 48 | - return $classes; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - //////////////////////////////////////////////////////////////////// |
|
| 52 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 53 | - //////////////////////////////////////////////////////////////////// |
|
| 54 | - |
|
| 55 | - public function getFieldClasses(Field $field, $classes = array()) |
|
| 56 | - { |
|
| 57 | - $classes = $this->filterFieldClasses($classes); |
|
| 58 | - |
|
| 59 | - // If we found any class, add them |
|
| 60 | - if ($classes) { |
|
| 61 | - $field->class(implode(' ', $classes)); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - return $field; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - public function getGroupClasses() |
|
| 68 | - { |
|
| 69 | - return null; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function getLabelClasses() |
|
| 73 | - { |
|
| 74 | - return null; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function getUneditableClasses() |
|
| 78 | - { |
|
| 79 | - return null; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function getPlainTextClasses() |
|
| 83 | - { |
|
| 84 | - return null; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public function getFormClasses($type) |
|
| 88 | - { |
|
| 89 | - return null; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function getActionClasses() |
|
| 93 | - { |
|
| 94 | - return null; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - //////////////////////////////////////////////////////////////////// |
|
| 98 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 99 | - //////////////////////////////////////////////////////////////////// |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Create an help text |
|
| 103 | - */ |
|
| 104 | - public function createHelp($text, $attributes = array()) |
|
| 105 | - { |
|
| 106 | - return Element::create('span', $text, $attributes)->addClass('help'); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Render a disabled field |
|
| 111 | - * |
|
| 112 | - * @param Field $field |
|
| 113 | - * |
|
| 114 | - * @return Input |
|
| 115 | - */ |
|
| 116 | - public function createDisabledField(Field $field) |
|
| 117 | - { |
|
| 118 | - $field->disabled(); |
|
| 119 | - |
|
| 120 | - return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes()); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Render a plain text field |
|
| 125 | - * Which fallback to a disabled field |
|
| 126 | - * |
|
| 127 | - * @param Field $field |
|
| 128 | - * |
|
| 129 | - * @return Element |
|
| 130 | - */ |
|
| 131 | - public function createPlainTextField(Field $field) |
|
| 132 | - { |
|
| 133 | - return $this->createDisabledField($field); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - //////////////////////////////////////////////////////////////////// |
|
| 137 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 138 | - //////////////////////////////////////////////////////////////////// |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Wrap an item to be prepended or appended to the current field |
|
| 142 | - * |
|
| 143 | - * @param string $item |
|
| 144 | - * |
|
| 145 | - * @return Element A wrapped item |
|
| 146 | - */ |
|
| 147 | - public function placeAround($item) |
|
| 148 | - { |
|
| 149 | - return Element::create('span', $item); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Wrap a field with prepended and appended items |
|
| 154 | - * |
|
| 155 | - * @param Field $field |
|
| 156 | - * @param array $prepend |
|
| 157 | - * @param array $append |
|
| 158 | - * |
|
| 159 | - * @return string A field concatented with prepended and/or appended items |
|
| 160 | - */ |
|
| 161 | - public function prependAppend($field, $prepend, $append) |
|
| 162 | - { |
|
| 163 | - $return = '<div>'; |
|
| 164 | - $return .= join(null, $prepend); |
|
| 165 | - $return .= $field->render(); |
|
| 166 | - $return .= join(null, $append); |
|
| 167 | - $return .= '</div>'; |
|
| 168 | - |
|
| 169 | - return $return; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Wraps all field contents with potential additional tags. |
|
| 174 | - * |
|
| 175 | - * @param Field $field |
|
| 176 | - * |
|
| 177 | - * @return Field A wrapped field |
|
| 178 | - */ |
|
| 179 | - public function wrapField($field) |
|
| 180 | - { |
|
| 181 | - return $field; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Wrap actions block with potential additional tags |
|
| 186 | - * |
|
| 187 | - * @param Actions $actions |
|
| 188 | - * |
|
| 189 | - * @return string A wrapped actions block |
|
| 190 | - */ |
|
| 191 | - public function wrapActions($actions) |
|
| 192 | - { |
|
| 193 | - return $actions; |
|
| 194 | - } |
|
| 17 | + /** |
|
| 18 | + * The field states available |
|
| 19 | + * |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + protected $states = array( |
|
| 23 | + 'error', |
|
| 24 | + ); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Create a new Nude instance |
|
| 28 | + * |
|
| 29 | + * @param Container $app |
|
| 30 | + */ |
|
| 31 | + public function __construct(Container $app) |
|
| 32 | + { |
|
| 33 | + $this->app = $app; |
|
| 34 | + $this->setFrameworkDefaults(); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + //////////////////////////////////////////////////////////////////// |
|
| 38 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 39 | + //////////////////////////////////////////////////////////////////// |
|
| 40 | + |
|
| 41 | + public function filterButtonClasses($classes) |
|
| 42 | + { |
|
| 43 | + return $classes; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function filterFieldClasses($classes) |
|
| 47 | + { |
|
| 48 | + return $classes; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + //////////////////////////////////////////////////////////////////// |
|
| 52 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 53 | + //////////////////////////////////////////////////////////////////// |
|
| 54 | + |
|
| 55 | + public function getFieldClasses(Field $field, $classes = array()) |
|
| 56 | + { |
|
| 57 | + $classes = $this->filterFieldClasses($classes); |
|
| 58 | + |
|
| 59 | + // If we found any class, add them |
|
| 60 | + if ($classes) { |
|
| 61 | + $field->class(implode(' ', $classes)); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + return $field; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + public function getGroupClasses() |
|
| 68 | + { |
|
| 69 | + return null; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function getLabelClasses() |
|
| 73 | + { |
|
| 74 | + return null; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function getUneditableClasses() |
|
| 78 | + { |
|
| 79 | + return null; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function getPlainTextClasses() |
|
| 83 | + { |
|
| 84 | + return null; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public function getFormClasses($type) |
|
| 88 | + { |
|
| 89 | + return null; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function getActionClasses() |
|
| 93 | + { |
|
| 94 | + return null; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + //////////////////////////////////////////////////////////////////// |
|
| 98 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 99 | + //////////////////////////////////////////////////////////////////// |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Create an help text |
|
| 103 | + */ |
|
| 104 | + public function createHelp($text, $attributes = array()) |
|
| 105 | + { |
|
| 106 | + return Element::create('span', $text, $attributes)->addClass('help'); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Render a disabled field |
|
| 111 | + * |
|
| 112 | + * @param Field $field |
|
| 113 | + * |
|
| 114 | + * @return Input |
|
| 115 | + */ |
|
| 116 | + public function createDisabledField(Field $field) |
|
| 117 | + { |
|
| 118 | + $field->disabled(); |
|
| 119 | + |
|
| 120 | + return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes()); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Render a plain text field |
|
| 125 | + * Which fallback to a disabled field |
|
| 126 | + * |
|
| 127 | + * @param Field $field |
|
| 128 | + * |
|
| 129 | + * @return Element |
|
| 130 | + */ |
|
| 131 | + public function createPlainTextField(Field $field) |
|
| 132 | + { |
|
| 133 | + return $this->createDisabledField($field); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + //////////////////////////////////////////////////////////////////// |
|
| 137 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 138 | + //////////////////////////////////////////////////////////////////// |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Wrap an item to be prepended or appended to the current field |
|
| 142 | + * |
|
| 143 | + * @param string $item |
|
| 144 | + * |
|
| 145 | + * @return Element A wrapped item |
|
| 146 | + */ |
|
| 147 | + public function placeAround($item) |
|
| 148 | + { |
|
| 149 | + return Element::create('span', $item); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Wrap a field with prepended and appended items |
|
| 154 | + * |
|
| 155 | + * @param Field $field |
|
| 156 | + * @param array $prepend |
|
| 157 | + * @param array $append |
|
| 158 | + * |
|
| 159 | + * @return string A field concatented with prepended and/or appended items |
|
| 160 | + */ |
|
| 161 | + public function prependAppend($field, $prepend, $append) |
|
| 162 | + { |
|
| 163 | + $return = '<div>'; |
|
| 164 | + $return .= join(null, $prepend); |
|
| 165 | + $return .= $field->render(); |
|
| 166 | + $return .= join(null, $append); |
|
| 167 | + $return .= '</div>'; |
|
| 168 | + |
|
| 169 | + return $return; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Wraps all field contents with potential additional tags. |
|
| 174 | + * |
|
| 175 | + * @param Field $field |
|
| 176 | + * |
|
| 177 | + * @return Field A wrapped field |
|
| 178 | + */ |
|
| 179 | + public function wrapField($field) |
|
| 180 | + { |
|
| 181 | + return $field; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Wrap actions block with potential additional tags |
|
| 186 | + * |
|
| 187 | + * @param Actions $actions |
|
| 188 | + * |
|
| 189 | + * @return string A wrapped actions block |
|
| 190 | + */ |
|
| 191 | + public function wrapActions($actions) |
|
| 192 | + { |
|
| 193 | + return $actions; |
|
| 194 | + } |
|
| 195 | 195 | } |
@@ -267,7 +267,7 @@ |
||
| 267 | 267 | /** |
| 268 | 268 | * Transform extensions and mime groups into a list of mime types |
| 269 | 269 | * |
| 270 | - * @param array $mimes An array of mimes |
|
| 270 | + * @param string[] $mimes An array of mimes |
|
| 271 | 271 | * |
| 272 | 272 | * @return string A concatenated list of mimes |
| 273 | 273 | */ |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | * |
| 22 | 22 | * @param Field $field The field |
| 23 | 23 | */ |
| 24 | - public function __construct(Field &$field) |
|
| 24 | + public function __construct(Field&$field) |
|
| 25 | 25 | { |
| 26 | 26 | $this->field = $field; |
| 27 | 27 | } |
@@ -9,338 +9,338 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class LiveValidation |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * The field being worked on |
|
| 14 | - * |
|
| 15 | - * @var Field |
|
| 16 | - */ |
|
| 17 | - public $field; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Load a Field instance to apply rules to it |
|
| 21 | - * |
|
| 22 | - * @param Field $field The field |
|
| 23 | - */ |
|
| 24 | - public function __construct(Field &$field) |
|
| 25 | - { |
|
| 26 | - $this->field = $field; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Apply live validation rules to a field |
|
| 31 | - * |
|
| 32 | - * @param array $rules The rules to apply |
|
| 33 | - */ |
|
| 34 | - public function apply($rules) |
|
| 35 | - { |
|
| 36 | - // If no rules to apply, cancel |
|
| 37 | - if (!$rules) { |
|
| 38 | - return false; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - foreach ($rules as $rule => $parameters) { |
|
| 42 | - |
|
| 43 | - // If the rule is unsupported yet, skip it |
|
| 44 | - if (!method_exists($this, $rule)) { |
|
| 45 | - continue; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $this->$rule($parameters); |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - //////////////////////////////////////////////////////////////////// |
|
| 53 | - //////////////////////////////// RULES ///////////////////////////// |
|
| 54 | - //////////////////////////////////////////////////////////////////// |
|
| 55 | - |
|
| 56 | - // Field types |
|
| 57 | - //////////////////////////////////////////////////////////////////// |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Email field |
|
| 61 | - */ |
|
| 62 | - public function email() |
|
| 63 | - { |
|
| 64 | - $this->field->setType('email'); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * URL field |
|
| 69 | - */ |
|
| 70 | - public function url() |
|
| 71 | - { |
|
| 72 | - $this->field->setType('url'); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Required field |
|
| 77 | - */ |
|
| 78 | - public function required() |
|
| 79 | - { |
|
| 80 | - $this->field->required(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Patterns |
|
| 84 | - //////////////////////////////////////////////////////////////////// |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Integer field |
|
| 88 | - */ |
|
| 89 | - public function integer() |
|
| 90 | - { |
|
| 91 | - $this->field->pattern('\d+'); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Numeric field |
|
| 96 | - */ |
|
| 97 | - public function numeric() |
|
| 98 | - { |
|
| 99 | - if ($this->field->isOfType('number')) { |
|
| 100 | - $this->field->step('any'); |
|
| 101 | - } else { |
|
| 102 | - $this->field->pattern('[+-]?\d*\.?\d+'); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Not numeric field |
|
| 108 | - */ |
|
| 109 | - public function not_numeric() |
|
| 110 | - { |
|
| 111 | - $this->field->pattern('\D+'); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Only alphanumerical |
|
| 116 | - */ |
|
| 117 | - public function alpha() |
|
| 118 | - { |
|
| 119 | - $this->field->pattern('[a-zA-Z]+'); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Only alphanumerical and numbers |
|
| 124 | - */ |
|
| 125 | - public function alpha_num() |
|
| 126 | - { |
|
| 127 | - $this->field->pattern('[a-zA-Z0-9]+'); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Alphanumerical, numbers and dashes |
|
| 132 | - */ |
|
| 133 | - public function alpha_dash() |
|
| 134 | - { |
|
| 135 | - $this->field->pattern('[a-zA-Z0-9_\-]+'); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * In [] |
|
| 140 | - */ |
|
| 141 | - public function in($possible) |
|
| 142 | - { |
|
| 143 | - // Create the corresponding regex |
|
| 144 | - $possible = (sizeof($possible) == 1) ? $possible[0] : '('.join('|', $possible).')'; |
|
| 145 | - |
|
| 146 | - $this->field->pattern('^'.$possible.'$'); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Not in [] |
|
| 151 | - */ |
|
| 152 | - public function not_in($impossible) |
|
| 153 | - { |
|
| 154 | - $this->field->pattern('(?:(?!^'.join('$|^', $impossible).'$).)*'); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Matches a pattern |
|
| 159 | - */ |
|
| 160 | - public function match($pattern) |
|
| 161 | - { |
|
| 162 | - // Remove delimiters from existing regex |
|
| 163 | - $pattern = substr($pattern[0], 1, -1); |
|
| 164 | - |
|
| 165 | - $this->field->pattern($pattern); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Alias for match |
|
| 170 | - */ |
|
| 171 | - public function regex($pattern) |
|
| 172 | - { |
|
| 173 | - return $this->match($pattern); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Boundaries |
|
| 177 | - //////////////////////////////////////////////////////////////////// |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Max value |
|
| 181 | - */ |
|
| 182 | - public function max($max) |
|
| 183 | - { |
|
| 184 | - if ($this->field->isOfType('file')) { |
|
| 185 | - $this->size($max); |
|
| 186 | - } else { |
|
| 187 | - $this->setMax($max[0]); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Max size |
|
| 193 | - */ |
|
| 194 | - public function size($size) |
|
| 195 | - { |
|
| 196 | - $this->field->max($size[0]); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Min value |
|
| 201 | - */ |
|
| 202 | - public function min($min) |
|
| 203 | - { |
|
| 204 | - $this->setMin($min[0]); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Set boundaries |
|
| 209 | - */ |
|
| 210 | - public function between($between) |
|
| 211 | - { |
|
| 212 | - list($min, $max) = $between; |
|
| 213 | - |
|
| 214 | - $this->setBetween($min, $max); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Set accepted mime types |
|
| 219 | - * |
|
| 220 | - * @param string[] $mimes |
|
| 221 | - */ |
|
| 222 | - public function mimes($mimes) |
|
| 223 | - { |
|
| 224 | - // Only useful on file fields |
|
| 225 | - if (!$this->field->isOfType('file')) { |
|
| 226 | - return false; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - $this->field->accept($this->setAccepted($mimes)); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Set accept only images |
|
| 234 | - */ |
|
| 235 | - public function image() |
|
| 236 | - { |
|
| 237 | - $this->mimes(array('jpg', 'png', 'gif', 'bmp')); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - // Dates |
|
| 241 | - //////////////////////////////////////////////////////////////////// |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Before a date |
|
| 245 | - */ |
|
| 246 | - public function before($date) |
|
| 247 | - { |
|
| 248 | - list($format, $date) = $this->formatDate($date[0]); |
|
| 249 | - |
|
| 250 | - $this->field->max(date($format, $date)); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * After a date |
|
| 255 | - */ |
|
| 256 | - public function after($date) |
|
| 257 | - { |
|
| 258 | - list($format, $date) = $this->formatDate($date[0]); |
|
| 259 | - |
|
| 260 | - $this->field->min(date($format, $date)); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - //////////////////////////////////////////////////////////////////// |
|
| 264 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 265 | - //////////////////////////////////////////////////////////////////// |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Transform extensions and mime groups into a list of mime types |
|
| 269 | - * |
|
| 270 | - * @param array $mimes An array of mimes |
|
| 271 | - * |
|
| 272 | - * @return string A concatenated list of mimes |
|
| 273 | - */ |
|
| 274 | - private function setAccepted($mimes) |
|
| 275 | - { |
|
| 276 | - // Transform extensions or mime groups into mime types |
|
| 277 | - $mimes = array_map(array('\Laravel\File', 'mime'), $mimes); |
|
| 278 | - |
|
| 279 | - return implode(',', $mimes); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Format a date to a pattern |
|
| 284 | - * |
|
| 285 | - * @param string $date The date |
|
| 286 | - * |
|
| 287 | - * @return string The pattern |
|
| 288 | - */ |
|
| 289 | - private function formatDate($date) |
|
| 290 | - { |
|
| 291 | - $format = 'Y-m-d'; |
|
| 292 | - |
|
| 293 | - // Add hour for datetime fields |
|
| 294 | - if ($this->field->isOfType('datetime', 'datetime-local')) { |
|
| 295 | - $format .= '\TH:i:s'; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - return array($format, strtotime($date)); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Set a maximum value to a field |
|
| 303 | - * |
|
| 304 | - * @param integer $max |
|
| 305 | - */ |
|
| 306 | - private function setMax($max) |
|
| 307 | - { |
|
| 308 | - $attribute = $this->field->isOfType('number') ? 'max' : 'maxlength'; |
|
| 309 | - |
|
| 310 | - $this->field->$attribute($max); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Set a minimum value to a field |
|
| 315 | - * |
|
| 316 | - * @param integer $min |
|
| 317 | - */ |
|
| 318 | - private function setMin($min) |
|
| 319 | - { |
|
| 320 | - if ($this->field->isOfType('number') == 'min') { |
|
| 321 | - $this->field->min($min); |
|
| 322 | - } else { |
|
| 323 | - $this->field->pattern(".{".$min.",}"); |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Set a minimum and maximum value to a field |
|
| 329 | - * |
|
| 330 | - * @param $min |
|
| 331 | - * @param $max |
|
| 332 | - */ |
|
| 333 | - public function setBetween($min, $max) |
|
| 334 | - { |
|
| 335 | - if ($this->field->isOfType('number') == 'min') { |
|
| 336 | - // min, max values for generation of the pattern |
|
| 337 | - $this->field->min($min); |
|
| 338 | - $this->field->max($max); |
|
| 339 | - } else { |
|
| 340 | - $this->field->pattern('.{'.$min.','.$max.'}'); |
|
| 341 | - |
|
| 342 | - // still let the browser limit text input after reaching the max |
|
| 343 | - $this->field->maxlength($max); |
|
| 344 | - } |
|
| 345 | - } |
|
| 12 | + /** |
|
| 13 | + * The field being worked on |
|
| 14 | + * |
|
| 15 | + * @var Field |
|
| 16 | + */ |
|
| 17 | + public $field; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Load a Field instance to apply rules to it |
|
| 21 | + * |
|
| 22 | + * @param Field $field The field |
|
| 23 | + */ |
|
| 24 | + public function __construct(Field &$field) |
|
| 25 | + { |
|
| 26 | + $this->field = $field; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Apply live validation rules to a field |
|
| 31 | + * |
|
| 32 | + * @param array $rules The rules to apply |
|
| 33 | + */ |
|
| 34 | + public function apply($rules) |
|
| 35 | + { |
|
| 36 | + // If no rules to apply, cancel |
|
| 37 | + if (!$rules) { |
|
| 38 | + return false; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + foreach ($rules as $rule => $parameters) { |
|
| 42 | + |
|
| 43 | + // If the rule is unsupported yet, skip it |
|
| 44 | + if (!method_exists($this, $rule)) { |
|
| 45 | + continue; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $this->$rule($parameters); |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + //////////////////////////////////////////////////////////////////// |
|
| 53 | + //////////////////////////////// RULES ///////////////////////////// |
|
| 54 | + //////////////////////////////////////////////////////////////////// |
|
| 55 | + |
|
| 56 | + // Field types |
|
| 57 | + //////////////////////////////////////////////////////////////////// |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Email field |
|
| 61 | + */ |
|
| 62 | + public function email() |
|
| 63 | + { |
|
| 64 | + $this->field->setType('email'); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * URL field |
|
| 69 | + */ |
|
| 70 | + public function url() |
|
| 71 | + { |
|
| 72 | + $this->field->setType('url'); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Required field |
|
| 77 | + */ |
|
| 78 | + public function required() |
|
| 79 | + { |
|
| 80 | + $this->field->required(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Patterns |
|
| 84 | + //////////////////////////////////////////////////////////////////// |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Integer field |
|
| 88 | + */ |
|
| 89 | + public function integer() |
|
| 90 | + { |
|
| 91 | + $this->field->pattern('\d+'); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Numeric field |
|
| 96 | + */ |
|
| 97 | + public function numeric() |
|
| 98 | + { |
|
| 99 | + if ($this->field->isOfType('number')) { |
|
| 100 | + $this->field->step('any'); |
|
| 101 | + } else { |
|
| 102 | + $this->field->pattern('[+-]?\d*\.?\d+'); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Not numeric field |
|
| 108 | + */ |
|
| 109 | + public function not_numeric() |
|
| 110 | + { |
|
| 111 | + $this->field->pattern('\D+'); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Only alphanumerical |
|
| 116 | + */ |
|
| 117 | + public function alpha() |
|
| 118 | + { |
|
| 119 | + $this->field->pattern('[a-zA-Z]+'); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Only alphanumerical and numbers |
|
| 124 | + */ |
|
| 125 | + public function alpha_num() |
|
| 126 | + { |
|
| 127 | + $this->field->pattern('[a-zA-Z0-9]+'); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Alphanumerical, numbers and dashes |
|
| 132 | + */ |
|
| 133 | + public function alpha_dash() |
|
| 134 | + { |
|
| 135 | + $this->field->pattern('[a-zA-Z0-9_\-]+'); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * In [] |
|
| 140 | + */ |
|
| 141 | + public function in($possible) |
|
| 142 | + { |
|
| 143 | + // Create the corresponding regex |
|
| 144 | + $possible = (sizeof($possible) == 1) ? $possible[0] : '('.join('|', $possible).')'; |
|
| 145 | + |
|
| 146 | + $this->field->pattern('^'.$possible.'$'); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Not in [] |
|
| 151 | + */ |
|
| 152 | + public function not_in($impossible) |
|
| 153 | + { |
|
| 154 | + $this->field->pattern('(?:(?!^'.join('$|^', $impossible).'$).)*'); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Matches a pattern |
|
| 159 | + */ |
|
| 160 | + public function match($pattern) |
|
| 161 | + { |
|
| 162 | + // Remove delimiters from existing regex |
|
| 163 | + $pattern = substr($pattern[0], 1, -1); |
|
| 164 | + |
|
| 165 | + $this->field->pattern($pattern); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Alias for match |
|
| 170 | + */ |
|
| 171 | + public function regex($pattern) |
|
| 172 | + { |
|
| 173 | + return $this->match($pattern); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Boundaries |
|
| 177 | + //////////////////////////////////////////////////////////////////// |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Max value |
|
| 181 | + */ |
|
| 182 | + public function max($max) |
|
| 183 | + { |
|
| 184 | + if ($this->field->isOfType('file')) { |
|
| 185 | + $this->size($max); |
|
| 186 | + } else { |
|
| 187 | + $this->setMax($max[0]); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Max size |
|
| 193 | + */ |
|
| 194 | + public function size($size) |
|
| 195 | + { |
|
| 196 | + $this->field->max($size[0]); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Min value |
|
| 201 | + */ |
|
| 202 | + public function min($min) |
|
| 203 | + { |
|
| 204 | + $this->setMin($min[0]); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Set boundaries |
|
| 209 | + */ |
|
| 210 | + public function between($between) |
|
| 211 | + { |
|
| 212 | + list($min, $max) = $between; |
|
| 213 | + |
|
| 214 | + $this->setBetween($min, $max); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Set accepted mime types |
|
| 219 | + * |
|
| 220 | + * @param string[] $mimes |
|
| 221 | + */ |
|
| 222 | + public function mimes($mimes) |
|
| 223 | + { |
|
| 224 | + // Only useful on file fields |
|
| 225 | + if (!$this->field->isOfType('file')) { |
|
| 226 | + return false; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + $this->field->accept($this->setAccepted($mimes)); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Set accept only images |
|
| 234 | + */ |
|
| 235 | + public function image() |
|
| 236 | + { |
|
| 237 | + $this->mimes(array('jpg', 'png', 'gif', 'bmp')); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + // Dates |
|
| 241 | + //////////////////////////////////////////////////////////////////// |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Before a date |
|
| 245 | + */ |
|
| 246 | + public function before($date) |
|
| 247 | + { |
|
| 248 | + list($format, $date) = $this->formatDate($date[0]); |
|
| 249 | + |
|
| 250 | + $this->field->max(date($format, $date)); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * After a date |
|
| 255 | + */ |
|
| 256 | + public function after($date) |
|
| 257 | + { |
|
| 258 | + list($format, $date) = $this->formatDate($date[0]); |
|
| 259 | + |
|
| 260 | + $this->field->min(date($format, $date)); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + //////////////////////////////////////////////////////////////////// |
|
| 264 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 265 | + //////////////////////////////////////////////////////////////////// |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Transform extensions and mime groups into a list of mime types |
|
| 269 | + * |
|
| 270 | + * @param array $mimes An array of mimes |
|
| 271 | + * |
|
| 272 | + * @return string A concatenated list of mimes |
|
| 273 | + */ |
|
| 274 | + private function setAccepted($mimes) |
|
| 275 | + { |
|
| 276 | + // Transform extensions or mime groups into mime types |
|
| 277 | + $mimes = array_map(array('\Laravel\File', 'mime'), $mimes); |
|
| 278 | + |
|
| 279 | + return implode(',', $mimes); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Format a date to a pattern |
|
| 284 | + * |
|
| 285 | + * @param string $date The date |
|
| 286 | + * |
|
| 287 | + * @return string The pattern |
|
| 288 | + */ |
|
| 289 | + private function formatDate($date) |
|
| 290 | + { |
|
| 291 | + $format = 'Y-m-d'; |
|
| 292 | + |
|
| 293 | + // Add hour for datetime fields |
|
| 294 | + if ($this->field->isOfType('datetime', 'datetime-local')) { |
|
| 295 | + $format .= '\TH:i:s'; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + return array($format, strtotime($date)); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Set a maximum value to a field |
|
| 303 | + * |
|
| 304 | + * @param integer $max |
|
| 305 | + */ |
|
| 306 | + private function setMax($max) |
|
| 307 | + { |
|
| 308 | + $attribute = $this->field->isOfType('number') ? 'max' : 'maxlength'; |
|
| 309 | + |
|
| 310 | + $this->field->$attribute($max); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Set a minimum value to a field |
|
| 315 | + * |
|
| 316 | + * @param integer $min |
|
| 317 | + */ |
|
| 318 | + private function setMin($min) |
|
| 319 | + { |
|
| 320 | + if ($this->field->isOfType('number') == 'min') { |
|
| 321 | + $this->field->min($min); |
|
| 322 | + } else { |
|
| 323 | + $this->field->pattern(".{".$min.",}"); |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Set a minimum and maximum value to a field |
|
| 329 | + * |
|
| 330 | + * @param $min |
|
| 331 | + * @param $max |
|
| 332 | + */ |
|
| 333 | + public function setBetween($min, $max) |
|
| 334 | + { |
|
| 335 | + if ($this->field->isOfType('number') == 'min') { |
|
| 336 | + // min, max values for generation of the pattern |
|
| 337 | + $this->field->min($min); |
|
| 338 | + $this->field->max($max); |
|
| 339 | + } else { |
|
| 340 | + $this->field->pattern('.{'.$min.','.$max.'}'); |
|
| 341 | + |
|
| 342 | + // still let the browser limit text input after reaching the max |
|
| 343 | + $this->field->maxlength($max); |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | 346 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php namespace Former\Exceptions; |
| 2 | 2 | |
| 3 | -class InvalidFrameworkException extends \RuntimeException{ |
|
| 3 | +class InvalidFrameworkException extends \RuntimeException { |
|
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | 6 | * reference to framework class |
@@ -2,33 +2,33 @@ |
||
| 2 | 2 | |
| 3 | 3 | class InvalidFrameworkException extends \RuntimeException{ |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * reference to framework class |
|
| 7 | - * |
|
| 8 | - * @var |
|
| 9 | - */ |
|
| 10 | - private $framework; |
|
| 5 | + /** |
|
| 6 | + * reference to framework class |
|
| 7 | + * |
|
| 8 | + * @var |
|
| 9 | + */ |
|
| 10 | + private $framework; |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Set framework |
|
| 14 | - * |
|
| 15 | - * @param string $framework |
|
| 16 | - * @return $this |
|
| 17 | - */ |
|
| 18 | - public function setFramework($framework) |
|
| 19 | - { |
|
| 20 | - $this->framework = $framework; |
|
| 21 | - $this->message = "Framework was not found [{$this->framework}]"; |
|
| 12 | + /** |
|
| 13 | + * Set framework |
|
| 14 | + * |
|
| 15 | + * @param string $framework |
|
| 16 | + * @return $this |
|
| 17 | + */ |
|
| 18 | + public function setFramework($framework) |
|
| 19 | + { |
|
| 20 | + $this->framework = $framework; |
|
| 21 | + $this->message = "Framework was not found [{$this->framework}]"; |
|
| 22 | 22 | |
| 23 | - return $this; |
|
| 24 | - } |
|
| 25 | - /** |
|
| 26 | - * Gets the errors object. |
|
| 27 | - * |
|
| 28 | - * @return string |
|
| 29 | - */ |
|
| 30 | - public function getFramework() |
|
| 31 | - { |
|
| 32 | - return $this->framework; |
|
| 33 | - } |
|
| 23 | + return $this; |
|
| 24 | + } |
|
| 25 | + /** |
|
| 26 | + * Gets the errors object. |
|
| 27 | + * |
|
| 28 | + * @return string |
|
| 29 | + */ |
|
| 30 | + public function getFramework() |
|
| 31 | + { |
|
| 32 | + return $this->framework; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | //Convert parametrs of old format to new format |
| 128 | 128 | if (!is_callable($text)) { |
| 129 | 129 | $optionTextValue = $text; |
| 130 | - $text = function ($model) use($optionTextValue) { |
|
| 130 | + $text = function($model) use($optionTextValue) { |
|
| 131 | 131 | if ($optionTextValue and isset($model->$optionTextValue)) { |
| 132 | 132 | return $model->$optionTextValue; |
| 133 | 133 | } elseif (method_exists($model, '__toString')) { |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | $optionAttributeValue = $modelAttributeName($model); |
| 173 | 173 | } elseif ($modelAttributeName and isset($model->$modelAttributeName)) { |
| 174 | 174 | $optionAttributeValue = $model->$modelAttributeName; |
| 175 | - } elseif($optionAttributeName === 'value') { |
|
| 175 | + } elseif ($optionAttributeName === 'value') { |
|
| 176 | 176 | //For backward compatibility |
| 177 | 177 | if (method_exists($model, 'getKey')) { |
| 178 | 178 | $optionAttributeValue = $model->getKey(); |
@@ -9,191 +9,191 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Helpers |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * The IoC Container |
|
| 14 | - * |
|
| 15 | - * @var Container |
|
| 16 | - */ |
|
| 17 | - protected static $app; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Bind a Container to the Helpers class |
|
| 21 | - * |
|
| 22 | - * @param Container $app |
|
| 23 | - */ |
|
| 24 | - public static function setApp(Container $app) |
|
| 25 | - { |
|
| 26 | - static::$app = $app; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Encodes HTML |
|
| 31 | - * |
|
| 32 | - * @param string $value The string to encode |
|
| 33 | - * |
|
| 34 | - * @return string |
|
| 35 | - */ |
|
| 36 | - public static function encode($value) |
|
| 37 | - { |
|
| 38 | - return htmlentities($value, ENT_QUOTES, 'UTF-8', true); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - //////////////////////////////////////////////////////////////////// |
|
| 42 | - ///////////////////////// LOCALIZATION HELPERS ///////////////////// |
|
| 43 | - //////////////////////////////////////////////////////////////////// |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Translates a string by trying several fallbacks |
|
| 47 | - * |
|
| 48 | - * @param string $key The key to translate |
|
| 49 | - * @param string $fallback The ultimate fallback |
|
| 50 | - * |
|
| 51 | - * @return string A translated string |
|
| 52 | - */ |
|
| 53 | - public static function translate($key, $fallback = null) |
|
| 54 | - { |
|
| 55 | - // If nothing was given, return nothing, bitch |
|
| 56 | - if (!$key) { |
|
| 57 | - return null; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - // If no fallback, use the key |
|
| 61 | - if (!$fallback) { |
|
| 62 | - $fallback = $key; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Assure we don't already have a Lang object |
|
| 66 | - if (is_object($key) and method_exists($key, 'get')) { |
|
| 67 | - return $key->get(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $translation = null; |
|
| 71 | - $translateFrom = static::$app['former']->getOption('translate_from'); |
|
| 72 | - if (substr($translateFrom, -1) !== '/') { |
|
| 73 | - $translateFrom .= '.'; |
|
| 74 | - } |
|
| 75 | - $translateFrom .= $key; |
|
| 76 | - |
|
| 77 | - // Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']] |
|
| 78 | - if (strpos($translateFrom, ']') !== false) { |
|
| 79 | - $translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - // Search for the key itself, if it is valid |
|
| 83 | - $validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1; |
|
| 84 | - if ($validKey && static::$app['translator']->has($key)) { |
|
| 85 | - $translation = static::$app['translator']->get($key); |
|
| 86 | - } elseif (static::$app['translator']->has($translateFrom)) { |
|
| 87 | - $translation = static::$app['translator']->get($translateFrom); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - // Replace by fallback if invalid |
|
| 91 | - if (!$translation or is_array($translation)) { |
|
| 92 | - $translation = $fallback; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // Capitalize |
|
| 96 | - $capitalize = static::$app['former']->getOption('capitalize_translations'); |
|
| 97 | - |
|
| 98 | - return $capitalize ? ucfirst($translation) : $translation; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - //////////////////////////////////////////////////////////////////// |
|
| 102 | - ////////////////////////// DATABASE HELPERS //////////////////////// |
|
| 103 | - //////////////////////////////////////////////////////////////////// |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Transforms an array of models into an associative array |
|
| 107 | - * |
|
| 108 | - * @param array|object $query The array of results |
|
| 109 | - * @param string|function $text The value to use as text |
|
| 110 | - * @param string|array $attributes The data to use as attributes |
|
| 111 | - * |
|
| 112 | - * @return array A data array |
|
| 113 | - */ |
|
| 114 | - public static function queryToArray($query, $text = null, $attributes = null) |
|
| 115 | - { |
|
| 116 | - // Automatically fetch Lang objects for people who store translated options lists |
|
| 117 | - // Same of unfetched queries |
|
| 118 | - if (!$query instanceof Collection) { |
|
| 119 | - if (method_exists($query, 'get')) { |
|
| 120 | - $query = $query->get(); |
|
| 121 | - } |
|
| 122 | - if (!is_array($query)) { |
|
| 123 | - $query = (array) $query; |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - //Convert parametrs of old format to new format |
|
| 128 | - if (!is_callable($text)) { |
|
| 129 | - $optionTextValue = $text; |
|
| 130 | - $text = function ($model) use($optionTextValue) { |
|
| 131 | - if ($optionTextValue and isset($model->$optionTextValue)) { |
|
| 132 | - return $model->$optionTextValue; |
|
| 133 | - } elseif (method_exists($model, '__toString')) { |
|
| 134 | - return $model->__toString(); |
|
| 135 | - } else { |
|
| 136 | - return null; |
|
| 137 | - } |
|
| 138 | - }; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - if (!is_array($attributes)) { |
|
| 142 | - if (is_string($attributes)) { |
|
| 143 | - $attributes = ['value' => $attributes]; |
|
| 144 | - } else { |
|
| 145 | - $attributes = ['value' => null]; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if (!isset($attributes['value'])) { |
|
| 150 | - $attributes['value'] = null; |
|
| 151 | - } |
|
| 152 | - //------------------------------------------------- |
|
| 153 | - |
|
| 154 | - // Populates the new options |
|
| 155 | - foreach ($query as $model) { |
|
| 156 | - // If it's an array, convert to object |
|
| 157 | - if (is_array($model)) { |
|
| 158 | - $model = (object) $model; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // Calculate option text |
|
| 162 | - $optionText = $text($model); |
|
| 163 | - |
|
| 164 | - // Skip if no text value found |
|
| 165 | - if (!$optionText) { |
|
| 166 | - continue; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - //Collect option attributes |
|
| 170 | - foreach ($attributes as $optionAttributeName => $modelAttributeName) { |
|
| 171 | - if (is_callable($modelAttributeName)) { |
|
| 172 | - $optionAttributeValue = $modelAttributeName($model); |
|
| 173 | - } elseif ($modelAttributeName and isset($model->$modelAttributeName)) { |
|
| 174 | - $optionAttributeValue = $model->$modelAttributeName; |
|
| 175 | - } elseif($optionAttributeName === 'value') { |
|
| 176 | - //For backward compatibility |
|
| 177 | - if (method_exists($model, 'getKey')) { |
|
| 178 | - $optionAttributeValue = $model->getKey(); |
|
| 179 | - } elseif (isset($model->id)) { |
|
| 180 | - $optionAttributeValue = $model->id; |
|
| 181 | - } else { |
|
| 182 | - $optionAttributeValue = $optionText; |
|
| 183 | - } |
|
| 184 | - } else { |
|
| 185 | - $optionAttributeValue = ''; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - //For backward compatibility |
|
| 189 | - if (count($attributes) === 1) { |
|
| 190 | - $array[$optionAttributeValue] = (string) $optionText; |
|
| 191 | - } else { |
|
| 192 | - $array[$optionText][$optionAttributeName] = (string) $optionAttributeValue; |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return !empty($array) ? $array : $query; |
|
| 198 | - } |
|
| 12 | + /** |
|
| 13 | + * The IoC Container |
|
| 14 | + * |
|
| 15 | + * @var Container |
|
| 16 | + */ |
|
| 17 | + protected static $app; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Bind a Container to the Helpers class |
|
| 21 | + * |
|
| 22 | + * @param Container $app |
|
| 23 | + */ |
|
| 24 | + public static function setApp(Container $app) |
|
| 25 | + { |
|
| 26 | + static::$app = $app; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Encodes HTML |
|
| 31 | + * |
|
| 32 | + * @param string $value The string to encode |
|
| 33 | + * |
|
| 34 | + * @return string |
|
| 35 | + */ |
|
| 36 | + public static function encode($value) |
|
| 37 | + { |
|
| 38 | + return htmlentities($value, ENT_QUOTES, 'UTF-8', true); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + //////////////////////////////////////////////////////////////////// |
|
| 42 | + ///////////////////////// LOCALIZATION HELPERS ///////////////////// |
|
| 43 | + //////////////////////////////////////////////////////////////////// |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Translates a string by trying several fallbacks |
|
| 47 | + * |
|
| 48 | + * @param string $key The key to translate |
|
| 49 | + * @param string $fallback The ultimate fallback |
|
| 50 | + * |
|
| 51 | + * @return string A translated string |
|
| 52 | + */ |
|
| 53 | + public static function translate($key, $fallback = null) |
|
| 54 | + { |
|
| 55 | + // If nothing was given, return nothing, bitch |
|
| 56 | + if (!$key) { |
|
| 57 | + return null; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + // If no fallback, use the key |
|
| 61 | + if (!$fallback) { |
|
| 62 | + $fallback = $key; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Assure we don't already have a Lang object |
|
| 66 | + if (is_object($key) and method_exists($key, 'get')) { |
|
| 67 | + return $key->get(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $translation = null; |
|
| 71 | + $translateFrom = static::$app['former']->getOption('translate_from'); |
|
| 72 | + if (substr($translateFrom, -1) !== '/') { |
|
| 73 | + $translateFrom .= '.'; |
|
| 74 | + } |
|
| 75 | + $translateFrom .= $key; |
|
| 76 | + |
|
| 77 | + // Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']] |
|
| 78 | + if (strpos($translateFrom, ']') !== false) { |
|
| 79 | + $translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + // Search for the key itself, if it is valid |
|
| 83 | + $validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1; |
|
| 84 | + if ($validKey && static::$app['translator']->has($key)) { |
|
| 85 | + $translation = static::$app['translator']->get($key); |
|
| 86 | + } elseif (static::$app['translator']->has($translateFrom)) { |
|
| 87 | + $translation = static::$app['translator']->get($translateFrom); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + // Replace by fallback if invalid |
|
| 91 | + if (!$translation or is_array($translation)) { |
|
| 92 | + $translation = $fallback; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // Capitalize |
|
| 96 | + $capitalize = static::$app['former']->getOption('capitalize_translations'); |
|
| 97 | + |
|
| 98 | + return $capitalize ? ucfirst($translation) : $translation; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + //////////////////////////////////////////////////////////////////// |
|
| 102 | + ////////////////////////// DATABASE HELPERS //////////////////////// |
|
| 103 | + //////////////////////////////////////////////////////////////////// |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Transforms an array of models into an associative array |
|
| 107 | + * |
|
| 108 | + * @param array|object $query The array of results |
|
| 109 | + * @param string|function $text The value to use as text |
|
| 110 | + * @param string|array $attributes The data to use as attributes |
|
| 111 | + * |
|
| 112 | + * @return array A data array |
|
| 113 | + */ |
|
| 114 | + public static function queryToArray($query, $text = null, $attributes = null) |
|
| 115 | + { |
|
| 116 | + // Automatically fetch Lang objects for people who store translated options lists |
|
| 117 | + // Same of unfetched queries |
|
| 118 | + if (!$query instanceof Collection) { |
|
| 119 | + if (method_exists($query, 'get')) { |
|
| 120 | + $query = $query->get(); |
|
| 121 | + } |
|
| 122 | + if (!is_array($query)) { |
|
| 123 | + $query = (array) $query; |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + //Convert parametrs of old format to new format |
|
| 128 | + if (!is_callable($text)) { |
|
| 129 | + $optionTextValue = $text; |
|
| 130 | + $text = function ($model) use($optionTextValue) { |
|
| 131 | + if ($optionTextValue and isset($model->$optionTextValue)) { |
|
| 132 | + return $model->$optionTextValue; |
|
| 133 | + } elseif (method_exists($model, '__toString')) { |
|
| 134 | + return $model->__toString(); |
|
| 135 | + } else { |
|
| 136 | + return null; |
|
| 137 | + } |
|
| 138 | + }; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + if (!is_array($attributes)) { |
|
| 142 | + if (is_string($attributes)) { |
|
| 143 | + $attributes = ['value' => $attributes]; |
|
| 144 | + } else { |
|
| 145 | + $attributes = ['value' => null]; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if (!isset($attributes['value'])) { |
|
| 150 | + $attributes['value'] = null; |
|
| 151 | + } |
|
| 152 | + //------------------------------------------------- |
|
| 153 | + |
|
| 154 | + // Populates the new options |
|
| 155 | + foreach ($query as $model) { |
|
| 156 | + // If it's an array, convert to object |
|
| 157 | + if (is_array($model)) { |
|
| 158 | + $model = (object) $model; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // Calculate option text |
|
| 162 | + $optionText = $text($model); |
|
| 163 | + |
|
| 164 | + // Skip if no text value found |
|
| 165 | + if (!$optionText) { |
|
| 166 | + continue; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + //Collect option attributes |
|
| 170 | + foreach ($attributes as $optionAttributeName => $modelAttributeName) { |
|
| 171 | + if (is_callable($modelAttributeName)) { |
|
| 172 | + $optionAttributeValue = $modelAttributeName($model); |
|
| 173 | + } elseif ($modelAttributeName and isset($model->$modelAttributeName)) { |
|
| 174 | + $optionAttributeValue = $model->$modelAttributeName; |
|
| 175 | + } elseif($optionAttributeName === 'value') { |
|
| 176 | + //For backward compatibility |
|
| 177 | + if (method_exists($model, 'getKey')) { |
|
| 178 | + $optionAttributeValue = $model->getKey(); |
|
| 179 | + } elseif (isset($model->id)) { |
|
| 180 | + $optionAttributeValue = $model->id; |
|
| 181 | + } else { |
|
| 182 | + $optionAttributeValue = $optionText; |
|
| 183 | + } |
|
| 184 | + } else { |
|
| 185 | + $optionAttributeValue = ''; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + //For backward compatibility |
|
| 189 | + if (count($attributes) === 1) { |
|
| 190 | + $array[$optionAttributeValue] = (string) $optionText; |
|
| 191 | + } else { |
|
| 192 | + $array[$optionText][$optionAttributeName] = (string) $optionAttributeValue; |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return !empty($array) ? $array : $query; |
|
| 198 | + } |
|
| 199 | 199 | } |
@@ -51,7 +51,7 @@ |
||
| 51 | 51 | */ |
| 52 | 52 | public function getContent() |
| 53 | 53 | { |
| 54 | - $content = array_map(function ($content) { |
|
| 54 | + $content = array_map(function($content) { |
|
| 55 | 55 | return method_exists($content, '__toString') ? (string) $content : $content; |
| 56 | 56 | }, $this->value); |
| 57 | 57 | |
@@ -11,110 +11,110 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Actions extends FormerObject |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * The Container |
|
| 16 | - * |
|
| 17 | - * @var Container |
|
| 18 | - */ |
|
| 19 | - protected $app; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The Actions element |
|
| 23 | - * |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $element = 'div'; |
|
| 27 | - |
|
| 28 | - //////////////////////////////////////////////////////////////////// |
|
| 29 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 30 | - //////////////////////////////////////////////////////////////////// |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Constructs a new Actions block |
|
| 34 | - * |
|
| 35 | - * @param Container $app |
|
| 36 | - * @param array $value The block content |
|
| 37 | - */ |
|
| 38 | - public function __construct(Container $app, $value) |
|
| 39 | - { |
|
| 40 | - $this->app = $app; |
|
| 41 | - $this->value = $value; |
|
| 42 | - |
|
| 43 | - // Add specific actions classes to the actions block |
|
| 44 | - $this->addClass($this->app['former.framework']->getActionClasses()); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Get the content of the Actions block |
|
| 49 | - * |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function getContent() |
|
| 53 | - { |
|
| 54 | - $content = array_map(function ($content) { |
|
| 55 | - return method_exists($content, '__toString') ? (string) $content : $content; |
|
| 56 | - }, $this->value); |
|
| 57 | - |
|
| 58 | - return $this->app['former.framework']->wrapActions(implode(' ', $content)); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Dynamically append actions to the block |
|
| 63 | - * |
|
| 64 | - * @param string $method The method |
|
| 65 | - * @param array $parameters Its parameters |
|
| 66 | - * |
|
| 67 | - * @return Actions |
|
| 68 | - */ |
|
| 69 | - public function __call($method, $parameters) |
|
| 70 | - { |
|
| 71 | - // Dynamically add buttons to an actions block |
|
| 72 | - if ($this->isButtonMethod($method)) { |
|
| 73 | - $text = array_get($parameters, 0); |
|
| 74 | - $link = array_get($parameters, 1); |
|
| 75 | - $attributes = array_get($parameters, 2); |
|
| 76 | - if (!$attributes and is_array($link)) { |
|
| 77 | - $attributes = $link; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - return $this->createButtonOfType($method, $text, $link, $attributes); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return parent::__call($method, $parameters); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - //////////////////////////////////////////////////////////////////// |
|
| 87 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 88 | - //////////////////////////////////////////////////////////////////// |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Create a new Button and add it to the actions |
|
| 92 | - * |
|
| 93 | - * @param string $type The button type |
|
| 94 | - * @param string $name Its name |
|
| 95 | - * @param string $link A link to point to |
|
| 96 | - * @param array $attributes Its attributes |
|
| 97 | - * |
|
| 98 | - * @return Actions |
|
| 99 | - */ |
|
| 100 | - private function createButtonOfType($type, $name, $link, $attributes) |
|
| 101 | - { |
|
| 102 | - $this->value[] = $this->app['former']->$type($name, $link, $attributes)->__toString(); |
|
| 103 | - |
|
| 104 | - return $this; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Check if a given method calls a button or not |
|
| 109 | - * |
|
| 110 | - * @param string $method The method to check |
|
| 111 | - * |
|
| 112 | - * @return boolean |
|
| 113 | - */ |
|
| 114 | - private function isButtonMethod($method) |
|
| 115 | - { |
|
| 116 | - $buttons = array('button', 'submit', 'link', 'reset'); |
|
| 117 | - |
|
| 118 | - return (bool) Str::contains($method, $buttons); |
|
| 119 | - } |
|
| 14 | + /** |
|
| 15 | + * The Container |
|
| 16 | + * |
|
| 17 | + * @var Container |
|
| 18 | + */ |
|
| 19 | + protected $app; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The Actions element |
|
| 23 | + * |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $element = 'div'; |
|
| 27 | + |
|
| 28 | + //////////////////////////////////////////////////////////////////// |
|
| 29 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 30 | + //////////////////////////////////////////////////////////////////// |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Constructs a new Actions block |
|
| 34 | + * |
|
| 35 | + * @param Container $app |
|
| 36 | + * @param array $value The block content |
|
| 37 | + */ |
|
| 38 | + public function __construct(Container $app, $value) |
|
| 39 | + { |
|
| 40 | + $this->app = $app; |
|
| 41 | + $this->value = $value; |
|
| 42 | + |
|
| 43 | + // Add specific actions classes to the actions block |
|
| 44 | + $this->addClass($this->app['former.framework']->getActionClasses()); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Get the content of the Actions block |
|
| 49 | + * |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function getContent() |
|
| 53 | + { |
|
| 54 | + $content = array_map(function ($content) { |
|
| 55 | + return method_exists($content, '__toString') ? (string) $content : $content; |
|
| 56 | + }, $this->value); |
|
| 57 | + |
|
| 58 | + return $this->app['former.framework']->wrapActions(implode(' ', $content)); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Dynamically append actions to the block |
|
| 63 | + * |
|
| 64 | + * @param string $method The method |
|
| 65 | + * @param array $parameters Its parameters |
|
| 66 | + * |
|
| 67 | + * @return Actions |
|
| 68 | + */ |
|
| 69 | + public function __call($method, $parameters) |
|
| 70 | + { |
|
| 71 | + // Dynamically add buttons to an actions block |
|
| 72 | + if ($this->isButtonMethod($method)) { |
|
| 73 | + $text = array_get($parameters, 0); |
|
| 74 | + $link = array_get($parameters, 1); |
|
| 75 | + $attributes = array_get($parameters, 2); |
|
| 76 | + if (!$attributes and is_array($link)) { |
|
| 77 | + $attributes = $link; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + return $this->createButtonOfType($method, $text, $link, $attributes); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return parent::__call($method, $parameters); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + //////////////////////////////////////////////////////////////////// |
|
| 87 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 88 | + //////////////////////////////////////////////////////////////////// |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Create a new Button and add it to the actions |
|
| 92 | + * |
|
| 93 | + * @param string $type The button type |
|
| 94 | + * @param string $name Its name |
|
| 95 | + * @param string $link A link to point to |
|
| 96 | + * @param array $attributes Its attributes |
|
| 97 | + * |
|
| 98 | + * @return Actions |
|
| 99 | + */ |
|
| 100 | + private function createButtonOfType($type, $name, $link, $attributes) |
|
| 101 | + { |
|
| 102 | + $this->value[] = $this->app['former']->$type($name, $link, $attributes)->__toString(); |
|
| 103 | + |
|
| 104 | + return $this; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Check if a given method calls a button or not |
|
| 109 | + * |
|
| 110 | + * @param string $method The method to check |
|
| 111 | + * |
|
| 112 | + * @return boolean |
|
| 113 | + */ |
|
| 114 | + private function isButtonMethod($method) |
|
| 115 | + { |
|
| 116 | + $buttons = array('button', 'submit', 'link', 'reset'); |
|
| 117 | + |
|
| 118 | + return (bool) Str::contains($method, $buttons); |
|
| 119 | + } |
|
| 120 | 120 | } |