<?php

namespace Dingo\Api\Http;

use Illuminate\Http\Request as IlluminateRequest;

class Validator
{
    protected $domain;

    protected $prefix;

    /**
     * Create a new request validator instance.
     *
     * @param string $domain
     * @param string $prefix
     *
     * @return void
     */
    public function __construct($domain = null, $prefix = null)
    {
        $this->domain = $domain;
        $this->prefix = $prefix;
    }

    /**
     * Validate a request.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return bool
     */
    public function validateRequest(IlluminateRequest $request)
    {
        return $this->validateDomain($request) || $this->validatePrefix($request);
    }

    /**
     * Validates domain in the request.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return bool
     */
    public function validateDomain(IlluminateRequest $request)
    {
        return ! is_null($this->domain) && $request->header('host') == $this->domain;
    }

    /**
     * Validates prefix in the request.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return bool
     */
    public function validatePrefix(IlluminateRequest $request)
    {
        $prefix = $this->filterAndExplode($this->prefix);

        $path = $this->filterAndExplode($request->getPathInfo());

        return ! is_null($this->prefix) && $prefix == array_slice($path, 0, count($prefix));
    }

    /**
     * Explode array on slash and remove empty values.
     *
     * @param array $array
     *
     * @return array
     */
    protected function filterAndExplode($array)
    {
        return array_filter(explode('/', $array));
    }
}

<?php

namespace Dingo\Api\Http;

use Illuminate\Container\Container;
use Illuminate\Http\Request as IlluminateRequest;

class Validator
{
    /**
     * Container instance.
     *
     * @var \Illuminate\Container\Container
     */
    protected $container;

    /**
     * Array of request validators.
     *
     * @var array
     */
    protected $validators = [
        'Dingo\Api\Http\Matching\DomainValidator',
        'Dingo\Api\Http\Matching\PrefixValidator',
        'Dingo\Api\Http\Matching\AcceptValidator'
    ];

    /**
     * Create a new request validator instance.
     *
     * @param \Illuminate\Container\Container $container
     *
     * @return void
     */
    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    /**
     * Validate a request.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return bool
     */
    public function validateRequest(IlluminateRequest $request)
    {
        foreach ($this->validators as $validator) {
            $validator = $this->container->make($validator);

            if (! $validator->matches($request)) {
                return false;
            }
        }

        return true;
    }
}

Implement a better way of validating incoming requests.
Implement a better way of validating incoming requests.

Signed-off-by: Jason Lewis <b136be6b8ecc2c62ceb9857ec62bf85489a45e0c@gmail.com>
