Permalink
Browse files

Make Populator handle array notation of fields

  • Loading branch information...
1 parent 514d053 commit c0aad8a359cca64a5d5b3c65e5f3409d1d0c6963 @Anahkiasen Anahkiasen committed Mar 20, 2013
Showing with 100 additions and 27 deletions.
  1. +20 −20 composer.lock
  2. +2 −0 src/Former/Form/Fields/Hidden.php
  3. +43 −4 src/Former/Populator.php
  4. +20 −2 src/Former/Traits/Checkable.php
  5. +1 −1 src/config/config.php
  6. +14 −0 tests/PopulatorTest.php
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -27,6 +27,8 @@ class Hidden extends Field
public function __construct(Former $former, $type, $name, $value, $attributes)
{
parent::__construct($former, $type, $name, '', $value, $attributes);
+
+ $this->value = $value;
}
/**
View
@@ -65,17 +65,22 @@ public function getValue($field, $fallback = null)
// Transform the name into an array
$value = $this->values;
- $field = String::contains($field, '.') ? explode('.', $field) : (array) $field;
+ $field = $this->parseFieldAsArray($field);
// Dive into the model
foreach ($field as $relationship) {
// Multiple results relation
if (is_array($value)) {
$me = $this;
- $value = Arrays::each($value, function($submodel) use ($me, $relationship, $fallback) {
- return $me->getAttributeFromModel($submodel, $relationship, $fallback);
- });
+
+ if (array_key_exists($relationship, $value)) {
+ $value = $value[$relationship];
+ } else {
+ $value = Arrays::each($value, function($submodel) use ($me, $relationship, $fallback) {
+ return $me->getAttributeFromModel($submodel, $relationship, $fallback);
+ });
+ }
// Get attribute from model
} else {
@@ -128,6 +133,36 @@ public function reset()
////////////////////////////// HELPERS /////////////////////////////
////////////////////////////////////////////////////////////////////
+ /**
+ * Parses the name of a field to a tree of fields
+ *
+ * @param string $field The field's name
+ *
+ * @return array A tree of field
+ */
+ protected function parseFieldAsArray($field)
+ {
+ if (String::contains($field, '[]')) {
+ return (array) $field;
+ }
+
+ // Transform array notation to dot notation
+ if (String::contains($field, '[')) {
+ $field = preg_replace("/[\[\]]/", '.', $field);
+ $field = String::replace($field, '..', '.');
+ $field = trim($field, '.');
+ }
+
+ // Parse dot notation
+ if (String::contains($field, '.')) {
+ $field = explode('.', $field);
+ } else {
+ $field = (array) $field;
+ }
+
+ return $field;
+ }
+
/**
* Get an attribute from a model
*
@@ -146,6 +181,10 @@ public function getAttributeFromModel($model, $attribute, $fallback)
return $model->$attribute;
}
+ if (array_key_exists($attribute, $model)) {
+ return $model[$attribute];
+ }
+
return $fallback;
}
@@ -61,6 +61,13 @@
*/
protected $focus = null;
+ /**
+ * Whether this particular checkable is to be pushed
+ *
+ * @var boolean
+ */
+ protected $isPushed = false;
+
////////////////////////////////////////////////////////////////////
//////////////////////////// CORE METHODS //////////////////////////
////////////////////////////////////////////////////////////////////
@@ -178,6 +185,18 @@ public function text($text)
return $this;
}
+ /**
+ * Push this particular checkbox
+ *
+ * @param boolean $pushed
+ */
+ public function push($pushed = true)
+ {
+ $this->isPushed = $pushed;
+
+ return $this;
+ }
+
/**
* Check a specific item
*
@@ -293,11 +312,10 @@ protected function createCheckable($item, $fallbackValue = 1)
if ($this->isChecked($name, $value)) $field->checked('checked');
// Add hidden checkbox if requested
- if ($this->former->getOption('push_checkboxes')) {
+ if ($this->former->getOption('push_checkboxes') or $this->isPushed) {
$field = $this->former->hidden($name, $this->former->getOption('unchecked_value')) . $field;
}
-
// If no label to wrap, return plain checkable
if(!$label) return $field->render();
View
@@ -24,7 +24,7 @@
'push_checkboxes' => false,
// The value a checkbox will have in the POST array if unchecked
- 'unchecked_value' => '',
+ 'unchecked_value' => 0,
// The class to be added to required fields
'required_class' => 'required',
View
@@ -44,6 +44,20 @@ public function testCanGetAttributesAndMutators()
$this->assertEquals('foo', $populator->getValue('name'));
}
+ public function testCanGetValueThroughArrayNotation()
+ {
+ $values = (object) array(
+ 'foo' => array(
+ 'bar' => array(
+ 'bis' => 'ter',
+ ),
+ ),
+ );
+ $populator = new Populator($values);
+
+ $this->assertEquals('ter', $populator->getValue('foo[bar][bis]'));
+ }
+
public function testCanGetRelationships()
{
$model = new DummyEloquent(array('id' => 1, 'name' => 'foo'));

0 comments on commit c0aad8a

Please sign in to comment.