Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Merge pull request #93 from adamwathan/aw-avoid-array-functions
Browse files Browse the repository at this point in the history
Manually split key/values in arrays to prevent breaking anything that was passing a collection
  • Loading branch information
adamwathan committed Mar 28, 2016
2 parents 057d98e + 97584cf commit 573a4d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/AdamWathan/Form/Elements/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,25 @@ public function __toString()

protected function renderAttributes()
{
list($attributes, $values) = $this->splitKeysAndValues($this->attributes);

return implode(array_map(function ($attribute, $value) {
return sprintf(' %s="%s"', $attribute, $value);
}, array_keys($this->attributes), $this->attributes));
}, $attributes, $values));
}

protected function splitKeysAndValues($array)
{
// Disgusting crap because people might have passed a collection
$keys = [];
$values = [];

foreach ($array as $key => $value) {
$keys[] = $key;
$values[] = $value;
}

return [$keys, $values];
}

public function __call($method, $params)
Expand Down
8 changes: 6 additions & 2 deletions src/AdamWathan/Form/Elements/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,25 @@ public function render()

protected function renderOptions()
{
list($values, $labels) = $this->splitKeysAndValues($this->options);

$tags = array_map(function ($value, $label) {
if (is_array($label)) {
return $this->renderOptGroup($value, $label);
}
return $this->renderOption($value, $label);
}, array_keys($this->options), $this->options);
}, $values, $labels);

return implode($tags);
}

protected function renderOptGroup($label, $options)
{
list($values, $labels) = $this->splitKeysAndValues($options);

$options = array_map(function ($value, $label) {
return $this->renderOption($value, $label);
}, array_keys($options), $options);
}, $values, $labels);

return implode([
sprintf('<optgroup label="%s">', $label),
Expand Down

0 comments on commit 573a4d6

Please sign in to comment.