diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e2f750..2f91f27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 15b749f..5ec61ac 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ $result = Collection::from([[[1]]]) ->first(true) ->first(); -vardump($result); //[1] +var_dump($result); //[1] ``` @@ -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, @@ -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 diff --git a/src/CollectionTrait.php b/src/CollectionTrait.php index 4faf40d..020d5c0 100644 --- a/src/CollectionTrait.php +++ b/src/CollectionTrait.php @@ -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 */ diff --git a/src/collection_functions.php b/src/collection_functions.php index f825651..6def47f 100644 --- a/src/collection_functions.php +++ b/src/collection_functions.php @@ -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; +} diff --git a/tests/spec/CollectionSpec.php b/tests/spec/CollectionSpec.php index 3f5ec0c..19f0503 100644 --- a/tests/spec/CollectionSpec.php +++ b/tests/spec/CollectionSpec.php @@ -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(); + } }