Skip to content

Commit

Permalink
PrintDump function added
Browse files Browse the repository at this point in the history
  • Loading branch information
DusanKasan committed Jun 9, 2016
1 parent 224495e commit 397a9b0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,6 @@

##8.2.0
- Dump function added, to make debugging easier.

##8.2.0
- PrintDump function added, to make debugging easier. Prints debug output, but returns the original collection.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ $result = Collection::from([[[1]]])
->first(true)
->first();

vardump($result); //[1]
var_dump($result); //[1]
```


Expand Down Expand Up @@ -424,7 +424,7 @@ Collection::from([1, 3, 3, 2])
toArray(values(dropWhile([1, 3, 3, 2], function ($v) {return $v < 3;}))); // [3, 3, 2]
```

#### dump(int $maxItemsPerCollection = null, $maxDepth = null) : Collection
#### dump(int $maxItemsPerCollection = null, $maxDepth = null) : array
Dumps this collection into array (recursively).

- scalars are returned as they are,
Expand Down Expand Up @@ -962,6 +962,17 @@ Collection::from([1, 3, 3, 2])
->toArray(); //['a' => 1, 0 => 1, 1 => 3, 2 => 3, 3 => 2]
```

#### printDump(int $maxItemsPerCollection = null, $maxDepth = null) : Collection
Calls dump on $input and then prints it using the var_export. Returns the collection. See dump function for arguments and output documentation.
```php
Collection::from([1, 3, 3, 2])
->printDump()
->toArray(); //[1, 3, 3, 2]
```
```php
toArray(printDump([1, 3, 3, 2])); //[1, 3, 3, 2]
```

#### realize() : Collection
Realizes collection - turns lazy collection into non-lazy one by iterating over it and storing the key/values.
```php
Expand Down
12 changes: 12 additions & 0 deletions src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,18 @@ public function dump($maxItemsPerCollection = null, $maxDepth = null)
return dump($this->getItems(), $maxItemsPerCollection, $maxDepth);
}

/**
* Calls dump on this collection and then prints it using the var_export.
*
* @param null|int $maxItemsPerCollection
* @param null|int $maxDepth
* @return Collection
*/
public function printDump($maxItemsPerCollection = null, $maxDepth = null)
{
return printDump($this->getItems(), $maxItemsPerCollection, $maxDepth);
}

/**
* @return array|\Traversable
*/
Expand Down
14 changes: 14 additions & 0 deletions src/collection_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1706,3 +1706,17 @@ function dump($input, $maxItemsPerCollection = null, $maxDepth = null)

return gettype($input);
}

/**
* Calls dump on $input and then prints it using the var_export. Returns $input.
*
* @param mixed $input
* @param null|int $maxItemsPerCollection
* @param null|int $maxDepth
* @return mixed
*/
function printDump($input, $maxItemsPerCollection = null, $maxDepth = null)
{
var_export(dump($input, $maxItemsPerCollection, $maxDepth));
return $input;
}
11 changes: 11 additions & 0 deletions tests/spec/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1291,5 +1291,16 @@ function it_can_dump_the_collection()
]
);
}

function it_can_print_dump()
{
$this->beConstructedWith([1, [2], 3]);

ob_start();
$this->printDump()->shouldReturn($this);
$this->printDump(2)->shouldReturn($this);
$this->printDump(2, 2)->shouldReturn($this);
ob_clean();
}
}

0 comments on commit 397a9b0

Please sign in to comment.