From 1b4c26eb46601e59b3dfd81e89011817881d578e Mon Sep 17 00:00:00 2001 From: Luke Watts Date: Fri, 8 Mar 2024 13:09:51 +0000 Subject: [PATCH] Adding Dumpable trait --- README.md | 24 ++++++++++++++++++++++++ composer.json | 3 ++- src/Support/Traits/Dumpable.php | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/Support/Traits/Dumpable.php diff --git a/README.md b/README.md index a4cc5b9..17e676c 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,30 @@ $container ->unless(fn($container) => $container->has('Logger'), LoggerServiceFactory($container)); ``` +### Dumpable + +Adds `dump` and `dd` methods to any class + +```php +class Collection +{ + use Dumpable; + + public function __constructor( + protected array $collection = [] + ) {} +} + +$collection = new Collection([ + // data... +]); + +// Debug the collection... +$collection->dump(); +// Or +$collection->dd(); +``` + ## Pipeline Support class Pipelines allow for a middleware-like interface to chain processing of tasks. diff --git a/composer.json b/composer.json index 4d51c4b..f32fb86 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ }, "require": { "php": "^8.1", - "psr/http-factory": "^1.0" + "psr/http-factory": "^1.0", + "symfony/var-dumper": "^6.4" }, "require-dev": { "slim/slim": "^4.0", diff --git a/src/Support/Traits/Dumpable.php b/src/Support/Traits/Dumpable.php new file mode 100644 index 0000000..9d08def --- /dev/null +++ b/src/Support/Traits/Dumpable.php @@ -0,0 +1,32 @@ +dump(...$args); + + dd(); + } + + /** + * Dump the given arguments. + * + * @param mixed ...$args + * @return $this + */ + public function dump(...$args) + { + dump($this, ...$args); + + return $this; + } +}