Skip to content

Instantly share code, notes, and snippets.

@fabiocarneiro
Forked from prendit/gist:8237520
Created January 28, 2014 18:09
Show Gist options
  • Save fabiocarneiro/8673004 to your computer and use it in GitHub Desktop.
Save fabiocarneiro/8673004 to your computer and use it in GitHub Desktop.
public function extract()
{
if ($this->object instanceof Traversable) {
$this->object = ArrayUtils::iteratorToArray($this->object, false);
}
if (!is_array($this->object)) {
return array();
}
$values = array();
foreach ($this->object as $key => $value) {
if ($this->hydrator) {
$values[$key] = $this->hydrator->extract($value);
} elseif ($value instanceof $this->targetElement->object) {
// @see https://github.com/zendframework/zf2/pull/2848
$targetElement = clone $this->targetElement;
$targetElement->object = $value;
$values[$key] = $targetElement->extract();
if (! $this->createNewObjects() && $this->has($key)) {
$fieldset = $this->get($key);
if ($fieldset instanceof Fieldset && $fieldset->allowObjectBinding($value)) {
$fieldset->setObject($value);
}
}
}
}
// Recursively extract and populate values for nested fieldsets
// PROBLEM: Since this is a collection, the elements in it are fieldsets, so they get extracted here
foreach ($this->fieldsets as $fieldset) {
$name = $fieldset->getName();
if (isset($values[$name])) {
$object = $values[$name];
if ($fieldset->allowObjectBinding($object)) {
$fieldset->setObject($object);
$values[$name] = $fieldset->extract();
} else {
// PROBLEM: childFieldset is in our case the nested collection, which also implements FieldsetInterface
foreach ($fieldset->fieldsets as $childFieldset) {
// FIX: Skipping collections solves the problem, but does not seem to be the right way to go
if($childFieldset instanceof \Zend\Form\Element\Collection) {
continue;
}
$childName = $childFieldset->getName();
if (isset($object[$childName])) {
$childObject = $object[$childName];
if ($childFieldset->allowObjectBinding($childObject)) {
// Fatal Error: Now what happens here is, we attach the childObject, which is a traversable object of the
// nested fieldset to one of the elements(fieldsets) of the current collection - BAM
$fieldset->setObject($childObject);
$values[$name][$childName] = $fieldset->extract();
}
}
}
}
}
}
return $values;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment