PHP code example of liamhackett / valueobjectcompiler

1. Go to this page and download the library: Download liamhackett/valueobjectcompiler 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/ */

    

liamhackett / valueobjectcompiler example snippets




readonly class Example
{
    /**
     * @var string[] $tags
     * @var SubObject[] $subObjects
     */
    public function __construct(
        public int $id,
        public string $name,
        public string $status,
        public array $subObjects,
        public array $tags,
    ) {
    }

    /**
     * @return self[]
     */
    public static function hydrateMany(array $bulkData): array
    {
        $result = [];

        foreach ($bulkData as $data) {
            $result[] = self::hydrate($data);
        }

        return $result;
    }

    public static function hydrate(array $data): self
    {
        if (!isset($data['id'], $data['name'], $data['status'], $data['sub_objects'], $data['tags'])) {
            throw new \RuntimeException('Missing 



readonly class SubObject
{
    public function __construct(
        public string $name,
        public ?float $value = null,
    ) {
    }

    /**
     * @return self[]
     */
    public static function hydrateMany(array $bulkData): array
    {
        $result = [];

        foreach ($bulkData as $data) {
            $result[] = self::hydrate($data);
        }

        return $result;
    }

    public static function hydrate(array $data): self
    {
        if (!isset($data['name'])) {
            throw new \RuntimeException('Missing 



readonly class Catalog
{
    /**
     * @param Book[] $book
     */
    public function __construct(
        public array $book,
    ) {
    }

    /**
     * @return self[]
     */
    public static function hydrateMany(array $bulkData): array
    {
        $result = [];

        foreach ($bulkData as $data) {
            $result[] = self::hydrate($data);
        }

        return $result;
    }

    public static function hydrate(\SimpleXMLElement $data): self
    {
        if ((string)$data->book === '') {
            throw new \RuntimeException('Missing 



readonly class Book
{
    public function __construct(
        public string $author,
        public string $description,
        public string $genre,
        public string $id,
        public float $price,
        public string $publishDate,
        public string $title,
    ) {
    }

    /**
     * @return self[]
     */
    public static function hydrateMany(array $bulkData): array
    {
        $result = [];

        foreach ($bulkData as $data) {
            $result[] = self::hydrate($data);
        }

        return $result;
    }

    public static function hydrate(\SimpleXMLElement $data): self
    {
        if (
            !isset($data['id']) ||
            (string)$data->author === '' ||
            (string)$data->description === '' ||
            (string)$data->genre === '' ||
            (string)$data->price === '' ||
            (string)$data->publish_date === '' ||
            (string)$data->title === ''
        ) {
            throw new \RuntimeException('Missing