PHP code example of darsyn / unboxer

1. Go to this page and download the library: Download darsyn/unboxer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

darsyn / unboxer example snippets


 declare(strict_types=1);

use Darsyn\Unboxer\Unboxer;
use Darsyn\Unboxer\UnboxableInterface;
use Darsyn\Unboxer\UnboxingException;

class Group implements UnboxableInterface {
    public function __construct(
        private string $name
    ) {}

    public function __unbox() {
        return $this->name;
    }
}

class Options implements \JsonSerializable {
    public function __construct(
        private bool $active,
        private bool $verified,
        private \DateTimeZone $timezone
    ) {}

    public function jsonSerialize() {
        return [
            'active' => $this->active,
            'verified' => $this->verified,
            'tz' => $this->timezone,
        ];
    }
}

class Member implements UnboxableInterface
{
    private ArrayCollection $groups;

    public function __construct(
        private int $id,
        private string $username,
        array $groups = [],
        private ?Options $options = null
    ) {
        $this->groups = new ArrayCollection($groups);
    }

    public function __unbox()
    {
        return [
            // Scalars are used as-is.
            'id' => $this->id,
            'username' => $this->username,
            // Objects of known types are returned as-is, but recursively iterated over.
            'groups' => $this->groups,
            // JSON-serializable objects are never actually run through json_encode().
            'options' => $this->options ?: [],
        ];
    }
}

$member = new Member(123, 'dr-evil', [
    new Group('admin'),
    new Group('moderator'),
    new Group('sharks-with-lasers'),
], new Options(true, false, new \DateTimeZone('America/Vancouver')));

try {
    $output = (new Unboxer)->unbox($member);
    var_dump($output);
} catch (UnboxingException $e) {
    echo $e->getMessage();
}

 declare(strict_types=1);

use Darsyn\Unboxer\Unboxer;
use Darsyn\Unboxer\UnboxableInterface;
use Darsyn\Unboxer\UnboxingException;

$data = new class implements UnboxableInterface {
    public function __unbox() {
        return new class implements UnboxableInterface {
            public function __unbox() {
                return new class implements UnboxableInterface {
                    public function __unbox() {
                        return new \RuntimeException('Error Message');
                    }
                };
            }
        };
    }
};

try {
    $output = (new Unboxer)->unbox($data);
    var_dump($output);
} catch (UnboxingException $e) {
    echo $e->getMessage();
}

 declare(strict_types=1);

use Darsyn\Unboxer\Unboxer;

class MyUnboxer extends Unboxer {
    protected function getKnownDataMethods(): iterable {
        // Don't forget to return the known data methods defined in the original, parent Unboxer.
        // The parent returns an array, but any iterable is acceptable.
        yield from parent::getKnownDataMethods();

        // Config array example.
        // Must be in the format ['methodToCall', ['optional', 'arguments', 'array']].
        yield \DateTimeInterface::class => ['format', [\DateTimeInterface::RFC3339]];

        // Closure example.
        yield \DateTimeInterface::class => function (\DateTimeInterface $date): string {
            return $date->format(\DateTimeInterface::RFC3339);
        };
    }
}

 declare(strict_types=1);

use Darsyn\Unboxer\Unboxer;

class MyUnboxer extends Unboxer {
    public const STRINGIFY_OBJECTS = false;
}