giobi
8/13/2019 - 4:41 PM

EntityField.php

<?php

namespace App\Entities;

final class EntityField {
    private $attributes;

    public function __construct(array $params) {
        #defaults
        $this->setAttribute('type', 'text');    #ridondante in form.blade.php

        foreach ($params as $name => $value) {
            $this->setAttribute($name, $value);
        }
    }

    public function __set($name, $value) {
        $method = 'set'.ucfirst($name).'Attribute';
        \Debugbar::info($method);
        if (method_exists($this, $method))
            $this->$method($value);

        else $this->setAttribute($name, $value);
    }

    public function setBelongsToAttribute(Entity $entity): self {
        $this->type = 'belongsTo';
        $this->sourceUrl = $entity->getSelect2Url();
        $this->updateRule = 'filled|integer';
//        $this->index = '?';
        return $this;
    }

    private function setAttribute(string $name, $value): void {
        $this->attributes[$name] = $value;
    }

    public function __get($name) {
        $method = 'get'.ucfirst($name).'Attribute';
        if (method_exists($this, $method))
            return $this->$method();

        else return $this->getAttribute($name);
    }

    private function getAttribute(string $name) {
        return $this->attributes[$name];
    }

    public function __isset($name) {
        return array_key_exists($name, $this->attributes);
    }

    private function getLabelAttribute() {
        return $this->attributes['label'];
    }

    public function asArray(): array {
        return $this->attributes;
    }

}