Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…efactoring `PartialLoop` `$values` extraction logic into its own private method
  • Loading branch information
Ocramius committed Jan 3, 2015
1 parent f3f1974 commit b512d7b
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/Helper/PartialLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,11 @@ public function __invoke($name = null, $values = null)
return $this;
}

if (!is_array($values)) {
if ($values instanceof Traversable) {
$values = ArrayUtils::iteratorToArray($values, false);
} elseif (is_object($values) && method_exists($values, 'toArray')) {
$values = $values->toArray();
} else {
throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data');
}
}

// reset the counter if it's called again
$this->partialCounter = 0;
$content = '';

foreach ($values as $item) {
foreach ($this->extractViewVariables($values) as $item) {
$this->nestObjectKey();

$this->partialCounter++;
Expand Down Expand Up @@ -143,4 +133,26 @@ private function unNestObjectKey()

return $this;
}

/**
* @param mixed $values
*
* @return array Variables to populate in the view
*/
private function extractViewVariables($values)
{
if ($values instanceof Traversable) {
return ArrayUtils::iteratorToArray($values, false);
}

if (is_array($values)) {
return $values;
}

if (is_object($values) && method_exists($values, 'toArray')) {
return $values->toArray();
}

throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data');
}
}

0 comments on commit b512d7b

Please sign in to comment.