Request.php 2.9 KB
<?php


namespace Lackoxygen\TopWarehouse\Request;

use Lackoxygen\TopWarehouse\Contracts\RequestInterface;
use ReflectionClass;
use ReflectionProperty;
use Illuminate\Support\Arr;

abstract class Request implements RequestInterface
{
    /**
     * @var array
     */
    protected $propertyAlias = [];

    /**
     * @var string
     */
    protected $path = '';

    /**
     * @var string
     */
    protected $method = 'GET';

    /**
     * @var string
     */
    protected $version = '1.0';

    /**
     * @var string
     */
    protected $contentType = 'application/x-www-form-urlencoded';

    final public function __construct() { }

    protected function initialize() { }


    /**
     * @inheritDoc
     */
    public function append(string $name, $value): bool
    {
        if (!property_exists($this, $name)) {
            return false;
        }
        if (!is_array($this->{$name})) {
            return false;
        }
        $array = &$this->{$name};
        Arr::set($array, $name, $value);

        return true;
    }


    /**
     * @inheritDoc
     */
    public function getMethod(): string
    {
        return $this->method;
    }

    /**
     * @inheritDoc
     */
    public function getPath(): string
    {
        return $this->path;
    }

    /**
     * @inheritDoc
     */
    public function getContentType(): string
    {
        return $this->contentType;
    }

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

    /**
     * @inheritDoc
     */
    public function toArray(): array
    {
        $reflection = new ReflectionClass($this);
        $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
        $array      = [];
        foreach (Arr::pluck($properties, 'name') as $property) {
            if ($this->propertyIsNotNull($property)) {
                $array[$this->getPropertyAlias($property, $property)] = $this->{$property};
            }
        }

        return $array;
    }

    /**
     * @param $property
     *
     * @return bool
     */
    protected function propertyIsNotNull($property): bool
    {
        return !is_null($this->{$property});
    }

    /**
     * @param $key
     *
     * @return bool
     */
    private function hasPropertyAlias($key): bool
    {
        return Arr::exists($this->propertyAlias, $key);
    }

    /**
     * @param        $key
     * @param string $default
     *
     * @return mixed|string
     */
    private function getPropertyAlias($key, $default = ''): string
    {
        if ($this->hasPropertyAlias($key)) {
            return $this->propertyAlias[$key];
        }

        return $default;
    }

    /**
     * @inheritDoc
     */
    public function toString(): string
    {
        return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE);
    }

    /**
     * @inheritDoc
     */
    public function __invoke(): RequestInterface
    {
        $this->initialize();

        return $this;
    }
}