michaelhall/nice-dump

This package is abandoned and no longer maintained. The author suggests using the nicedump/nicedump package instead.

Dump a PHP variable in NiceDump format.

v1.1.0 2018-12-27 17:24 UTC

This package is auto-updated.

Last update: 2018-12-29 23:13:36 UTC


README

Build Status AppVeyor codecov.io StyleCI License Latest Stable Version Total Downloads

Dump a PHP variable according to the NiceDump format specification.

Requirements

  • PHP >= 7.1

Install with composer

$ composer require michaelhall/nice-dump

Basic usage

Create a NiceDump

<?php

require __DIR__ . '/vendor/autoload.php';

// $var can be anything.
$var = 'Foo';

// Create a plain dump.
$niceDump = \MichaelHall\NiceDump\NiceDump::create($var);

// Create a dump with a name.
$niceDump = \MichaelHall\NiceDump\NiceDump::create($var, 'var');

// Create a dump with a name and a comment.
$niceDump = \MichaelHall\NiceDump\NiceDump::create($var, 'var', 'This is my variable');

Output a NiceDump

<?php

require __DIR__ . '/vendor/autoload.php';

// $var can be anything.
$var = 'Foo';

// Create a plain dump.
$niceDump = \MichaelHall\NiceDump\NiceDump::create($var);

// Outputs the NiceDump as:
//
// =====BEGIN NICE-DUMP=====
// eyJ0eXBlIjoic3RyaW5nIiwidmFsdWUiOiJGb28iLCJzaXplIjozfQ==
// =====END NICE-DUMP=====
echo $niceDump;

// Get the output explicitly as a string.
$dumpStr = $niceDump->__toString();

// Outputs the same as above.
echo $dumpStr; 

Custom serialization

The NiceDump serialization can be customized for a class by implementing the NiceDumpSerializable interface and the niceDumpSerialize() method:

<?php

require __DIR__ . '/vendor/autoload.php';

class Foo implements \MichaelHall\NiceDump\NiceDumpSerializable
{
    public function __construct(string $text)
    {
        $this->text = $text;
    }

    public function niceDumpSerialize(): array
    {
        return [
            'type'  => '_text_',
            'value' => $this->text,
        ];
    }

    private $text;
}

$foo = new Foo('Bar');
$niceDump = \MichaelHall\NiceDump\NiceDump::create($foo);

// Outputs the NiceDump as:
//
// =====BEGIN NICE-DUMP=====
// eyJ0eXBlIjoiX3RleHRfIiwidmFsdWUiOiJCYXIifQ==
// =====END NICE-DUMP=====
//
// which corresponds to:
//
// {"type":"_text_","value":"Bar"}
echo $niceDump;

License

MIT