Skip to content

Commit

Permalink
Merge pull request #2 from theiconic/feature/handle-traversable-in-ab…
Browse files Browse the repository at this point in the history
…stract

handle traversable objects in AbstractSynopsis
  • Loading branch information
wyrfel committed Apr 13, 2018
2 parents e4c3bf9 + 46817ae commit 61bd10b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
16 changes: 16 additions & 0 deletions src/AbstractSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TheIconic\Synopsis;

use Countable;
use Traversable;

/**
* Represents a synopsis of a value (or object), that is a description of the value useful
Expand Down Expand Up @@ -54,6 +55,21 @@ public function process($value, $depth)
} else if ($value instanceof Countable) {
$this->length = count($value);
}

if ($value instanceof Traversable) {
if ($depth) {
$this->children = [];
foreach ($value as $k => $v) {
$this->addChild($this->getFactory()->synopsize($v, $depth), $k);
}
$this->length = count($this->children);
} else {
$this->length = 0;
foreach ($value as $k => $v) {
$this->length++;
}
}
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/ArraySynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function process($value, $depth)
parent::process($value, $depth);

if ($depth) {
$this->children = [];
foreach ($value as $k => $v) {
$this->addChild($this->getFactory()->synopsize($v, $depth), $k);
}
Expand Down
21 changes: 0 additions & 21 deletions src/Object/IteratorSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,4 @@
*/
class IteratorSynopsis extends ObjectSynopsis
{
/**
* @see parent::process()
* @param $value
* @param $depth
*/
public function process($value, $depth)
{
parent::process($value, $depth);

if ($depth) {
foreach ($value as $k => $v) {
$this->addChild($this->getFactory()->synopsize($v, $depth), $k);
}
$this->length = count($this->children);
} else {
$this->length = 0;
foreach ($value as $k => $v) {
$this->length++;
}
}
}
}

0 comments on commit 61bd10b

Please sign in to comment.