bnomei/kirby-blueprints

View on GitHub
classes/Blueprints/IsArrayable.php

Summary

Maintainability
A
0 mins
Test Coverage
A
90%
<?php

namespace Bnomei\Blueprints;

trait IsArrayable
{
    public function toArray(): array
    {
        $json_encode = json_encode($this);
        if ($json_encode === false) {
            throw new \Exception('Could not encode to JSON.');
        }

        $data = json_decode($json_encode, true);
        if (! is_array($data)) {
            $data = []; // @codeCoverageIgnore
        }
        ksort($data);

        $data = Blueprint::arraySetKeysFromColumns($data);
        $data = Blueprint::arrayRemoveByValuesRecursive($data, [null, '', []]);

        return $data;
    }
}