HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/slenders/slenders.nl/app/Komma/Components/ComponentType/SaveState/SaveState.php
<?php


namespace App\Komma\Components\ComponentType\SaveState;

use App\Komma\Kms\Core\Attributes\Attribute;
use Illuminate\Support\Collection;

/**
 * Class SaveState
 *
 * Represents the state of attributes
 * Which settings it has and how they are configured.
 * But not which values they have.
 *
 * @package App\Komma\Components\ComponentType\SaveStateAttributes\SaveState
 */
class SaveState implements SaveStateInterface
{
    /** @var string The savestate version number. Allows us to invent newer improved versions of the savestate */
    private $version = '';

    /** @var Collection The attribute **/
    private $attributeInstances;

    public function __construct()
    {
        $this->attributeInstances = collect();
    }

    /**
     * Specify data which should be serialized to JSON
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
     * @return mixed data which can be serialized by <b>json_encode</b>,
     * which is a value of any type other than a resource.
     * @since 5.4.0
     */
    public function jsonSerialize()
    {
        return $this->toArray();
    }

    /**
     * Get the instance as an array.
     *
     * @return array
     */
    public function toArray()
    {
        return [
            'version' => $this->version,
            'attributes' => $this->attributeInstances
        ];
    }

    /**
     * String representation of object
     * @link http://php.net/manual/en/serializable.serialize.php
     * @return string the string representation of the object or null
     * @since 5.1.0
     */
    public function serialize()
    {
        $data = [];
        $data['version'] = $this->version;
        $data['attributes'] = [];

        foreach($this->attributeInstances as $attribute)
        {
            $data['attributes'][] = $attribute;
        }

        $data = serialize($data);

        return $data;
    }

    /**
     * Constructs the object
     * @link http://php.net/manual/en/serializable.unserialize.php
     * @param string $serialized <p>
     * The string representation of the object.
     * </p>
     * @return void
     * @since 5.1.0
     */
    public function unserialize($serialized)
    {
        $data = unserialize($serialized);
        $version = $data['version'];
        if(!is_string($version)) throw new \RuntimeException('Savestate encountered an illigal version value. It must be a string');
        $this->version = $version;

        $this->attributeInstances = collect();
        foreach($data['attributes'] as $attribute) {
            if(!is_a($attribute, Attribute::class)) throw new \RuntimeException('The savestate encountered an illegal attribute.');

            $this->attributeInstances->push($attribute);
        }
    }


    /**
     * @return string
     */
    public function getVersion(): string
    {
        return $this->version;
    }

    /**
     * @param string $version
     * @return SaveState
     */
    public function setVersion(string $version): SaveState
    {
        $this->version = $version;
        return $this;
    }

    /**
     * @return Collection|null
     */
    public function getAttributeInstances(): ? Collection
    {
        return $this->attributeInstances;
    }

    /**
     * @param Attribute $attributeInstance
     * @return SaveState
     */
    public function addAttributeInstance(Attribute $attributeInstance): SaveState
    {
        $this->attributeInstances->push($attributeInstance);
        return $this;
    }
}