@@ -188,6 +188,9 @@ discard block |
||
| 188 | 188 | return $element; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | + /** |
|
| 192 | + * @param string $choiceType |
|
| 193 | + */ |
|
| 191 | 194 | public function getCheckables($choiceType) |
| 192 | 195 | { |
| 193 | 196 | if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
@@ -292,7 +295,7 @@ discard block |
||
| 292 | 295 | /** |
| 293 | 296 | * Sets the element's type based on options |
| 294 | 297 | * |
| 295 | - * @return this |
|
| 298 | + * @return Choice |
|
| 296 | 299 | */ |
| 297 | 300 | protected function setChoiceType() |
| 298 | 301 | { |
@@ -309,7 +312,6 @@ discard block |
||
| 309 | 312 | /** |
| 310 | 313 | * Select a value in the field's children |
| 311 | 314 | * |
| 312 | - * @param mixed $value |
|
| 313 | 315 | * |
| 314 | 316 | * @return bool |
| 315 | 317 | */ |
@@ -333,10 +335,6 @@ discard block |
||
| 333 | 335 | /** |
| 334 | 336 | * Set the choices |
| 335 | 337 | * |
| 336 | - * @param array $_choices The choices as an array |
|
| 337 | - * @param mixed $selected Facultative selected entry |
|
| 338 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 339 | - * the option's values instead of the array's keys |
|
| 340 | 338 | */ |
| 341 | 339 | public function addChoice($value, $key = null) |
| 342 | 340 | { |
@@ -349,7 +347,6 @@ discard block |
||
| 349 | 347 | * Set the choices |
| 350 | 348 | * |
| 351 | 349 | * @param array $_choices The choices as an array |
| 352 | - * @param mixed $selected Facultative selected entry |
|
| 353 | 350 | * @param boolean $valuesAsKeys Whether the array's values should be used as |
| 354 | 351 | * the option's values instead of the array's keys |
| 355 | 352 | */ |
@@ -12,477 +12,477 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Choice extends Field |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Renders the checkables as inline |
|
| 17 | - * |
|
| 18 | - * @var boolean |
|
| 19 | - */ |
|
| 20 | - protected $inline = false; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The choice's placeholder |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - private $placeholder = null; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The choice's options |
|
| 31 | - * |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - protected $options = [ |
|
| 15 | + /** |
|
| 16 | + * Renders the checkables as inline |
|
| 17 | + * |
|
| 18 | + * @var boolean |
|
| 19 | + */ |
|
| 20 | + protected $inline = false; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The choice's placeholder |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + private $placeholder = null; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The choice's options |
|
| 31 | + * |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + protected $options = [ |
|
| 35 | 35 | 'isMultiple' => false, |
| 36 | 36 | 'isExpanded' => false, |
| 37 | 37 | ]; |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The choice's choices |
|
| 41 | - * |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - protected $choices = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The choice's element |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - protected $element = 'choice'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The choice's self-closing state |
|
| 55 | - * |
|
| 56 | - * @var boolean |
|
| 57 | - */ |
|
| 58 | - protected $isSelfClosing = false; |
|
| 59 | - |
|
| 60 | - //////////////////////////////////////////////////////////////////// |
|
| 61 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 62 | - //////////////////////////////////////////////////////////////////// |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Easier arguments order for selects |
|
| 66 | - * |
|
| 67 | - * @param Container $app The Container instance |
|
| 68 | - * @param string $type select |
|
| 69 | - * @param string $name Field name |
|
| 70 | - * @param string $label Its label |
|
| 71 | - * @param array $choices The choice's choices |
|
| 72 | - * @param string $selected The selected choice(s) |
|
| 73 | - * @param array $attributes Attributes |
|
| 74 | - */ |
|
| 75 | - public function __construct(Container $app, $type, $name, $label, $choices, $selected, $attributes) |
|
| 76 | - { |
|
| 77 | - if ($selected) { |
|
| 78 | - $this->value = $selected; |
|
| 79 | - } |
|
| 80 | - if ($choices) { |
|
| 81 | - $this->choices($choices); |
|
| 82 | - } |
|
| 39 | + /** |
|
| 40 | + * The choice's choices |
|
| 41 | + * |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + protected $choices = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The choice's element |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + protected $element = 'choice'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The choice's self-closing state |
|
| 55 | + * |
|
| 56 | + * @var boolean |
|
| 57 | + */ |
|
| 58 | + protected $isSelfClosing = false; |
|
| 59 | + |
|
| 60 | + //////////////////////////////////////////////////////////////////// |
|
| 61 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 62 | + //////////////////////////////////////////////////////////////////// |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Easier arguments order for selects |
|
| 66 | + * |
|
| 67 | + * @param Container $app The Container instance |
|
| 68 | + * @param string $type select |
|
| 69 | + * @param string $name Field name |
|
| 70 | + * @param string $label Its label |
|
| 71 | + * @param array $choices The choice's choices |
|
| 72 | + * @param string $selected The selected choice(s) |
|
| 73 | + * @param array $attributes Attributes |
|
| 74 | + */ |
|
| 75 | + public function __construct(Container $app, $type, $name, $label, $choices, $selected, $attributes) |
|
| 76 | + { |
|
| 77 | + if ($selected) { |
|
| 78 | + $this->value = $selected; |
|
| 79 | + } |
|
| 80 | + if ($choices) { |
|
| 81 | + $this->choices($choices); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 84 | + parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 85 | 85 | |
| 86 | - $this->setChoiceType(); |
|
| 87 | - |
|
| 88 | - // Nested models population |
|
| 89 | - if (str_contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 90 | - $this->fromQuery($this->value); |
|
| 91 | - $this->value = $selected ?: null; |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Renders the choice |
|
| 97 | - * |
|
| 98 | - * @return string A <select> tag |
|
| 99 | - */ |
|
| 100 | - public function render() |
|
| 101 | - { |
|
| 102 | - $choiceType = $this->getType(); |
|
| 103 | - $this->setFieldClasses(); |
|
| 104 | - |
|
| 105 | - if (!isset($this->attributes['id'])) { |
|
| 106 | - $this->setAttribute('id', $this->name); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - switch ($choiceType) { |
|
| 110 | - case 'select': |
|
| 111 | - $field = $this->getSelect(); |
|
| 112 | - break; |
|
| 113 | - case 'checkbox': |
|
| 114 | - case 'radio': |
|
| 115 | - $field = $this->getCheckables($choiceType); |
|
| 116 | - break; |
|
| 117 | - } |
|
| 118 | - $this->value = null; |
|
| 119 | - $content = $field->render(); |
|
| 120 | - return $content; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - public function getSelect() |
|
| 124 | - { |
|
| 125 | - $field = Element::create('select', null, $this->attributes); |
|
| 126 | - |
|
| 127 | - $name = $this->name; |
|
| 128 | - if ($this->options['isMultiple']) { |
|
| 129 | - $field->multiple(); |
|
| 130 | - $name .= '[]'; |
|
| 131 | - } |
|
| 86 | + $this->setChoiceType(); |
|
| 87 | + |
|
| 88 | + // Nested models population |
|
| 89 | + if (str_contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 90 | + $this->fromQuery($this->value); |
|
| 91 | + $this->value = $selected ?: null; |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Renders the choice |
|
| 97 | + * |
|
| 98 | + * @return string A <select> tag |
|
| 99 | + */ |
|
| 100 | + public function render() |
|
| 101 | + { |
|
| 102 | + $choiceType = $this->getType(); |
|
| 103 | + $this->setFieldClasses(); |
|
| 104 | + |
|
| 105 | + if (!isset($this->attributes['id'])) { |
|
| 106 | + $this->setAttribute('id', $this->name); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + switch ($choiceType) { |
|
| 110 | + case 'select': |
|
| 111 | + $field = $this->getSelect(); |
|
| 112 | + break; |
|
| 113 | + case 'checkbox': |
|
| 114 | + case 'radio': |
|
| 115 | + $field = $this->getCheckables($choiceType); |
|
| 116 | + break; |
|
| 117 | + } |
|
| 118 | + $this->value = null; |
|
| 119 | + $content = $field->render(); |
|
| 120 | + return $content; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + public function getSelect() |
|
| 124 | + { |
|
| 125 | + $field = Element::create('select', null, $this->attributes); |
|
| 126 | + |
|
| 127 | + $name = $this->name; |
|
| 128 | + if ($this->options['isMultiple']) { |
|
| 129 | + $field->multiple(); |
|
| 130 | + $name .= '[]'; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - $field->setAttribute('name', $name); |
|
| 133 | + $field->setAttribute('name', $name); |
|
| 134 | 134 | |
| 135 | - $field->nest($this->getOptions()); |
|
| 135 | + $field->nest($this->getOptions()); |
|
| 136 | 136 | |
| 137 | - return $field; |
|
| 138 | - } |
|
| 137 | + return $field; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - public function getOptions() |
|
| 141 | - { |
|
| 142 | - $children = []; |
|
| 140 | + public function getOptions() |
|
| 141 | + { |
|
| 142 | + $children = []; |
|
| 143 | 143 | |
| 144 | - // Add placeholder text if any |
|
| 145 | - if ($placeholder = $this->getPlaceholder()) { |
|
| 146 | - $children[] = $placeholder; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - foreach ($this->choices as $key => $value) { |
|
| 150 | - if (is_array($value) and !isset($value['value'])) { |
|
| 151 | - $children[] = $this->getOptGroup($key, $value); |
|
| 152 | - } else { |
|
| 153 | - $children[] = $this->getOption($key, $value); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - return $children; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - public function getOptGroup($label, $options) |
|
| 160 | - { |
|
| 161 | - $element = Element::create('optgroup')->label($label); |
|
| 162 | - $children = []; |
|
| 163 | - foreach ($options as $key => $value) { |
|
| 164 | - $option = $this->getOption($key, $value); |
|
| 165 | - $children[] = $option; |
|
| 166 | - } |
|
| 167 | - $element->nest($children); |
|
| 168 | - |
|
| 169 | - return $element; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - public function getOption($key, $value) |
|
| 173 | - { |
|
| 174 | - if (is_array($value) and isset($value['value'])) { |
|
| 175 | - $attributes = $value; |
|
| 176 | - $text = $key; |
|
| 177 | - $key = null; |
|
| 178 | - } else { |
|
| 179 | - $attributes = array('value' => $key); |
|
| 180 | - $text = $value; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $element = Element::create('option', $text, $attributes); |
|
| 184 | - if ($this->hasValue($attributes['value'])) { |
|
| 185 | - $element->selected('selected'); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $element; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - public function getCheckables($choiceType) |
|
| 192 | - { |
|
| 193 | - if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
|
| 194 | - $this->value = explode(',', $this->value); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $disabled = isset($this->attributes['disabled']); |
|
| 198 | - unset($this->attributes['disabled']); |
|
| 199 | - |
|
| 200 | - $field = Element::create('div', null, $this->attributes); |
|
| 201 | - |
|
| 202 | - $children = []; |
|
| 203 | - foreach ($this->choices as $key => $value) { |
|
| 204 | - $attributes = []; |
|
| 205 | - |
|
| 206 | - if (is_array($value) and isset($value['value'])) { |
|
| 207 | - $attributes = $value; |
|
| 208 | - $label = $key; |
|
| 209 | - $inputValue = $value['value']; |
|
| 210 | - } else { |
|
| 211 | - $attributes = []; |
|
| 212 | - $label = $value; |
|
| 213 | - $inputValue = $key; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if ($disabled) { |
|
| 217 | - $attributes['disabled'] = true; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - if(isset($attributes['name'])) { |
|
| 221 | - $name = $attributes['name']; |
|
| 222 | - unset($attributes['name']); |
|
| 223 | - } else { |
|
| 224 | - $name = $this->name; |
|
| 225 | - } |
|
| 226 | - if ($this->options['isMultiple']) { |
|
| 227 | - $name .= '[]'; |
|
| 228 | - } |
|
| 144 | + // Add placeholder text if any |
|
| 145 | + if ($placeholder = $this->getPlaceholder()) { |
|
| 146 | + $children[] = $placeholder; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + foreach ($this->choices as $key => $value) { |
|
| 150 | + if (is_array($value) and !isset($value['value'])) { |
|
| 151 | + $children[] = $this->getOptGroup($key, $value); |
|
| 152 | + } else { |
|
| 153 | + $children[] = $this->getOption($key, $value); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + return $children; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + public function getOptGroup($label, $options) |
|
| 160 | + { |
|
| 161 | + $element = Element::create('optgroup')->label($label); |
|
| 162 | + $children = []; |
|
| 163 | + foreach ($options as $key => $value) { |
|
| 164 | + $option = $this->getOption($key, $value); |
|
| 165 | + $children[] = $option; |
|
| 166 | + } |
|
| 167 | + $element->nest($children); |
|
| 168 | + |
|
| 169 | + return $element; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + public function getOption($key, $value) |
|
| 173 | + { |
|
| 174 | + if (is_array($value) and isset($value['value'])) { |
|
| 175 | + $attributes = $value; |
|
| 176 | + $text = $key; |
|
| 177 | + $key = null; |
|
| 178 | + } else { |
|
| 179 | + $attributes = array('value' => $key); |
|
| 180 | + $text = $value; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $element = Element::create('option', $text, $attributes); |
|
| 184 | + if ($this->hasValue($attributes['value'])) { |
|
| 185 | + $element->selected('selected'); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $element; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + public function getCheckables($choiceType) |
|
| 192 | + { |
|
| 193 | + if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
|
| 194 | + $this->value = explode(',', $this->value); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $disabled = isset($this->attributes['disabled']); |
|
| 198 | + unset($this->attributes['disabled']); |
|
| 199 | + |
|
| 200 | + $field = Element::create('div', null, $this->attributes); |
|
| 201 | + |
|
| 202 | + $children = []; |
|
| 203 | + foreach ($this->choices as $key => $value) { |
|
| 204 | + $attributes = []; |
|
| 205 | + |
|
| 206 | + if (is_array($value) and isset($value['value'])) { |
|
| 207 | + $attributes = $value; |
|
| 208 | + $label = $key; |
|
| 209 | + $inputValue = $value['value']; |
|
| 210 | + } else { |
|
| 211 | + $attributes = []; |
|
| 212 | + $label = $value; |
|
| 213 | + $inputValue = $key; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if ($disabled) { |
|
| 217 | + $attributes['disabled'] = true; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + if(isset($attributes['name'])) { |
|
| 221 | + $name = $attributes['name']; |
|
| 222 | + unset($attributes['name']); |
|
| 223 | + } else { |
|
| 224 | + $name = $this->name; |
|
| 225 | + } |
|
| 226 | + if ($this->options['isMultiple']) { |
|
| 227 | + $name .= '[]'; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - if(!isset($attributes['id'])) { |
|
| 231 | - $attributes['id'] = $this->id.'_'.count($children); |
|
| 232 | - } |
|
| 230 | + if(!isset($attributes['id'])) { |
|
| 231 | + $attributes['id'] = $this->id.'_'.count($children); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - // If inline items, add class |
|
| 235 | - $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 234 | + // If inline items, add class |
|
| 235 | + $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 236 | 236 | |
| 237 | - // In Bootsrap 3, don't append the the checkable type (radio/checkbox) as a class if |
|
| 238 | - // rendering inline. |
|
| 239 | - $class = $this->app['former']->framework() == 'TwitterBootstrap3' ? trim($isInline) : $choiceType.$isInline; |
|
| 237 | + // In Bootsrap 3, don't append the the checkable type (radio/checkbox) as a class if |
|
| 238 | + // rendering inline. |
|
| 239 | + $class = $this->app['former']->framework() == 'TwitterBootstrap3' ? trim($isInline) : $choiceType.$isInline; |
|
| 240 | 240 | |
| 241 | - $element = Input::create($choiceType, $name, $inputValue, $attributes); |
|
| 241 | + $element = Input::create($choiceType, $name, $inputValue, $attributes); |
|
| 242 | 242 | |
| 243 | - // $element->setAttribute('name', $name); |
|
| 244 | - |
|
| 245 | - if ($this->hasValue($inputValue)) { |
|
| 246 | - $element->checked('checked'); |
|
| 247 | - } |
|
| 248 | - // $attributes['value'] = $key; |
|
| 249 | - if (!$label) { |
|
| 250 | - $element = (is_object($field)) ? $field->render() : $field; |
|
| 251 | - } else { |
|
| 252 | - $rendered = $element->render(); |
|
| 253 | - $labelElement = Element::create('label', $rendered.$label); |
|
| 254 | - $element = $labelElement->for($attributes['id'])->class($class); |
|
| 255 | - } |
|
| 243 | + // $element->setAttribute('name', $name); |
|
| 244 | + |
|
| 245 | + if ($this->hasValue($inputValue)) { |
|
| 246 | + $element->checked('checked'); |
|
| 247 | + } |
|
| 248 | + // $attributes['value'] = $key; |
|
| 249 | + if (!$label) { |
|
| 250 | + $element = (is_object($field)) ? $field->render() : $field; |
|
| 251 | + } else { |
|
| 252 | + $rendered = $element->render(); |
|
| 253 | + $labelElement = Element::create('label', $rendered.$label); |
|
| 254 | + $element = $labelElement->for($attributes['id'])->class($class); |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 258 | - if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 259 | - $wrapper = Element::create('div', $element->render())->class($choiceType); |
|
| 260 | - if ($disabled) { |
|
| 261 | - $wrapper->addClass('disabled'); |
|
| 262 | - } |
|
| 263 | - $element = $wrapper; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $children[] = $element; |
|
| 267 | - } |
|
| 268 | - $field->nest($children); |
|
| 257 | + // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 258 | + if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 259 | + $wrapper = Element::create('div', $element->render())->class($choiceType); |
|
| 260 | + if ($disabled) { |
|
| 261 | + $wrapper->addClass('disabled'); |
|
| 262 | + } |
|
| 263 | + $element = $wrapper; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $children[] = $element; |
|
| 267 | + } |
|
| 268 | + $field->nest($children); |
|
| 269 | 269 | |
| 270 | - return $field; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Get the choice's placeholder |
|
| 275 | - * |
|
| 276 | - * @return Element |
|
| 277 | - */ |
|
| 278 | - protected function getPlaceholder() |
|
| 279 | - { |
|
| 280 | - if (!$this->placeholder) { |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 285 | - if (!(array)$this->value) { |
|
| 286 | - $attributes['selected'] = 'selected'; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - return Element::create('option', $this->placeholder, $attributes); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Sets the element's type based on options |
|
| 294 | - * |
|
| 295 | - * @return this |
|
| 296 | - */ |
|
| 297 | - protected function setChoiceType() |
|
| 298 | - { |
|
| 299 | - if ($this->options['isExpanded'] && !$this->options['isMultiple']) { |
|
| 300 | - $this->setType('radio'); |
|
| 301 | - } elseif ($this->options['isExpanded'] && $this->options['isMultiple']) { |
|
| 302 | - $this->setType('checkbox'); |
|
| 303 | - } else { |
|
| 304 | - $this->setType('select'); |
|
| 305 | - } |
|
| 306 | - return $this; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Select a value in the field's children |
|
| 311 | - * |
|
| 312 | - * @param mixed $value |
|
| 313 | - * |
|
| 314 | - * @return bool |
|
| 315 | - */ |
|
| 316 | - protected function hasValue($choiceValue) |
|
| 317 | - { |
|
| 318 | - foreach ((array)$this->value as $key => $value) { |
|
| 319 | - if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 320 | - $value = $value->getKey(); |
|
| 321 | - } |
|
| 322 | - if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 323 | - return true; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - return false; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - //////////////////////////////////////////////////////////////////// |
|
| 330 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 331 | - //////////////////////////////////////////////////////////////////// |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Set the choices |
|
| 335 | - * |
|
| 336 | - * @param array $_choices The choices as an array |
|
| 337 | - * @param mixed $selected Facultative selected entry |
|
| 338 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 339 | - * the option's values instead of the array's keys |
|
| 340 | - */ |
|
| 341 | - public function addChoice($value, $key = null) |
|
| 342 | - { |
|
| 343 | - $this->choices[$key ?? $value] = $value; |
|
| 344 | - |
|
| 345 | - return $this; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Set the choices |
|
| 350 | - * |
|
| 351 | - * @param array $_choices The choices as an array |
|
| 352 | - * @param mixed $selected Facultative selected entry |
|
| 353 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 354 | - * the option's values instead of the array's keys |
|
| 355 | - */ |
|
| 356 | - public function choices($_choices, $valuesAsKeys = false) |
|
| 357 | - { |
|
| 358 | - $choices = (array) $_choices; |
|
| 359 | - |
|
| 360 | - // If valuesAsKeys is true, use the values as keys |
|
| 361 | - if ($valuesAsKeys) { |
|
| 362 | - foreach ($choices as $value) { |
|
| 363 | - $this->addChoice($value, $value); |
|
| 364 | - } |
|
| 365 | - } else { |
|
| 366 | - foreach ($choices as $key => $value) { |
|
| 367 | - $this->addChoice($value, $key); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return $this; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Creates a list of choices from a range |
|
| 376 | - * |
|
| 377 | - * @param integer $from |
|
| 378 | - * @param integer $to |
|
| 379 | - * @param integer $step |
|
| 380 | - */ |
|
| 381 | - public function range($from, $to, $step = 1) |
|
| 382 | - { |
|
| 383 | - $range = range($from, $to, $step); |
|
| 384 | - $this->choices($range, true); |
|
| 385 | - |
|
| 386 | - return $this; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Use the results from a Fluent/Eloquent query as choices |
|
| 391 | - * |
|
| 392 | - * @param array $results An array of Eloquent models |
|
| 393 | - * @param string|function $text The value to use as text |
|
| 394 | - * @param string|array $attributes The data to use as attributes |
|
| 395 | - * @param string $selected The default selected item |
|
| 396 | - */ |
|
| 397 | - public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 398 | - { |
|
| 399 | - $this->choices(Helpers::queryToArray($results, $text, $attributes))->value($selected); |
|
| 400 | - |
|
| 401 | - return $this; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Add a placeholder to the current select |
|
| 406 | - * |
|
| 407 | - * @param string $placeholder The placeholder text |
|
| 408 | - */ |
|
| 409 | - public function placeholder($placeholder) |
|
| 410 | - { |
|
| 411 | - $this->placeholder = Helpers::translate($placeholder); |
|
| 412 | - |
|
| 413 | - return $this; |
|
| 414 | - } |
|
| 270 | + return $field; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Get the choice's placeholder |
|
| 275 | + * |
|
| 276 | + * @return Element |
|
| 277 | + */ |
|
| 278 | + protected function getPlaceholder() |
|
| 279 | + { |
|
| 280 | + if (!$this->placeholder) { |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 285 | + if (!(array)$this->value) { |
|
| 286 | + $attributes['selected'] = 'selected'; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + return Element::create('option', $this->placeholder, $attributes); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Sets the element's type based on options |
|
| 294 | + * |
|
| 295 | + * @return this |
|
| 296 | + */ |
|
| 297 | + protected function setChoiceType() |
|
| 298 | + { |
|
| 299 | + if ($this->options['isExpanded'] && !$this->options['isMultiple']) { |
|
| 300 | + $this->setType('radio'); |
|
| 301 | + } elseif ($this->options['isExpanded'] && $this->options['isMultiple']) { |
|
| 302 | + $this->setType('checkbox'); |
|
| 303 | + } else { |
|
| 304 | + $this->setType('select'); |
|
| 305 | + } |
|
| 306 | + return $this; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Select a value in the field's children |
|
| 311 | + * |
|
| 312 | + * @param mixed $value |
|
| 313 | + * |
|
| 314 | + * @return bool |
|
| 315 | + */ |
|
| 316 | + protected function hasValue($choiceValue) |
|
| 317 | + { |
|
| 318 | + foreach ((array)$this->value as $key => $value) { |
|
| 319 | + if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 320 | + $value = $value->getKey(); |
|
| 321 | + } |
|
| 322 | + if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 323 | + return true; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + return false; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + //////////////////////////////////////////////////////////////////// |
|
| 330 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 331 | + //////////////////////////////////////////////////////////////////// |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Set the choices |
|
| 335 | + * |
|
| 336 | + * @param array $_choices The choices as an array |
|
| 337 | + * @param mixed $selected Facultative selected entry |
|
| 338 | + * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 339 | + * the option's values instead of the array's keys |
|
| 340 | + */ |
|
| 341 | + public function addChoice($value, $key = null) |
|
| 342 | + { |
|
| 343 | + $this->choices[$key ?? $value] = $value; |
|
| 344 | + |
|
| 345 | + return $this; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Set the choices |
|
| 350 | + * |
|
| 351 | + * @param array $_choices The choices as an array |
|
| 352 | + * @param mixed $selected Facultative selected entry |
|
| 353 | + * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 354 | + * the option's values instead of the array's keys |
|
| 355 | + */ |
|
| 356 | + public function choices($_choices, $valuesAsKeys = false) |
|
| 357 | + { |
|
| 358 | + $choices = (array) $_choices; |
|
| 359 | + |
|
| 360 | + // If valuesAsKeys is true, use the values as keys |
|
| 361 | + if ($valuesAsKeys) { |
|
| 362 | + foreach ($choices as $value) { |
|
| 363 | + $this->addChoice($value, $value); |
|
| 364 | + } |
|
| 365 | + } else { |
|
| 366 | + foreach ($choices as $key => $value) { |
|
| 367 | + $this->addChoice($value, $key); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return $this; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Creates a list of choices from a range |
|
| 376 | + * |
|
| 377 | + * @param integer $from |
|
| 378 | + * @param integer $to |
|
| 379 | + * @param integer $step |
|
| 380 | + */ |
|
| 381 | + public function range($from, $to, $step = 1) |
|
| 382 | + { |
|
| 383 | + $range = range($from, $to, $step); |
|
| 384 | + $this->choices($range, true); |
|
| 385 | + |
|
| 386 | + return $this; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Use the results from a Fluent/Eloquent query as choices |
|
| 391 | + * |
|
| 392 | + * @param array $results An array of Eloquent models |
|
| 393 | + * @param string|function $text The value to use as text |
|
| 394 | + * @param string|array $attributes The data to use as attributes |
|
| 395 | + * @param string $selected The default selected item |
|
| 396 | + */ |
|
| 397 | + public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 398 | + { |
|
| 399 | + $this->choices(Helpers::queryToArray($results, $text, $attributes))->value($selected); |
|
| 400 | + |
|
| 401 | + return $this; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Add a placeholder to the current select |
|
| 406 | + * |
|
| 407 | + * @param string $placeholder The placeholder text |
|
| 408 | + */ |
|
| 409 | + public function placeholder($placeholder) |
|
| 410 | + { |
|
| 411 | + $this->placeholder = Helpers::translate($placeholder); |
|
| 412 | + |
|
| 413 | + return $this; |
|
| 414 | + } |
|
| 415 | 415 | |
| 416 | - /** |
|
| 417 | - * Set isMultiple |
|
| 418 | - * |
|
| 419 | - * @param boolean $isMultiple |
|
| 420 | - * @return $this |
|
| 421 | - */ |
|
| 422 | - public function multiple($isMultiple = true) |
|
| 423 | - { |
|
| 424 | - $this->options['isMultiple'] = $isMultiple; |
|
| 425 | - $this->setChoiceType(); |
|
| 426 | - |
|
| 427 | - return $this; |
|
| 428 | - } |
|
| 416 | + /** |
|
| 417 | + * Set isMultiple |
|
| 418 | + * |
|
| 419 | + * @param boolean $isMultiple |
|
| 420 | + * @return $this |
|
| 421 | + */ |
|
| 422 | + public function multiple($isMultiple = true) |
|
| 423 | + { |
|
| 424 | + $this->options['isMultiple'] = $isMultiple; |
|
| 425 | + $this->setChoiceType(); |
|
| 426 | + |
|
| 427 | + return $this; |
|
| 428 | + } |
|
| 429 | 429 | |
| 430 | - /** |
|
| 431 | - * Set isExpanded |
|
| 432 | - * |
|
| 433 | - * @param boolean $isExpanded |
|
| 434 | - * @return $this |
|
| 435 | - */ |
|
| 436 | - public function expanded($isExpanded = true) |
|
| 437 | - { |
|
| 438 | - $this->options['isExpanded'] = $isExpanded; |
|
| 439 | - $this->setChoiceType(); |
|
| 440 | - |
|
| 441 | - return $this; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Set the choices as inline (for expanded items) |
|
| 446 | - */ |
|
| 447 | - public function inline($isInline = true) |
|
| 448 | - { |
|
| 449 | - $this->inline = $isInline; |
|
| 450 | - |
|
| 451 | - return $this; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Set the choices as stacked (for expanded items) |
|
| 456 | - */ |
|
| 457 | - public function stacked($isStacked = true) |
|
| 458 | - { |
|
| 459 | - $this->inline = !$isStacked; |
|
| 460 | - |
|
| 461 | - return $this; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Check if field is a checkbox or a radio |
|
| 466 | - * |
|
| 467 | - * @return boolean |
|
| 468 | - */ |
|
| 469 | - public function isCheckable() |
|
| 470 | - { |
|
| 471 | - return $this->options['isExpanded']; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - //////////////////////////////////////////////////////////////////// |
|
| 475 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 476 | - //////////////////////////////////////////////////////////////////// |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Returns the current choices in memory for manipulations |
|
| 480 | - * |
|
| 481 | - * @return array The current choices array |
|
| 482 | - */ |
|
| 483 | - public function getChoices() |
|
| 484 | - { |
|
| 485 | - return $this->choices; |
|
| 486 | - } |
|
| 430 | + /** |
|
| 431 | + * Set isExpanded |
|
| 432 | + * |
|
| 433 | + * @param boolean $isExpanded |
|
| 434 | + * @return $this |
|
| 435 | + */ |
|
| 436 | + public function expanded($isExpanded = true) |
|
| 437 | + { |
|
| 438 | + $this->options['isExpanded'] = $isExpanded; |
|
| 439 | + $this->setChoiceType(); |
|
| 440 | + |
|
| 441 | + return $this; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Set the choices as inline (for expanded items) |
|
| 446 | + */ |
|
| 447 | + public function inline($isInline = true) |
|
| 448 | + { |
|
| 449 | + $this->inline = $isInline; |
|
| 450 | + |
|
| 451 | + return $this; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Set the choices as stacked (for expanded items) |
|
| 456 | + */ |
|
| 457 | + public function stacked($isStacked = true) |
|
| 458 | + { |
|
| 459 | + $this->inline = !$isStacked; |
|
| 460 | + |
|
| 461 | + return $this; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Check if field is a checkbox or a radio |
|
| 466 | + * |
|
| 467 | + * @return boolean |
|
| 468 | + */ |
|
| 469 | + public function isCheckable() |
|
| 470 | + { |
|
| 471 | + return $this->options['isExpanded']; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + //////////////////////////////////////////////////////////////////// |
|
| 475 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 476 | + //////////////////////////////////////////////////////////////////// |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Returns the current choices in memory for manipulations |
|
| 480 | + * |
|
| 481 | + * @return array The current choices array |
|
| 482 | + */ |
|
| 483 | + public function getChoices() |
|
| 484 | + { |
|
| 485 | + return $this->choices; |
|
| 486 | + } |
|
| 487 | 487 | |
| 488 | 488 | } |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | $attributes['disabled'] = true; |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - if(isset($attributes['name'])) { |
|
| 220 | + if (isset($attributes['name'])) { |
|
| 221 | 221 | $name = $attributes['name']; |
| 222 | 222 | unset($attributes['name']); |
| 223 | 223 | } else { |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | $name .= '[]'; |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | - if(!isset($attributes['id'])) { |
|
| 230 | + if (!isset($attributes['id'])) { |
|
| 231 | 231 | $attributes['id'] = $this->id.'_'.count($children); |
| 232 | 232 | } |
| 233 | 233 | |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | $attributes = array('value' => '', 'disabled' => 'disabled'); |
| 285 | - if (!(array)$this->value) { |
|
| 285 | + if (!(array) $this->value) { |
|
| 286 | 286 | $attributes['selected'] = 'selected'; |
| 287 | 287 | } |
| 288 | 288 | |
@@ -315,11 +315,11 @@ discard block |
||
| 315 | 315 | */ |
| 316 | 316 | protected function hasValue($choiceValue) |
| 317 | 317 | { |
| 318 | - foreach ((array)$this->value as $key => $value) { |
|
| 318 | + foreach ((array) $this->value as $key => $value) { |
|
| 319 | 319 | if (is_object($value) && method_exists($value, 'getKey')) { |
| 320 | 320 | $value = $value->getKey(); |
| 321 | 321 | } |
| 322 | - if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 322 | + if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int) $value) { |
|
| 323 | 323 | return true; |
| 324 | 324 | } |
| 325 | 325 | } |
@@ -181,7 +181,7 @@ |
||
| 181 | 181 | /** |
| 182 | 182 | * Set the modifiers from initial method call |
| 183 | 183 | * |
| 184 | - * @return $this |
|
| 184 | + * @return string |
|
| 185 | 185 | */ |
| 186 | 186 | public function getModifiers() |
| 187 | 187 | { |
@@ -10,192 +10,192 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | abstract class FormerObject extends Element |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The field's name |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - protected $name; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The field type |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected $type; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A list of class properties to be added to attributes |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - protected $injectedProperties = array('name'); |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The field's modifiers from method call |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - protected $modifiers; |
|
| 40 | - |
|
| 41 | - //////////////////////////////////////////////////////////////////// |
|
| 42 | - /////////////////////////// ID AND LABELS ////////////////////////// |
|
| 43 | - //////////////////////////////////////////////////////////////////// |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Create an unique ID and return it |
|
| 47 | - * |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function getCreatedId() |
|
| 51 | - { |
|
| 52 | - $this->setId(); |
|
| 53 | - |
|
| 54 | - return $this->attributes['id']; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Set the matching ID on a field if possible |
|
| 59 | - */ |
|
| 60 | - protected function setId() |
|
| 61 | - { |
|
| 62 | - if (!array_key_exists('id', $this->attributes) and |
|
| 63 | - in_array($this->name, $this->app['former']->labels) |
|
| 64 | - ) { |
|
| 65 | - // Set and save the field's ID |
|
| 66 | - $id = $this->getUniqueId($this->name); |
|
| 67 | - $this->attributes['id'] = $id; |
|
| 68 | - $this->app['former']->ids[] = $id; |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Get an unique ID for a field from its name |
|
| 74 | - * |
|
| 75 | - * @param string $name |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - protected function getUniqueId($name) |
|
| 80 | - { |
|
| 81 | - $names = &$this->app['former']->names; |
|
| 82 | - |
|
| 83 | - if (array_key_exists($name, $names)) { |
|
| 84 | - $count = $names[$name] + 1; |
|
| 85 | - $names[$name] = $count; |
|
| 86 | - return $name . '-' . $count; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $names[$name] = 1; |
|
| 90 | - return $name; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Runs the frameworks getFieldClasses method on this |
|
| 95 | - * |
|
| 96 | - * @return $this |
|
| 97 | - */ |
|
| 98 | - protected function setFieldClasses() |
|
| 99 | - { |
|
| 100 | - $framework = isset($this->app['former.form.framework']) ? $this->app['former.form.framework'] : $this->app['former.framework']; |
|
| 101 | - $framework->getFieldClasses($this, $this->modifiers); |
|
| 102 | - |
|
| 103 | - return $this; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Render the FormerObject and set its id |
|
| 108 | - * |
|
| 109 | - * @return string |
|
| 110 | - */ |
|
| 111 | - public function render() |
|
| 112 | - { |
|
| 113 | - // Set the proper ID according to the label |
|
| 114 | - $this->setId(); |
|
| 115 | - |
|
| 116 | - if($this instanceof Field) { |
|
| 117 | - $this->setFieldClasses(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // Encode HTML value |
|
| 121 | - $isButton = ($this instanceof Field) ? $this->isButton() : false; |
|
| 122 | - if (!$isButton and is_string($this->value)) { |
|
| 123 | - $this->value = Helpers::encode($this->value); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return parent::render(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - //////////////////////////////////////////////////////////////////// |
|
| 130 | - ////////////////////////////// GETTERS ///////////////////////////// |
|
| 131 | - //////////////////////////////////////////////////////////////////// |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Get the object's name |
|
| 135 | - * |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public function getName() |
|
| 139 | - { |
|
| 140 | - return $this->name; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - //////////////////////////////////////////////////////////////////// |
|
| 144 | - //////////////////////////// OBJECT TYPE /////////////////////////// |
|
| 145 | - //////////////////////////////////////////////////////////////////// |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Get the object's type |
|
| 149 | - * |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function getType() |
|
| 153 | - { |
|
| 154 | - return $this->type; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Change a object's type |
|
| 159 | - * |
|
| 160 | - * @param string $type |
|
| 161 | - */ |
|
| 162 | - public function setType($type) |
|
| 163 | - { |
|
| 164 | - $this->type = $type; |
|
| 165 | - |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Check if an object is of a certain type |
|
| 171 | - * |
|
| 172 | - * @return boolean |
|
| 173 | - */ |
|
| 174 | - public function isOfType() |
|
| 175 | - { |
|
| 176 | - $types = func_get_args(); |
|
| 177 | - |
|
| 178 | - return in_array($this->type, $types); |
|
| 179 | - } |
|
| 13 | + /** |
|
| 14 | + * The field's name |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + protected $name; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The field type |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected $type; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A list of class properties to be added to attributes |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + protected $injectedProperties = array('name'); |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The field's modifiers from method call |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + protected $modifiers; |
|
| 40 | + |
|
| 41 | + //////////////////////////////////////////////////////////////////// |
|
| 42 | + /////////////////////////// ID AND LABELS ////////////////////////// |
|
| 43 | + //////////////////////////////////////////////////////////////////// |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Create an unique ID and return it |
|
| 47 | + * |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function getCreatedId() |
|
| 51 | + { |
|
| 52 | + $this->setId(); |
|
| 53 | + |
|
| 54 | + return $this->attributes['id']; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Set the matching ID on a field if possible |
|
| 59 | + */ |
|
| 60 | + protected function setId() |
|
| 61 | + { |
|
| 62 | + if (!array_key_exists('id', $this->attributes) and |
|
| 63 | + in_array($this->name, $this->app['former']->labels) |
|
| 64 | + ) { |
|
| 65 | + // Set and save the field's ID |
|
| 66 | + $id = $this->getUniqueId($this->name); |
|
| 67 | + $this->attributes['id'] = $id; |
|
| 68 | + $this->app['former']->ids[] = $id; |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Get an unique ID for a field from its name |
|
| 74 | + * |
|
| 75 | + * @param string $name |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + protected function getUniqueId($name) |
|
| 80 | + { |
|
| 81 | + $names = &$this->app['former']->names; |
|
| 82 | + |
|
| 83 | + if (array_key_exists($name, $names)) { |
|
| 84 | + $count = $names[$name] + 1; |
|
| 85 | + $names[$name] = $count; |
|
| 86 | + return $name . '-' . $count; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $names[$name] = 1; |
|
| 90 | + return $name; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Runs the frameworks getFieldClasses method on this |
|
| 95 | + * |
|
| 96 | + * @return $this |
|
| 97 | + */ |
|
| 98 | + protected function setFieldClasses() |
|
| 99 | + { |
|
| 100 | + $framework = isset($this->app['former.form.framework']) ? $this->app['former.form.framework'] : $this->app['former.framework']; |
|
| 101 | + $framework->getFieldClasses($this, $this->modifiers); |
|
| 102 | + |
|
| 103 | + return $this; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Render the FormerObject and set its id |
|
| 108 | + * |
|
| 109 | + * @return string |
|
| 110 | + */ |
|
| 111 | + public function render() |
|
| 112 | + { |
|
| 113 | + // Set the proper ID according to the label |
|
| 114 | + $this->setId(); |
|
| 115 | + |
|
| 116 | + if($this instanceof Field) { |
|
| 117 | + $this->setFieldClasses(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // Encode HTML value |
|
| 121 | + $isButton = ($this instanceof Field) ? $this->isButton() : false; |
|
| 122 | + if (!$isButton and is_string($this->value)) { |
|
| 123 | + $this->value = Helpers::encode($this->value); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return parent::render(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + //////////////////////////////////////////////////////////////////// |
|
| 130 | + ////////////////////////////// GETTERS ///////////////////////////// |
|
| 131 | + //////////////////////////////////////////////////////////////////// |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Get the object's name |
|
| 135 | + * |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public function getName() |
|
| 139 | + { |
|
| 140 | + return $this->name; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + //////////////////////////////////////////////////////////////////// |
|
| 144 | + //////////////////////////// OBJECT TYPE /////////////////////////// |
|
| 145 | + //////////////////////////////////////////////////////////////////// |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Get the object's type |
|
| 149 | + * |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function getType() |
|
| 153 | + { |
|
| 154 | + return $this->type; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Change a object's type |
|
| 159 | + * |
|
| 160 | + * @param string $type |
|
| 161 | + */ |
|
| 162 | + public function setType($type) |
|
| 163 | + { |
|
| 164 | + $this->type = $type; |
|
| 165 | + |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Check if an object is of a certain type |
|
| 171 | + * |
|
| 172 | + * @return boolean |
|
| 173 | + */ |
|
| 174 | + public function isOfType() |
|
| 175 | + { |
|
| 176 | + $types = func_get_args(); |
|
| 177 | + |
|
| 178 | + return in_array($this->type, $types); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Set the modifiers from initial method call |
|
| 183 | - * |
|
| 184 | - * @return $this |
|
| 185 | - */ |
|
| 186 | - public function getModifiers() |
|
| 187 | - { |
|
| 188 | - return $this->modifiers; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Set the modifiers from initial method call |
|
| 193 | - * |
|
| 194 | - * @return $this |
|
| 195 | - */ |
|
| 196 | - public function setModifiers($modifiers) |
|
| 197 | - { |
|
| 198 | - $this->modifiers = $modifiers; |
|
| 199 | - return $this; |
|
| 200 | - } |
|
| 181 | + /** |
|
| 182 | + * Set the modifiers from initial method call |
|
| 183 | + * |
|
| 184 | + * @return $this |
|
| 185 | + */ |
|
| 186 | + public function getModifiers() |
|
| 187 | + { |
|
| 188 | + return $this->modifiers; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Set the modifiers from initial method call |
|
| 193 | + * |
|
| 194 | + * @return $this |
|
| 195 | + */ |
|
| 196 | + public function setModifiers($modifiers) |
|
| 197 | + { |
|
| 198 | + $this->modifiers = $modifiers; |
|
| 199 | + return $this; |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | if (array_key_exists($name, $names)) { |
| 84 | 84 | $count = $names[$name] + 1; |
| 85 | 85 | $names[$name] = $count; |
| 86 | - return $name . '-' . $count; |
|
| 86 | + return $name.'-'.$count; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | $names[$name] = 1; |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | // Set the proper ID according to the label |
| 114 | 114 | $this->setId(); |
| 115 | 115 | |
| 116 | - if($this instanceof Field) { |
|
| 116 | + if ($this instanceof Field) { |
|
| 117 | 117 | $this->setFieldClasses(); |
| 118 | 118 | } |
| 119 | 119 | |
@@ -13,483 +13,483 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Former |
| 15 | 15 | { |
| 16 | - // Instances |
|
| 17 | - //////////////////////////////////////////////////////////////////// |
|
| 18 | - |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The current environment |
|
| 22 | - * |
|
| 23 | - * @var \Illuminate\Container\Container |
|
| 24 | - */ |
|
| 25 | - protected $app; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The Method Dispatcher |
|
| 29 | - * |
|
| 30 | - * @var MethodDispatcher |
|
| 31 | - */ |
|
| 32 | - protected $dispatch; |
|
| 33 | - |
|
| 34 | - // Informations |
|
| 35 | - //////////////////////////////////////////////////////////////////// |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The form's errors |
|
| 39 | - * |
|
| 40 | - * @var Message |
|
| 41 | - */ |
|
| 42 | - protected $errors; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * An array of rules to use |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - protected $rules = array(); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * An array of field macros |
|
| 53 | - * |
|
| 54 | - * @var array |
|
| 55 | - */ |
|
| 56 | - protected $macros = array(); |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * The labels created so far |
|
| 60 | - * |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - public $labels = array(); |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * The IDs created so far |
|
| 67 | - * |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - public $ids = array(); |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * A lookup table where the key is the input name, |
|
| 74 | - * and the value is number of times seen. This is |
|
| 75 | - * used to calculate unique ids. |
|
| 76 | - * |
|
| 77 | - * @var array |
|
| 78 | - */ |
|
| 79 | - public $names = array(); |
|
| 80 | - |
|
| 81 | - // Namespaces |
|
| 82 | - //////////////////////////////////////////////////////////////////// |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The namespace of Form elements |
|
| 86 | - */ |
|
| 87 | - const FORMSPACE = 'Former\Form\\'; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * The namespace of fields |
|
| 91 | - */ |
|
| 92 | - const FIELDSPACE = 'Former\Form\Fields\\'; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Build a new Former instance |
|
| 96 | - * |
|
| 97 | - * @param Container $app |
|
| 98 | - * @param MethodDispatcher $dispatcher |
|
| 99 | - */ |
|
| 100 | - public function __construct(Container $app, MethodDispatcher $dispatcher) |
|
| 101 | - { |
|
| 102 | - $this->app = $app; |
|
| 103 | - $this->dispatch = $dispatcher; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - //////////////////////////////////////////////////////////////////// |
|
| 107 | - //////////////////////////// INTERFACE ///////////////////////////// |
|
| 108 | - //////////////////////////////////////////////////////////////////// |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Acts as a router that redirects methods to all of Former classes |
|
| 112 | - * |
|
| 113 | - * @param string $method The method called |
|
| 114 | - * @param array $parameters An array of parameters |
|
| 115 | - * |
|
| 116 | - * @return mixed |
|
| 117 | - */ |
|
| 118 | - public function __call($method, $parameters) |
|
| 119 | - { |
|
| 120 | - // Dispatch to Form\Elements |
|
| 121 | - // Explicitly check false since closeGroup() may return an empty string |
|
| 122 | - if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { |
|
| 123 | - return $element; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - // Dispatch to Form\Form |
|
| 127 | - if ($form = $this->dispatch->toForm($method, $parameters)) { |
|
| 128 | - $this->app->instance('former.form', $form); |
|
| 129 | - |
|
| 130 | - return $this->app['former.form']; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // Dispatch to Form\Group |
|
| 134 | - if ($group = $this->dispatch->toGroup($method, $parameters)) { |
|
| 135 | - return $group; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Dispatch to Form\Actions |
|
| 139 | - if ($actions = $this->dispatch->toActions($method, $parameters)) { |
|
| 140 | - return $actions; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Dispatch to macros |
|
| 144 | - if ($macro = $this->dispatch->toMacros($method, $parameters)) { |
|
| 145 | - return $macro; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - // Checking for any supplementary classes |
|
| 149 | - $modifiers = explode('_', $method); |
|
| 150 | - $method = array_pop($modifiers); |
|
| 16 | + // Instances |
|
| 17 | + //////////////////////////////////////////////////////////////////// |
|
| 18 | + |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The current environment |
|
| 22 | + * |
|
| 23 | + * @var \Illuminate\Container\Container |
|
| 24 | + */ |
|
| 25 | + protected $app; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The Method Dispatcher |
|
| 29 | + * |
|
| 30 | + * @var MethodDispatcher |
|
| 31 | + */ |
|
| 32 | + protected $dispatch; |
|
| 33 | + |
|
| 34 | + // Informations |
|
| 35 | + //////////////////////////////////////////////////////////////////// |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The form's errors |
|
| 39 | + * |
|
| 40 | + * @var Message |
|
| 41 | + */ |
|
| 42 | + protected $errors; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * An array of rules to use |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + protected $rules = array(); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * An array of field macros |
|
| 53 | + * |
|
| 54 | + * @var array |
|
| 55 | + */ |
|
| 56 | + protected $macros = array(); |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * The labels created so far |
|
| 60 | + * |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + public $labels = array(); |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * The IDs created so far |
|
| 67 | + * |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + public $ids = array(); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * A lookup table where the key is the input name, |
|
| 74 | + * and the value is number of times seen. This is |
|
| 75 | + * used to calculate unique ids. |
|
| 76 | + * |
|
| 77 | + * @var array |
|
| 78 | + */ |
|
| 79 | + public $names = array(); |
|
| 80 | + |
|
| 81 | + // Namespaces |
|
| 82 | + //////////////////////////////////////////////////////////////////// |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The namespace of Form elements |
|
| 86 | + */ |
|
| 87 | + const FORMSPACE = 'Former\Form\\'; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * The namespace of fields |
|
| 91 | + */ |
|
| 92 | + const FIELDSPACE = 'Former\Form\Fields\\'; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Build a new Former instance |
|
| 96 | + * |
|
| 97 | + * @param Container $app |
|
| 98 | + * @param MethodDispatcher $dispatcher |
|
| 99 | + */ |
|
| 100 | + public function __construct(Container $app, MethodDispatcher $dispatcher) |
|
| 101 | + { |
|
| 102 | + $this->app = $app; |
|
| 103 | + $this->dispatch = $dispatcher; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + //////////////////////////////////////////////////////////////////// |
|
| 107 | + //////////////////////////// INTERFACE ///////////////////////////// |
|
| 108 | + //////////////////////////////////////////////////////////////////// |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Acts as a router that redirects methods to all of Former classes |
|
| 112 | + * |
|
| 113 | + * @param string $method The method called |
|
| 114 | + * @param array $parameters An array of parameters |
|
| 115 | + * |
|
| 116 | + * @return mixed |
|
| 117 | + */ |
|
| 118 | + public function __call($method, $parameters) |
|
| 119 | + { |
|
| 120 | + // Dispatch to Form\Elements |
|
| 121 | + // Explicitly check false since closeGroup() may return an empty string |
|
| 122 | + if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { |
|
| 123 | + return $element; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + // Dispatch to Form\Form |
|
| 127 | + if ($form = $this->dispatch->toForm($method, $parameters)) { |
|
| 128 | + $this->app->instance('former.form', $form); |
|
| 129 | + |
|
| 130 | + return $this->app['former.form']; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // Dispatch to Form\Group |
|
| 134 | + if ($group = $this->dispatch->toGroup($method, $parameters)) { |
|
| 135 | + return $group; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Dispatch to Form\Actions |
|
| 139 | + if ($actions = $this->dispatch->toActions($method, $parameters)) { |
|
| 140 | + return $actions; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Dispatch to macros |
|
| 144 | + if ($macro = $this->dispatch->toMacros($method, $parameters)) { |
|
| 145 | + return $macro; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + // Checking for any supplementary classes |
|
| 149 | + $modifiers = explode('_', $method); |
|
| 150 | + $method = array_pop($modifiers); |
|
| 151 | 151 | |
| 152 | - // Dispatch to the different Form\Fields |
|
| 153 | - $field = $this->dispatch->toFields($method, $parameters); |
|
| 154 | - $field->setModifiers($modifiers); |
|
| 155 | - $field->addClass(''); |
|
| 152 | + // Dispatch to the different Form\Fields |
|
| 153 | + $field = $this->dispatch->toFields($method, $parameters); |
|
| 154 | + $field->setModifiers($modifiers); |
|
| 155 | + $field->addClass(''); |
|
| 156 | 156 | |
| 157 | - // Else bind field |
|
| 158 | - $this->app->instance('former.field', $field); |
|
| 159 | - |
|
| 160 | - return $this->app['former.field']; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - //////////////////////////////////////////////////////////////////// |
|
| 164 | - //////////////////////////////// MACROS //////////////////////////// |
|
| 165 | - //////////////////////////////////////////////////////////////////// |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Register a macro with Former |
|
| 169 | - * |
|
| 170 | - * @param string $name The name of the macro |
|
| 171 | - * @param Callable $macro The macro itself |
|
| 172 | - * |
|
| 173 | - * @return mixed |
|
| 174 | - */ |
|
| 175 | - public function macro($name, $macro) |
|
| 176 | - { |
|
| 177 | - $this->macros[$name] = $macro; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Check if a macro exists |
|
| 182 | - * |
|
| 183 | - * @param string $name |
|
| 184 | - * |
|
| 185 | - * @return boolean |
|
| 186 | - */ |
|
| 187 | - public function hasMacro($name) |
|
| 188 | - { |
|
| 189 | - return isset($this->macros[$name]); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Get a registered macro |
|
| 194 | - * |
|
| 195 | - * @param string $name |
|
| 196 | - * |
|
| 197 | - * @return Closure |
|
| 198 | - */ |
|
| 199 | - public function getMacro($name) |
|
| 200 | - { |
|
| 201 | - return $this->macros[$name]; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - //////////////////////////////////////////////////////////////////// |
|
| 205 | - ///////////////////////////// POPULATOR //////////////////////////// |
|
| 206 | - //////////////////////////////////////////////////////////////////// |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Add values to populate the array |
|
| 210 | - * |
|
| 211 | - * @param mixed $values Can be an Eloquent object or an array |
|
| 212 | - */ |
|
| 213 | - public function populate($values) |
|
| 214 | - { |
|
| 215 | - $this->app['former.populator']->replace($values); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Set the value of a particular field |
|
| 220 | - * |
|
| 221 | - * @param string $field The field's name |
|
| 222 | - * @param mixed $value Its new value |
|
| 223 | - */ |
|
| 224 | - public function populateField($field, $value) |
|
| 225 | - { |
|
| 226 | - $this->app['former.populator']->put($field, $value); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Get the value of a field |
|
| 231 | - * |
|
| 232 | - * @param string $field The field's name |
|
| 233 | - * @param null $fallback |
|
| 234 | - * |
|
| 235 | - * @return mixed |
|
| 236 | - */ |
|
| 237 | - public function getValue($field, $fallback = null) |
|
| 238 | - { |
|
| 239 | - return $this->app['former.populator']->get($field, $fallback); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Fetch a field value from both the new and old POST array |
|
| 244 | - * |
|
| 245 | - * @param string $name A field name |
|
| 246 | - * @param string $fallback A fallback if nothing was found |
|
| 247 | - * |
|
| 248 | - * @return string The results |
|
| 249 | - */ |
|
| 250 | - public function getPost($name, $fallback = null) |
|
| 251 | - { |
|
| 252 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 253 | - $name = trim($name, '.'); |
|
| 254 | - $oldValue = $this->app['request']->old($name, $fallback); |
|
| 255 | - |
|
| 256 | - return $this->app['request']->input($name, $oldValue, true); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - //////////////////////////////////////////////////////////////////// |
|
| 260 | - ////////////////////////////// TOOLKIT ///////////////////////////// |
|
| 261 | - //////////////////////////////////////////////////////////////////// |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Set the errors to use for validations |
|
| 265 | - * |
|
| 266 | - * @param Message $validator The result from a validation |
|
| 267 | - * |
|
| 268 | - * @return void |
|
| 269 | - */ |
|
| 270 | - public function withErrors($validator = null) |
|
| 271 | - { |
|
| 272 | - // Try to get the errors form the session |
|
| 273 | - if ($this->app['session']->has('errors')) { |
|
| 274 | - $this->errors = $this->app['session']->get('errors'); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - // If we're given a raw Validator, go fetch the errors in it |
|
| 278 | - if ($validator instanceof Validator) { |
|
| 279 | - $this->errors = $validator->getMessageBag(); |
|
| 280 | - } else { |
|
| 281 | - if ($validator instanceof MessageBag) { |
|
| 282 | - $this->errors = $validator; |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - return $this->errors; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Add live validation rules |
|
| 291 | - * |
|
| 292 | - * @param array *$rules An array of Laravel rules |
|
| 293 | - * |
|
| 294 | - * @return void |
|
| 295 | - */ |
|
| 296 | - public function withRules() |
|
| 297 | - { |
|
| 298 | - $rules = call_user_func_array('array_merge', func_get_args()); |
|
| 299 | - |
|
| 300 | - // Parse the rules according to Laravel conventions |
|
| 301 | - foreach ($rules as $name => $fieldRules) { |
|
| 302 | - $expFieldRules = $fieldRules; |
|
| 303 | - if (!is_array($expFieldRules)) { |
|
| 304 | - $expFieldRules = explode('|', $expFieldRules); |
|
| 305 | - $expFieldRules = array_map('trim', $expFieldRules); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - foreach ($expFieldRules as $rule) { |
|
| 309 | - |
|
| 310 | - $parameters = null; |
|
| 311 | - |
|
| 312 | - if (($colon = strpos($rule, ':')) !== false) { |
|
| 313 | - $rulename = substr($rule, 0, $colon); |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 317 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 318 | - */ |
|
| 319 | - if ($rulename !== 'regex') { |
|
| 320 | - $parameters = str_getcsv(substr($rule, $colon + 1)); |
|
| 321 | - } else { |
|
| 322 | - $parameters = [substr($rule, $colon + 1)]; |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - // Exclude unsupported rules |
|
| 327 | - $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule; |
|
| 328 | - |
|
| 329 | - // Store processed rule in Former's array |
|
| 330 | - if (!isset($parameters)) { |
|
| 331 | - $parameters = array(); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $this->rules[$name][$rule] = $parameters; |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Switch the framework used by Former |
|
| 341 | - * |
|
| 342 | - * @param string $framework The name of the framework to use |
|
| 343 | - */ |
|
| 344 | - public function framework($framework = null) |
|
| 345 | - { |
|
| 346 | - if (!$framework) { |
|
| 347 | - return $this->app['former.framework']->current(); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - $this->setOption('framework', $framework); |
|
| 351 | - |
|
| 352 | - $framework = $this->getFrameworkInstance($framework); |
|
| 353 | - $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 354 | - return $framework; |
|
| 355 | - }); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Get a new framework instance |
|
| 360 | - * |
|
| 361 | - * @param string $framework |
|
| 362 | - * |
|
| 363 | - * @throws Exceptions\InvalidFrameworkException |
|
| 364 | - * @return \Former\Interfaces\FrameworkInterface |
|
| 365 | - */ |
|
| 366 | - public function getFrameworkInstance($framework) |
|
| 367 | - { |
|
| 368 | - $formerClass = __NAMESPACE__.'\Framework\\'.$framework; |
|
| 369 | - |
|
| 370 | - //get interfaces of the given framework |
|
| 371 | - $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
|
| 372 | - |
|
| 373 | - if(class_exists($formerClass)) { |
|
| 374 | - $returnClass = $formerClass; |
|
| 375 | - } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 376 | - // We have some outside class, lets return it. |
|
| 377 | - $returnClass = $framework; |
|
| 378 | - } else { |
|
| 379 | - throw (new InvalidFrameworkException())->setFramework($framework); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - return new $returnClass($this->app); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Get an option from the config |
|
| 387 | - * |
|
| 388 | - * @param string $option The option |
|
| 389 | - * @param mixed $default Optional fallback |
|
| 390 | - * |
|
| 391 | - * @return mixed |
|
| 392 | - */ |
|
| 393 | - public function getOption($option, $default = null) |
|
| 394 | - { |
|
| 395 | - return $this->app['config']->get('former.'.$option, $default); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Set an option on the config |
|
| 400 | - * |
|
| 401 | - * @param string $option |
|
| 402 | - * @param string $value |
|
| 403 | - */ |
|
| 404 | - public function setOption($option, $value) |
|
| 405 | - { |
|
| 406 | - return $this->app['config']->set('former.'.$option, $value); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - //////////////////////////////////////////////////////////////////// |
|
| 410 | - ////////////////////////////// BUILDERS //////////////////////////// |
|
| 411 | - //////////////////////////////////////////////////////////////////// |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Closes a form |
|
| 415 | - * |
|
| 416 | - * @return string A form closing tag |
|
| 417 | - */ |
|
| 418 | - public function close() |
|
| 419 | - { |
|
| 420 | - if ($this->app->bound('former.form')) { |
|
| 421 | - $closing = $this->app['former.form']->close(); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - // Destroy instances |
|
| 425 | - $instances = array('former.form', 'former.form.framework'); |
|
| 426 | - foreach ($instances as $instance) { |
|
| 427 | - $this->app[$instance] = null; |
|
| 428 | - unset($this->app[$instance]); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - // Reset populator |
|
| 432 | - $this->app['former.populator']->reset(); |
|
| 433 | - |
|
| 434 | - // Reset all values |
|
| 435 | - $this->errors = null; |
|
| 436 | - $this->rules = array(); |
|
| 437 | - |
|
| 438 | - return isset($closing) ? $closing : null; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - //////////////////////////////////////////////////////////////////// |
|
| 442 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 443 | - //////////////////////////////////////////////////////////////////// |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Get the errors for the current field |
|
| 447 | - * |
|
| 448 | - * @param string $name A field name |
|
| 449 | - * |
|
| 450 | - * @return string An error message |
|
| 451 | - */ |
|
| 452 | - public function getErrors($name = null) |
|
| 453 | - { |
|
| 454 | - // Get name and translate array notation |
|
| 455 | - if (!$name and $this->app['former.field']) { |
|
| 456 | - $name = $this->app['former.field']->getName(); |
|
| 457 | - |
|
| 458 | - // Always return empty string for anonymous fields (i.e. fields with no name/id) |
|
| 459 | - if (!$name) { |
|
| 460 | - return ''; |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - if ($this->errors and $name) { |
|
| 465 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 466 | - $name = trim($name, '.'); |
|
| 467 | - |
|
| 468 | - return $this->errors->first($name); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - return $this->errors; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Get a rule from the Rules array |
|
| 476 | - * |
|
| 477 | - * @param string $name The field to fetch |
|
| 478 | - * |
|
| 479 | - * @return array An array of rules |
|
| 480 | - */ |
|
| 481 | - public function getRules($name) |
|
| 482 | - { |
|
| 483 | - // Check the rules for the name as given |
|
| 484 | - $ruleset = array_get($this->rules, $name); |
|
| 485 | - |
|
| 486 | - // If no rules found, convert to dot notation and try again |
|
| 487 | - if (is_null($ruleset)) { |
|
| 488 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 489 | - $name = trim($name, '.'); |
|
| 490 | - $ruleset = array_get($this->rules, $name); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - return $ruleset; |
|
| 494 | - } |
|
| 157 | + // Else bind field |
|
| 158 | + $this->app->instance('former.field', $field); |
|
| 159 | + |
|
| 160 | + return $this->app['former.field']; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + //////////////////////////////////////////////////////////////////// |
|
| 164 | + //////////////////////////////// MACROS //////////////////////////// |
|
| 165 | + //////////////////////////////////////////////////////////////////// |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Register a macro with Former |
|
| 169 | + * |
|
| 170 | + * @param string $name The name of the macro |
|
| 171 | + * @param Callable $macro The macro itself |
|
| 172 | + * |
|
| 173 | + * @return mixed |
|
| 174 | + */ |
|
| 175 | + public function macro($name, $macro) |
|
| 176 | + { |
|
| 177 | + $this->macros[$name] = $macro; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Check if a macro exists |
|
| 182 | + * |
|
| 183 | + * @param string $name |
|
| 184 | + * |
|
| 185 | + * @return boolean |
|
| 186 | + */ |
|
| 187 | + public function hasMacro($name) |
|
| 188 | + { |
|
| 189 | + return isset($this->macros[$name]); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Get a registered macro |
|
| 194 | + * |
|
| 195 | + * @param string $name |
|
| 196 | + * |
|
| 197 | + * @return Closure |
|
| 198 | + */ |
|
| 199 | + public function getMacro($name) |
|
| 200 | + { |
|
| 201 | + return $this->macros[$name]; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + //////////////////////////////////////////////////////////////////// |
|
| 205 | + ///////////////////////////// POPULATOR //////////////////////////// |
|
| 206 | + //////////////////////////////////////////////////////////////////// |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Add values to populate the array |
|
| 210 | + * |
|
| 211 | + * @param mixed $values Can be an Eloquent object or an array |
|
| 212 | + */ |
|
| 213 | + public function populate($values) |
|
| 214 | + { |
|
| 215 | + $this->app['former.populator']->replace($values); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Set the value of a particular field |
|
| 220 | + * |
|
| 221 | + * @param string $field The field's name |
|
| 222 | + * @param mixed $value Its new value |
|
| 223 | + */ |
|
| 224 | + public function populateField($field, $value) |
|
| 225 | + { |
|
| 226 | + $this->app['former.populator']->put($field, $value); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Get the value of a field |
|
| 231 | + * |
|
| 232 | + * @param string $field The field's name |
|
| 233 | + * @param null $fallback |
|
| 234 | + * |
|
| 235 | + * @return mixed |
|
| 236 | + */ |
|
| 237 | + public function getValue($field, $fallback = null) |
|
| 238 | + { |
|
| 239 | + return $this->app['former.populator']->get($field, $fallback); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Fetch a field value from both the new and old POST array |
|
| 244 | + * |
|
| 245 | + * @param string $name A field name |
|
| 246 | + * @param string $fallback A fallback if nothing was found |
|
| 247 | + * |
|
| 248 | + * @return string The results |
|
| 249 | + */ |
|
| 250 | + public function getPost($name, $fallback = null) |
|
| 251 | + { |
|
| 252 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 253 | + $name = trim($name, '.'); |
|
| 254 | + $oldValue = $this->app['request']->old($name, $fallback); |
|
| 255 | + |
|
| 256 | + return $this->app['request']->input($name, $oldValue, true); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + //////////////////////////////////////////////////////////////////// |
|
| 260 | + ////////////////////////////// TOOLKIT ///////////////////////////// |
|
| 261 | + //////////////////////////////////////////////////////////////////// |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Set the errors to use for validations |
|
| 265 | + * |
|
| 266 | + * @param Message $validator The result from a validation |
|
| 267 | + * |
|
| 268 | + * @return void |
|
| 269 | + */ |
|
| 270 | + public function withErrors($validator = null) |
|
| 271 | + { |
|
| 272 | + // Try to get the errors form the session |
|
| 273 | + if ($this->app['session']->has('errors')) { |
|
| 274 | + $this->errors = $this->app['session']->get('errors'); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // If we're given a raw Validator, go fetch the errors in it |
|
| 278 | + if ($validator instanceof Validator) { |
|
| 279 | + $this->errors = $validator->getMessageBag(); |
|
| 280 | + } else { |
|
| 281 | + if ($validator instanceof MessageBag) { |
|
| 282 | + $this->errors = $validator; |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + return $this->errors; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Add live validation rules |
|
| 291 | + * |
|
| 292 | + * @param array *$rules An array of Laravel rules |
|
| 293 | + * |
|
| 294 | + * @return void |
|
| 295 | + */ |
|
| 296 | + public function withRules() |
|
| 297 | + { |
|
| 298 | + $rules = call_user_func_array('array_merge', func_get_args()); |
|
| 299 | + |
|
| 300 | + // Parse the rules according to Laravel conventions |
|
| 301 | + foreach ($rules as $name => $fieldRules) { |
|
| 302 | + $expFieldRules = $fieldRules; |
|
| 303 | + if (!is_array($expFieldRules)) { |
|
| 304 | + $expFieldRules = explode('|', $expFieldRules); |
|
| 305 | + $expFieldRules = array_map('trim', $expFieldRules); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + foreach ($expFieldRules as $rule) { |
|
| 309 | + |
|
| 310 | + $parameters = null; |
|
| 311 | + |
|
| 312 | + if (($colon = strpos($rule, ':')) !== false) { |
|
| 313 | + $rulename = substr($rule, 0, $colon); |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 317 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 318 | + */ |
|
| 319 | + if ($rulename !== 'regex') { |
|
| 320 | + $parameters = str_getcsv(substr($rule, $colon + 1)); |
|
| 321 | + } else { |
|
| 322 | + $parameters = [substr($rule, $colon + 1)]; |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + // Exclude unsupported rules |
|
| 327 | + $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule; |
|
| 328 | + |
|
| 329 | + // Store processed rule in Former's array |
|
| 330 | + if (!isset($parameters)) { |
|
| 331 | + $parameters = array(); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $this->rules[$name][$rule] = $parameters; |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Switch the framework used by Former |
|
| 341 | + * |
|
| 342 | + * @param string $framework The name of the framework to use |
|
| 343 | + */ |
|
| 344 | + public function framework($framework = null) |
|
| 345 | + { |
|
| 346 | + if (!$framework) { |
|
| 347 | + return $this->app['former.framework']->current(); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + $this->setOption('framework', $framework); |
|
| 351 | + |
|
| 352 | + $framework = $this->getFrameworkInstance($framework); |
|
| 353 | + $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 354 | + return $framework; |
|
| 355 | + }); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Get a new framework instance |
|
| 360 | + * |
|
| 361 | + * @param string $framework |
|
| 362 | + * |
|
| 363 | + * @throws Exceptions\InvalidFrameworkException |
|
| 364 | + * @return \Former\Interfaces\FrameworkInterface |
|
| 365 | + */ |
|
| 366 | + public function getFrameworkInstance($framework) |
|
| 367 | + { |
|
| 368 | + $formerClass = __NAMESPACE__.'\Framework\\'.$framework; |
|
| 369 | + |
|
| 370 | + //get interfaces of the given framework |
|
| 371 | + $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
|
| 372 | + |
|
| 373 | + if(class_exists($formerClass)) { |
|
| 374 | + $returnClass = $formerClass; |
|
| 375 | + } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 376 | + // We have some outside class, lets return it. |
|
| 377 | + $returnClass = $framework; |
|
| 378 | + } else { |
|
| 379 | + throw (new InvalidFrameworkException())->setFramework($framework); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + return new $returnClass($this->app); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Get an option from the config |
|
| 387 | + * |
|
| 388 | + * @param string $option The option |
|
| 389 | + * @param mixed $default Optional fallback |
|
| 390 | + * |
|
| 391 | + * @return mixed |
|
| 392 | + */ |
|
| 393 | + public function getOption($option, $default = null) |
|
| 394 | + { |
|
| 395 | + return $this->app['config']->get('former.'.$option, $default); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Set an option on the config |
|
| 400 | + * |
|
| 401 | + * @param string $option |
|
| 402 | + * @param string $value |
|
| 403 | + */ |
|
| 404 | + public function setOption($option, $value) |
|
| 405 | + { |
|
| 406 | + return $this->app['config']->set('former.'.$option, $value); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + //////////////////////////////////////////////////////////////////// |
|
| 410 | + ////////////////////////////// BUILDERS //////////////////////////// |
|
| 411 | + //////////////////////////////////////////////////////////////////// |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Closes a form |
|
| 415 | + * |
|
| 416 | + * @return string A form closing tag |
|
| 417 | + */ |
|
| 418 | + public function close() |
|
| 419 | + { |
|
| 420 | + if ($this->app->bound('former.form')) { |
|
| 421 | + $closing = $this->app['former.form']->close(); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + // Destroy instances |
|
| 425 | + $instances = array('former.form', 'former.form.framework'); |
|
| 426 | + foreach ($instances as $instance) { |
|
| 427 | + $this->app[$instance] = null; |
|
| 428 | + unset($this->app[$instance]); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + // Reset populator |
|
| 432 | + $this->app['former.populator']->reset(); |
|
| 433 | + |
|
| 434 | + // Reset all values |
|
| 435 | + $this->errors = null; |
|
| 436 | + $this->rules = array(); |
|
| 437 | + |
|
| 438 | + return isset($closing) ? $closing : null; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + //////////////////////////////////////////////////////////////////// |
|
| 442 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 443 | + //////////////////////////////////////////////////////////////////// |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Get the errors for the current field |
|
| 447 | + * |
|
| 448 | + * @param string $name A field name |
|
| 449 | + * |
|
| 450 | + * @return string An error message |
|
| 451 | + */ |
|
| 452 | + public function getErrors($name = null) |
|
| 453 | + { |
|
| 454 | + // Get name and translate array notation |
|
| 455 | + if (!$name and $this->app['former.field']) { |
|
| 456 | + $name = $this->app['former.field']->getName(); |
|
| 457 | + |
|
| 458 | + // Always return empty string for anonymous fields (i.e. fields with no name/id) |
|
| 459 | + if (!$name) { |
|
| 460 | + return ''; |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + if ($this->errors and $name) { |
|
| 465 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 466 | + $name = trim($name, '.'); |
|
| 467 | + |
|
| 468 | + return $this->errors->first($name); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + return $this->errors; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Get a rule from the Rules array |
|
| 476 | + * |
|
| 477 | + * @param string $name The field to fetch |
|
| 478 | + * |
|
| 479 | + * @return array An array of rules |
|
| 480 | + */ |
|
| 481 | + public function getRules($name) |
|
| 482 | + { |
|
| 483 | + // Check the rules for the name as given |
|
| 484 | + $ruleset = array_get($this->rules, $name); |
|
| 485 | + |
|
| 486 | + // If no rules found, convert to dot notation and try again |
|
| 487 | + if (is_null($ruleset)) { |
|
| 488 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 489 | + $name = trim($name, '.'); |
|
| 490 | + $ruleset = array_get($this->rules, $name); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + return $ruleset; |
|
| 494 | + } |
|
| 495 | 495 | } |
@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | // Checking for any supplementary classes |
| 149 | 149 | $modifiers = explode('_', $method); |
| 150 | - $method = array_pop($modifiers); |
|
| 150 | + $method = array_pop($modifiers); |
|
| 151 | 151 | |
| 152 | 152 | // Dispatch to the different Form\Fields |
| 153 | - $field = $this->dispatch->toFields($method, $parameters); |
|
| 153 | + $field = $this->dispatch->toFields($method, $parameters); |
|
| 154 | 154 | $field->setModifiers($modifiers); |
| 155 | 155 | $field->addClass(''); |
| 156 | 156 | |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | $this->setOption('framework', $framework); |
| 351 | 351 | |
| 352 | 352 | $framework = $this->getFrameworkInstance($framework); |
| 353 | - $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 353 | + $this->app->bind('former.framework', function($app) use ($framework) { |
|
| 354 | 354 | return $framework; |
| 355 | 355 | }); |
| 356 | 356 | } |
@@ -370,9 +370,9 @@ discard block |
||
| 370 | 370 | //get interfaces of the given framework |
| 371 | 371 | $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
| 372 | 372 | |
| 373 | - if(class_exists($formerClass)) { |
|
| 373 | + if (class_exists($formerClass)) { |
|
| 374 | 374 | $returnClass = $formerClass; |
| 375 | - } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 375 | + } elseif (class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 376 | 376 | // We have some outside class, lets return it. |
| 377 | 377 | $returnClass = $framework; |
| 378 | 378 | } else { |
@@ -12,560 +12,560 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class Checkable extends Field |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Renders the checkables as inline |
|
| 17 | - * |
|
| 18 | - * @var boolean |
|
| 19 | - */ |
|
| 20 | - protected $inline = false; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Add a text to a single element |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $text = null; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Renders the checkables as grouped |
|
| 31 | - * |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 34 | - protected $grouped = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The checkable items currently stored |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - protected $items = array(); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The type of checkable item |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $checkable = null; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * An array of checked items |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - protected $checked = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The checkable currently being focused on |
|
| 59 | - * |
|
| 60 | - * @var integer |
|
| 61 | - */ |
|
| 62 | - protected $focus = null; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Whether this particular checkable is to be pushed |
|
| 66 | - * |
|
| 67 | - * @var boolean |
|
| 68 | - */ |
|
| 69 | - protected $isPushed = null; |
|
| 70 | - |
|
| 71 | - //////////////////////////////////////////////////////////////////// |
|
| 72 | - //////////////////////////// CORE METHODS ////////////////////////// |
|
| 73 | - //////////////////////////////////////////////////////////////////// |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Build a new checkable |
|
| 77 | - * |
|
| 78 | - * @param Container $app |
|
| 79 | - * @param string $type |
|
| 80 | - * @param array $name |
|
| 81 | - * @param $label |
|
| 82 | - * @param $value |
|
| 83 | - * @param $attributes |
|
| 84 | - */ |
|
| 85 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 86 | - { |
|
| 87 | - // Unify auto and chained methods of grouping checkboxes |
|
| 88 | - if (ends_with($name, '[]')) { |
|
| 89 | - $name = substr($name, 0, -2); |
|
| 90 | - $this->grouped(); |
|
| 91 | - } |
|
| 92 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 93 | - |
|
| 94 | - if (is_array($this->value)) { |
|
| 95 | - $this->items($this->value); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Apply methods to focused checkable |
|
| 101 | - * |
|
| 102 | - * @param string $method |
|
| 103 | - * @param array $parameters |
|
| 104 | - * |
|
| 105 | - * @return $this |
|
| 106 | - */ |
|
| 107 | - public function __call($method, $parameters) |
|
| 108 | - { |
|
| 109 | - $focused = $this->setOnFocused('attributes.'.$method, array_get($parameters, 0)); |
|
| 110 | - if ($focused) { |
|
| 111 | - return $this; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return parent::__call($method, $parameters); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Prints out the currently stored checkables |
|
| 119 | - */ |
|
| 120 | - public function render() |
|
| 121 | - { |
|
| 122 | - $html = null; |
|
| 123 | - |
|
| 124 | - $this->setFieldClasses(); |
|
| 125 | - |
|
| 126 | - // Multiple items |
|
| 127 | - if ($this->items) { |
|
| 128 | - unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 129 | - foreach ($this->items as $key => $item) { |
|
| 130 | - $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 131 | - $html .= $this->createCheckable($item, $value); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $html; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // Single item |
|
| 138 | - return $this->createCheckable(array( |
|
| 139 | - 'name' => $this->name, |
|
| 140 | - 'label' => $this->text, |
|
| 141 | - 'value' => $this->value, |
|
| 142 | - 'attributes' => $this->attributes, |
|
| 143 | - )); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - //////////////////////////////////////////////////////////////////// |
|
| 147 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 148 | - //////////////////////////////////////////////////////////////////// |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Focus on a particular checkable |
|
| 152 | - * |
|
| 153 | - * @param integer $on The checkable to focus on |
|
| 154 | - * |
|
| 155 | - * @return $this |
|
| 156 | - */ |
|
| 157 | - public function on($on) |
|
| 158 | - { |
|
| 159 | - if (!isset($this->items[$on])) { |
|
| 160 | - return $this; |
|
| 161 | - } else { |
|
| 162 | - $this->focus = $on; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return $this; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Set the checkables as inline |
|
| 170 | - */ |
|
| 171 | - public function inline($isInline = true) |
|
| 172 | - { |
|
| 173 | - $this->inline = $isInline; |
|
| 174 | - |
|
| 175 | - return $this; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Set the checkables as stacked |
|
| 180 | - */ |
|
| 181 | - public function stacked($isStacked = true) |
|
| 182 | - { |
|
| 183 | - $this->inline = !$isStacked; |
|
| 184 | - |
|
| 185 | - return $this; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Set the checkables as grouped |
|
| 190 | - */ |
|
| 191 | - public function grouped($isGrouped = true) |
|
| 192 | - { |
|
| 193 | - $this->grouped = $isGrouped; |
|
| 194 | - |
|
| 195 | - return $this; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Add text to a single checkable |
|
| 200 | - * |
|
| 201 | - * @param string $text The checkable label |
|
| 202 | - * |
|
| 203 | - * @return $this |
|
| 204 | - */ |
|
| 205 | - public function text($text) |
|
| 206 | - { |
|
| 207 | - // Translate and format |
|
| 208 | - $text = Helpers::translate($text); |
|
| 209 | - |
|
| 210 | - // Apply on focused if any |
|
| 211 | - $focused = $this->setOnFocused('label', $text); |
|
| 212 | - if ($focused) { |
|
| 213 | - return $this; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $this->text = $text; |
|
| 217 | - |
|
| 218 | - return $this; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Push this particular checkbox |
|
| 223 | - * |
|
| 224 | - * @param boolean $pushed |
|
| 225 | - * |
|
| 226 | - * @return $this |
|
| 227 | - */ |
|
| 228 | - public function push($pushed = true) |
|
| 229 | - { |
|
| 230 | - $this->isPushed = $pushed; |
|
| 231 | - |
|
| 232 | - return $this; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Check a specific item |
|
| 237 | - * |
|
| 238 | - * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 239 | - * |
|
| 240 | - * @return $this |
|
| 241 | - */ |
|
| 242 | - public function check($checked = true) |
|
| 243 | - { |
|
| 244 | - // If we're setting all the checked items at once |
|
| 245 | - if (is_array($checked)) { |
|
| 246 | - $this->checked = $checked; |
|
| 247 | - // Checking an item in particular |
|
| 248 | - } elseif (is_string($checked) or is_int($checked)) { |
|
| 249 | - $this->checked[$checked] = true; |
|
| 250 | - // Only setting a single item |
|
| 251 | - } else { |
|
| 252 | - $this->checked[$this->name] = (bool) $checked; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return $this; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - //////////////////////////////////////////////////////////////////// |
|
| 259 | - ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 260 | - //////////////////////////////////////////////////////////////////// |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Creates a series of checkable items |
|
| 264 | - * |
|
| 265 | - * @param array $_items Items to create |
|
| 266 | - */ |
|
| 267 | - protected function items($_items) |
|
| 268 | - { |
|
| 269 | - // If passing an array |
|
| 270 | - if (sizeof($_items) == 1 and |
|
| 271 | - isset($_items[0]) and |
|
| 272 | - is_array($_items[0]) |
|
| 273 | - ) { |
|
| 274 | - $_items = $_items[0]; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - // Fetch models if that's what we were passed |
|
| 278 | - if (isset($_items[0]) and is_object($_items[0])) { |
|
| 279 | - $_items = Helpers::queryToArray($_items); |
|
| 280 | - $_items = array_flip($_items); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - // Iterate through items, assign a name and a label to each |
|
| 284 | - $count = 0; |
|
| 285 | - foreach ($_items as $label => $name) { |
|
| 286 | - |
|
| 287 | - // Define a fallback name in case none is found |
|
| 288 | - $fallback = $this->isCheckbox() |
|
| 289 | - ? $this->name.'_'.$count |
|
| 290 | - : $this->name; |
|
| 291 | - |
|
| 292 | - // Grouped fields |
|
| 293 | - if ($this->isGrouped()) { |
|
| 294 | - $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 295 | - $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // If we haven't any name defined for the checkable, try to compute some |
|
| 299 | - if (!is_string($label) and !is_array($name)) { |
|
| 300 | - $label = $name; |
|
| 301 | - $name = $fallback; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // If we gave custom information on the item, add them |
|
| 305 | - if (is_array($name)) { |
|
| 306 | - $attributes = $name; |
|
| 307 | - $name = array_get($attributes, 'name', $fallback); |
|
| 308 | - unset($attributes['name']); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - // Store all informations we have in an array |
|
| 312 | - $item = array( |
|
| 313 | - 'name' => $name, |
|
| 314 | - 'label' => Helpers::translate($label), |
|
| 315 | - 'count' => $count, |
|
| 316 | - ); |
|
| 317 | - if (isset($attributes)) { |
|
| 318 | - $item['attributes'] = $attributes; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $this->items[] = $item; |
|
| 322 | - $count++; |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Renders a checkable |
|
| 328 | - * |
|
| 329 | - * @param string|array $item A checkable item |
|
| 330 | - * @param integer $fallbackValue A fallback value if none is set |
|
| 331 | - * |
|
| 332 | - * @return string |
|
| 333 | - */ |
|
| 334 | - protected function createCheckable($item, $fallbackValue = 1) |
|
| 335 | - { |
|
| 336 | - // Extract informations |
|
| 337 | - extract($item); |
|
| 338 | - |
|
| 339 | - // Set default values |
|
| 340 | - if (!isset($attributes)) { |
|
| 341 | - $attributes = array(); |
|
| 342 | - } |
|
| 343 | - if (isset($attributes['value'])) { |
|
| 344 | - $value = $attributes['value']; |
|
| 345 | - } |
|
| 346 | - if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 347 | - $value = $fallbackValue; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - // If inline items, add class |
|
| 351 | - $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 352 | - |
|
| 353 | - // In Bootsrap 3 or 4, don't append the the checkable type (radio/checkbox) as a class if |
|
| 354 | - // rendering inline. |
|
| 355 | - $class = ($this->app['former']->framework() == 'TwitterBootstrap3' || |
|
| 356 | - $this->app['former']->framework() == 'TwitterBootstrap4') ? trim($isInline) : $this->checkable.$isInline; |
|
| 357 | - |
|
| 358 | - // Merge custom attributes with global attributes |
|
| 359 | - $attributes = array_merge($this->attributes, $attributes); |
|
| 360 | - if (!isset($attributes['id'])) { |
|
| 361 | - $attributes['id'] = $name.$this->unique($name); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - // Create field |
|
| 365 | - $field = Input::create($this->checkable, $name, $value, $attributes); |
|
| 366 | - if ($this->isChecked($item, $value)) { |
|
| 367 | - $field->checked('checked'); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - // Add hidden checkbox if requested |
|
| 371 | - if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 372 | - if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 373 | - $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 374 | - |
|
| 375 | - // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 376 | - // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 377 | - $this->app->instance('former.field', $this); |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // If no label to wrap, return plain checkable |
|
| 382 | - if (!$label) { |
|
| 383 | - $element = (is_object($field)) ? $field->render() : $field; |
|
| 384 | - } elseif ($this->app['former']->framework() == 'TwitterBootstrap4') { |
|
| 385 | - // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
|
| 386 | - $element = $field . Element::create('label', $label)->for($attributes['id'])->class($class)->render(); |
|
| 387 | - |
|
| 388 | - $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
|
| 15 | + /** |
|
| 16 | + * Renders the checkables as inline |
|
| 17 | + * |
|
| 18 | + * @var boolean |
|
| 19 | + */ |
|
| 20 | + protected $inline = false; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Add a text to a single element |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $text = null; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Renders the checkables as grouped |
|
| 31 | + * |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | + protected $grouped = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The checkable items currently stored |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + protected $items = array(); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The type of checkable item |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $checkable = null; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * An array of checked items |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + protected $checked = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The checkable currently being focused on |
|
| 59 | + * |
|
| 60 | + * @var integer |
|
| 61 | + */ |
|
| 62 | + protected $focus = null; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Whether this particular checkable is to be pushed |
|
| 66 | + * |
|
| 67 | + * @var boolean |
|
| 68 | + */ |
|
| 69 | + protected $isPushed = null; |
|
| 70 | + |
|
| 71 | + //////////////////////////////////////////////////////////////////// |
|
| 72 | + //////////////////////////// CORE METHODS ////////////////////////// |
|
| 73 | + //////////////////////////////////////////////////////////////////// |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Build a new checkable |
|
| 77 | + * |
|
| 78 | + * @param Container $app |
|
| 79 | + * @param string $type |
|
| 80 | + * @param array $name |
|
| 81 | + * @param $label |
|
| 82 | + * @param $value |
|
| 83 | + * @param $attributes |
|
| 84 | + */ |
|
| 85 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 86 | + { |
|
| 87 | + // Unify auto and chained methods of grouping checkboxes |
|
| 88 | + if (ends_with($name, '[]')) { |
|
| 89 | + $name = substr($name, 0, -2); |
|
| 90 | + $this->grouped(); |
|
| 91 | + } |
|
| 92 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 93 | + |
|
| 94 | + if (is_array($this->value)) { |
|
| 95 | + $this->items($this->value); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Apply methods to focused checkable |
|
| 101 | + * |
|
| 102 | + * @param string $method |
|
| 103 | + * @param array $parameters |
|
| 104 | + * |
|
| 105 | + * @return $this |
|
| 106 | + */ |
|
| 107 | + public function __call($method, $parameters) |
|
| 108 | + { |
|
| 109 | + $focused = $this->setOnFocused('attributes.'.$method, array_get($parameters, 0)); |
|
| 110 | + if ($focused) { |
|
| 111 | + return $this; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return parent::__call($method, $parameters); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Prints out the currently stored checkables |
|
| 119 | + */ |
|
| 120 | + public function render() |
|
| 121 | + { |
|
| 122 | + $html = null; |
|
| 123 | + |
|
| 124 | + $this->setFieldClasses(); |
|
| 125 | + |
|
| 126 | + // Multiple items |
|
| 127 | + if ($this->items) { |
|
| 128 | + unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 129 | + foreach ($this->items as $key => $item) { |
|
| 130 | + $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 131 | + $html .= $this->createCheckable($item, $value); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $html; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // Single item |
|
| 138 | + return $this->createCheckable(array( |
|
| 139 | + 'name' => $this->name, |
|
| 140 | + 'label' => $this->text, |
|
| 141 | + 'value' => $this->value, |
|
| 142 | + 'attributes' => $this->attributes, |
|
| 143 | + )); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + //////////////////////////////////////////////////////////////////// |
|
| 147 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 148 | + //////////////////////////////////////////////////////////////////// |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Focus on a particular checkable |
|
| 152 | + * |
|
| 153 | + * @param integer $on The checkable to focus on |
|
| 154 | + * |
|
| 155 | + * @return $this |
|
| 156 | + */ |
|
| 157 | + public function on($on) |
|
| 158 | + { |
|
| 159 | + if (!isset($this->items[$on])) { |
|
| 160 | + return $this; |
|
| 161 | + } else { |
|
| 162 | + $this->focus = $on; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return $this; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Set the checkables as inline |
|
| 170 | + */ |
|
| 171 | + public function inline($isInline = true) |
|
| 172 | + { |
|
| 173 | + $this->inline = $isInline; |
|
| 174 | + |
|
| 175 | + return $this; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Set the checkables as stacked |
|
| 180 | + */ |
|
| 181 | + public function stacked($isStacked = true) |
|
| 182 | + { |
|
| 183 | + $this->inline = !$isStacked; |
|
| 184 | + |
|
| 185 | + return $this; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Set the checkables as grouped |
|
| 190 | + */ |
|
| 191 | + public function grouped($isGrouped = true) |
|
| 192 | + { |
|
| 193 | + $this->grouped = $isGrouped; |
|
| 194 | + |
|
| 195 | + return $this; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Add text to a single checkable |
|
| 200 | + * |
|
| 201 | + * @param string $text The checkable label |
|
| 202 | + * |
|
| 203 | + * @return $this |
|
| 204 | + */ |
|
| 205 | + public function text($text) |
|
| 206 | + { |
|
| 207 | + // Translate and format |
|
| 208 | + $text = Helpers::translate($text); |
|
| 209 | + |
|
| 210 | + // Apply on focused if any |
|
| 211 | + $focused = $this->setOnFocused('label', $text); |
|
| 212 | + if ($focused) { |
|
| 213 | + return $this; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $this->text = $text; |
|
| 217 | + |
|
| 218 | + return $this; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Push this particular checkbox |
|
| 223 | + * |
|
| 224 | + * @param boolean $pushed |
|
| 225 | + * |
|
| 226 | + * @return $this |
|
| 227 | + */ |
|
| 228 | + public function push($pushed = true) |
|
| 229 | + { |
|
| 230 | + $this->isPushed = $pushed; |
|
| 231 | + |
|
| 232 | + return $this; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Check a specific item |
|
| 237 | + * |
|
| 238 | + * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 239 | + * |
|
| 240 | + * @return $this |
|
| 241 | + */ |
|
| 242 | + public function check($checked = true) |
|
| 243 | + { |
|
| 244 | + // If we're setting all the checked items at once |
|
| 245 | + if (is_array($checked)) { |
|
| 246 | + $this->checked = $checked; |
|
| 247 | + // Checking an item in particular |
|
| 248 | + } elseif (is_string($checked) or is_int($checked)) { |
|
| 249 | + $this->checked[$checked] = true; |
|
| 250 | + // Only setting a single item |
|
| 251 | + } else { |
|
| 252 | + $this->checked[$this->name] = (bool) $checked; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return $this; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + //////////////////////////////////////////////////////////////////// |
|
| 259 | + ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 260 | + //////////////////////////////////////////////////////////////////// |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Creates a series of checkable items |
|
| 264 | + * |
|
| 265 | + * @param array $_items Items to create |
|
| 266 | + */ |
|
| 267 | + protected function items($_items) |
|
| 268 | + { |
|
| 269 | + // If passing an array |
|
| 270 | + if (sizeof($_items) == 1 and |
|
| 271 | + isset($_items[0]) and |
|
| 272 | + is_array($_items[0]) |
|
| 273 | + ) { |
|
| 274 | + $_items = $_items[0]; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // Fetch models if that's what we were passed |
|
| 278 | + if (isset($_items[0]) and is_object($_items[0])) { |
|
| 279 | + $_items = Helpers::queryToArray($_items); |
|
| 280 | + $_items = array_flip($_items); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + // Iterate through items, assign a name and a label to each |
|
| 284 | + $count = 0; |
|
| 285 | + foreach ($_items as $label => $name) { |
|
| 286 | + |
|
| 287 | + // Define a fallback name in case none is found |
|
| 288 | + $fallback = $this->isCheckbox() |
|
| 289 | + ? $this->name.'_'.$count |
|
| 290 | + : $this->name; |
|
| 291 | + |
|
| 292 | + // Grouped fields |
|
| 293 | + if ($this->isGrouped()) { |
|
| 294 | + $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 295 | + $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // If we haven't any name defined for the checkable, try to compute some |
|
| 299 | + if (!is_string($label) and !is_array($name)) { |
|
| 300 | + $label = $name; |
|
| 301 | + $name = $fallback; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // If we gave custom information on the item, add them |
|
| 305 | + if (is_array($name)) { |
|
| 306 | + $attributes = $name; |
|
| 307 | + $name = array_get($attributes, 'name', $fallback); |
|
| 308 | + unset($attributes['name']); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + // Store all informations we have in an array |
|
| 312 | + $item = array( |
|
| 313 | + 'name' => $name, |
|
| 314 | + 'label' => Helpers::translate($label), |
|
| 315 | + 'count' => $count, |
|
| 316 | + ); |
|
| 317 | + if (isset($attributes)) { |
|
| 318 | + $item['attributes'] = $attributes; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $this->items[] = $item; |
|
| 322 | + $count++; |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Renders a checkable |
|
| 328 | + * |
|
| 329 | + * @param string|array $item A checkable item |
|
| 330 | + * @param integer $fallbackValue A fallback value if none is set |
|
| 331 | + * |
|
| 332 | + * @return string |
|
| 333 | + */ |
|
| 334 | + protected function createCheckable($item, $fallbackValue = 1) |
|
| 335 | + { |
|
| 336 | + // Extract informations |
|
| 337 | + extract($item); |
|
| 338 | + |
|
| 339 | + // Set default values |
|
| 340 | + if (!isset($attributes)) { |
|
| 341 | + $attributes = array(); |
|
| 342 | + } |
|
| 343 | + if (isset($attributes['value'])) { |
|
| 344 | + $value = $attributes['value']; |
|
| 345 | + } |
|
| 346 | + if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 347 | + $value = $fallbackValue; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + // If inline items, add class |
|
| 351 | + $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 352 | + |
|
| 353 | + // In Bootsrap 3 or 4, don't append the the checkable type (radio/checkbox) as a class if |
|
| 354 | + // rendering inline. |
|
| 355 | + $class = ($this->app['former']->framework() == 'TwitterBootstrap3' || |
|
| 356 | + $this->app['former']->framework() == 'TwitterBootstrap4') ? trim($isInline) : $this->checkable.$isInline; |
|
| 357 | + |
|
| 358 | + // Merge custom attributes with global attributes |
|
| 359 | + $attributes = array_merge($this->attributes, $attributes); |
|
| 360 | + if (!isset($attributes['id'])) { |
|
| 361 | + $attributes['id'] = $name.$this->unique($name); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + // Create field |
|
| 365 | + $field = Input::create($this->checkable, $name, $value, $attributes); |
|
| 366 | + if ($this->isChecked($item, $value)) { |
|
| 367 | + $field->checked('checked'); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + // Add hidden checkbox if requested |
|
| 371 | + if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 372 | + if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 373 | + $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 374 | + |
|
| 375 | + // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 376 | + // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 377 | + $this->app->instance('former.field', $this); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // If no label to wrap, return plain checkable |
|
| 382 | + if (!$label) { |
|
| 383 | + $element = (is_object($field)) ? $field->render() : $field; |
|
| 384 | + } elseif ($this->app['former']->framework() == 'TwitterBootstrap4') { |
|
| 385 | + // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
|
| 386 | + $element = $field . Element::create('label', $label)->for($attributes['id'])->class($class)->render(); |
|
| 387 | + |
|
| 388 | + $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
|
| 389 | 389 | |
| 390 | - $element = Element::create('div', $element)->class($wrapper_class)->render(); |
|
| 391 | - |
|
| 392 | - } else { |
|
| 393 | - // Original way is to add the 'input' inside the 'label' |
|
| 394 | - $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 398 | - if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 399 | - $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 400 | - if ($this->getAttribute('disabled')) { |
|
| 401 | - $wrapper->addClass('disabled'); |
|
| 402 | - } |
|
| 403 | - $element = $wrapper->render(); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - // Return the field |
|
| 407 | - return $element; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - //////////////////////////////////////////////////////////////////// |
|
| 411 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 412 | - //////////////////////////////////////////////////////////////////// |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Generate an unique ID for a field |
|
| 416 | - * |
|
| 417 | - * @param string $name The field's name |
|
| 418 | - * |
|
| 419 | - * @return string A field number to use |
|
| 420 | - */ |
|
| 421 | - protected function unique($name) |
|
| 422 | - { |
|
| 423 | - $this->app['former']->labels[] = $name; |
|
| 424 | - |
|
| 425 | - // Count number of fields with the same ID |
|
| 426 | - $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 427 | - return $label == $name; |
|
| 428 | - }); |
|
| 429 | - $unique = sizeof($where); |
|
| 430 | - |
|
| 431 | - // In case the field doesn't need to be numbered |
|
| 432 | - if ($unique < 2 or empty($this->items)) { |
|
| 433 | - return false; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - return $unique; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Set something on the currently focused checkable |
|
| 441 | - * |
|
| 442 | - * @param string $attribute The key to set |
|
| 443 | - * @param string $value Its value |
|
| 444 | - * |
|
| 445 | - * @return $this|bool |
|
| 446 | - */ |
|
| 447 | - protected function setOnFocused($attribute, $value) |
|
| 448 | - { |
|
| 449 | - if (is_null($this->focus)) { |
|
| 450 | - return false; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - $this->items[$this->focus] = array_set($this->items[$this->focus], $attribute, $value); |
|
| 454 | - |
|
| 455 | - return $this; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Check if a checkable is checked |
|
| 460 | - * |
|
| 461 | - * @return boolean Checked or not |
|
| 462 | - */ |
|
| 463 | - protected function isChecked($item = null, $value = null) |
|
| 464 | - { |
|
| 465 | - if (isset($item['name'])) { |
|
| 466 | - $name = $item['name']; |
|
| 467 | - } |
|
| 468 | - if (empty($name)) { |
|
| 469 | - $name = $this->name; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 473 | - // Or if it's a single radio, simply see if we called check |
|
| 474 | - if ($this->isCheckbox() or |
|
| 475 | - !$this->isCheckbox() and !$this->items |
|
| 476 | - ) { |
|
| 477 | - $checked = array_get($this->checked, $name, false); |
|
| 478 | - |
|
| 479 | - // If there are multiple, search for the value |
|
| 480 | - // as the name are the same between radios |
|
| 481 | - } else { |
|
| 482 | - $checked = array_get($this->checked, $value, false); |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - // Check the values and POST array |
|
| 486 | - if ($this->isGrouped()) { |
|
| 487 | - // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 488 | - $groupIndex = self::getGroupIndexFromItem($item); |
|
| 489 | - |
|
| 490 | - // Search using the bare name, not the individual item name |
|
| 491 | - $post = $this->app['former']->getPost($this->name); |
|
| 492 | - $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 493 | - |
|
| 494 | - if (isset($post[$groupIndex])) { |
|
| 495 | - $post = $post[$groupIndex]; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 500 | - * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 501 | - * |
|
| 502 | - * array( |
|
| 503 | - * 'name' = 'roles[foo]', |
|
| 504 | - * 'value' => 'foo', |
|
| 505 | - * ) |
|
| 506 | - */ |
|
| 507 | - if ($static instanceof Collection) { |
|
| 508 | - // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 509 | - foreach ($static as $staticItem) { |
|
| 510 | - $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 511 | - if ($staticItemValue == $groupIndex) { |
|
| 512 | - $static = $staticItemValue; |
|
| 513 | - break; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - } else if (isset($static[$groupIndex])) { |
|
| 517 | - $static = $static[$groupIndex]; |
|
| 518 | - } |
|
| 519 | - } else { |
|
| 520 | - $post = $this->app['former']->getPost($name); |
|
| 521 | - $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 525 | - $isChecked = ($post == $value); |
|
| 526 | - } elseif (!is_null($static)) { |
|
| 527 | - $isChecked = ($static == $value); |
|
| 528 | - } else { |
|
| 529 | - $isChecked = $checked; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - return $isChecked ? true : false; |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * Check if the current element is a checkbox |
|
| 537 | - * |
|
| 538 | - * @return boolean Checkbox or radio |
|
| 539 | - */ |
|
| 540 | - protected function isCheckbox() |
|
| 541 | - { |
|
| 542 | - return $this->checkable == 'checkbox'; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Check if the checkables are grouped or not |
|
| 547 | - * |
|
| 548 | - * @return boolean |
|
| 549 | - */ |
|
| 550 | - protected function isGrouped() |
|
| 551 | - { |
|
| 552 | - return |
|
| 553 | - $this->grouped == true or |
|
| 554 | - strpos($this->name, '[]') !== false; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * @param array $item The item array, containing at least name and count keys. |
|
| 559 | - * |
|
| 560 | - * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 561 | - */ |
|
| 562 | - public static function getGroupIndexFromItem($item) |
|
| 563 | - { |
|
| 564 | - $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 565 | - if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 566 | - return $item['count']; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - return $groupIndex; |
|
| 570 | - } |
|
| 390 | + $element = Element::create('div', $element)->class($wrapper_class)->render(); |
|
| 391 | + |
|
| 392 | + } else { |
|
| 393 | + // Original way is to add the 'input' inside the 'label' |
|
| 394 | + $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 398 | + if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 399 | + $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 400 | + if ($this->getAttribute('disabled')) { |
|
| 401 | + $wrapper->addClass('disabled'); |
|
| 402 | + } |
|
| 403 | + $element = $wrapper->render(); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + // Return the field |
|
| 407 | + return $element; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + //////////////////////////////////////////////////////////////////// |
|
| 411 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 412 | + //////////////////////////////////////////////////////////////////// |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Generate an unique ID for a field |
|
| 416 | + * |
|
| 417 | + * @param string $name The field's name |
|
| 418 | + * |
|
| 419 | + * @return string A field number to use |
|
| 420 | + */ |
|
| 421 | + protected function unique($name) |
|
| 422 | + { |
|
| 423 | + $this->app['former']->labels[] = $name; |
|
| 424 | + |
|
| 425 | + // Count number of fields with the same ID |
|
| 426 | + $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 427 | + return $label == $name; |
|
| 428 | + }); |
|
| 429 | + $unique = sizeof($where); |
|
| 430 | + |
|
| 431 | + // In case the field doesn't need to be numbered |
|
| 432 | + if ($unique < 2 or empty($this->items)) { |
|
| 433 | + return false; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + return $unique; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Set something on the currently focused checkable |
|
| 441 | + * |
|
| 442 | + * @param string $attribute The key to set |
|
| 443 | + * @param string $value Its value |
|
| 444 | + * |
|
| 445 | + * @return $this|bool |
|
| 446 | + */ |
|
| 447 | + protected function setOnFocused($attribute, $value) |
|
| 448 | + { |
|
| 449 | + if (is_null($this->focus)) { |
|
| 450 | + return false; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + $this->items[$this->focus] = array_set($this->items[$this->focus], $attribute, $value); |
|
| 454 | + |
|
| 455 | + return $this; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Check if a checkable is checked |
|
| 460 | + * |
|
| 461 | + * @return boolean Checked or not |
|
| 462 | + */ |
|
| 463 | + protected function isChecked($item = null, $value = null) |
|
| 464 | + { |
|
| 465 | + if (isset($item['name'])) { |
|
| 466 | + $name = $item['name']; |
|
| 467 | + } |
|
| 468 | + if (empty($name)) { |
|
| 469 | + $name = $this->name; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 473 | + // Or if it's a single radio, simply see if we called check |
|
| 474 | + if ($this->isCheckbox() or |
|
| 475 | + !$this->isCheckbox() and !$this->items |
|
| 476 | + ) { |
|
| 477 | + $checked = array_get($this->checked, $name, false); |
|
| 478 | + |
|
| 479 | + // If there are multiple, search for the value |
|
| 480 | + // as the name are the same between radios |
|
| 481 | + } else { |
|
| 482 | + $checked = array_get($this->checked, $value, false); |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + // Check the values and POST array |
|
| 486 | + if ($this->isGrouped()) { |
|
| 487 | + // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 488 | + $groupIndex = self::getGroupIndexFromItem($item); |
|
| 489 | + |
|
| 490 | + // Search using the bare name, not the individual item name |
|
| 491 | + $post = $this->app['former']->getPost($this->name); |
|
| 492 | + $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 493 | + |
|
| 494 | + if (isset($post[$groupIndex])) { |
|
| 495 | + $post = $post[$groupIndex]; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 500 | + * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 501 | + * |
|
| 502 | + * array( |
|
| 503 | + * 'name' = 'roles[foo]', |
|
| 504 | + * 'value' => 'foo', |
|
| 505 | + * ) |
|
| 506 | + */ |
|
| 507 | + if ($static instanceof Collection) { |
|
| 508 | + // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 509 | + foreach ($static as $staticItem) { |
|
| 510 | + $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 511 | + if ($staticItemValue == $groupIndex) { |
|
| 512 | + $static = $staticItemValue; |
|
| 513 | + break; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + } else if (isset($static[$groupIndex])) { |
|
| 517 | + $static = $static[$groupIndex]; |
|
| 518 | + } |
|
| 519 | + } else { |
|
| 520 | + $post = $this->app['former']->getPost($name); |
|
| 521 | + $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 525 | + $isChecked = ($post == $value); |
|
| 526 | + } elseif (!is_null($static)) { |
|
| 527 | + $isChecked = ($static == $value); |
|
| 528 | + } else { |
|
| 529 | + $isChecked = $checked; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + return $isChecked ? true : false; |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * Check if the current element is a checkbox |
|
| 537 | + * |
|
| 538 | + * @return boolean Checkbox or radio |
|
| 539 | + */ |
|
| 540 | + protected function isCheckbox() |
|
| 541 | + { |
|
| 542 | + return $this->checkable == 'checkbox'; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Check if the checkables are grouped or not |
|
| 547 | + * |
|
| 548 | + * @return boolean |
|
| 549 | + */ |
|
| 550 | + protected function isGrouped() |
|
| 551 | + { |
|
| 552 | + return |
|
| 553 | + $this->grouped == true or |
|
| 554 | + strpos($this->name, '[]') !== false; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * @param array $item The item array, containing at least name and count keys. |
|
| 559 | + * |
|
| 560 | + * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 561 | + */ |
|
| 562 | + public static function getGroupIndexFromItem($item) |
|
| 563 | + { |
|
| 564 | + $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 565 | + if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 566 | + return $item['count']; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + return $groupIndex; |
|
| 570 | + } |
|
| 571 | 571 | } |