Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
16 / 16
IsArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
16 / 16
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 validate
100.00% covered (success)
100.00%
1 / 1
8
100.00% covered (success)
100.00%
13 / 13
 getInverseError
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace Verja\Validator;
use Verja\Error;
use Verja\Validator;
class IsArray extends Validator
{
    const TYPE_ANY = 'any';
    const TYPE_ASSOC = 'assoc';
    const TYPE_INDEX = 'index';
    protected $type;
    /**
     * IsArray constructor.
     *
     * @param string $type 'any' (default), 'assoc' or 'index'
     */
    public function __construct(string $type = self::TYPE_ANY)
    {
        $this->type = $type;
    }
    /**
     * Validate $value
     *
     * @param mixed $value
     * @param array $context
     * @return bool
     */
    public function validate($value, array $context = []): bool
    {
        $parameters = ['type' => $this->type];
        if (!is_array($value)) {
            $this->error = new Error('NO_ARRAY', $value, 'value should be an array', $parameters);
            return false;
        } elseif ($this->type !== self::TYPE_ANY) {
            $isIndexed = empty($value) || array_keys($value) === range(0, count($value) -1);
            if ($this->type === self::TYPE_ASSOC && $isIndexed) {
                $this->error = new Error('NO_ASSOC_ARRAY', $value, 'value should be an associative array', $parameters);
                return false;
            } elseif ($this->type === self::TYPE_INDEX && !$isIndexed) {
                $this->error = new Error('NO_INDEX_ARRAY', $value, 'value should be an indexed array', $parameters);
                return false;
            }
        }
        return true;
    }
    public function getInverseError($value)
    {
        return new Error('IS_ARRAY', $value, 'value should not be an array');
    }
}