respect-validation/library/Exceptions/LengthException.php

75 lines
2.6 KiB
PHP
Raw Normal View History

<?php
2015-06-08 16:47:14 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Exceptions;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Mazen Touati <mazen_touati@hotmail.com>
*/
final class LengthException extends ValidationException
{
public const BOTH = 'both';
public const LOWER = 'lower';
public const LOWER_INCLUSIVE = 'lower_inclusive';
public const GREATER = 'greater';
public const GREATER_INCLUSIVE = 'greater_inclusive';
public const EXACT = 'exact';
2011-02-19 20:47:34 +01:00
/**
* {@inheritDoc}
*/
protected $defaultTemplates = [
2015-10-18 03:44:47 +02:00
self::MODE_DEFAULT => [
self::BOTH => '{{name}} must have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must have a length greater than {{minValue}}',
self::LOWER_INCLUSIVE => '{{name}} must have a length greater than or equal to {{minValue}}',
self::GREATER => '{{name}} must have a length lower than {{maxValue}}',
self::GREATER_INCLUSIVE => '{{name}} must have a length lower than or equal to {{maxValue}}',
self::EXACT => '{{name}} must have a length of {{maxValue}}',
2015-10-18 03:44:47 +02:00
],
self::MODE_NEGATIVE => [
self::BOTH => '{{name}} must not have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must not have a length greater than {{minValue}}',
self::LOWER_INCLUSIVE => '{{name}} must not have a length greater than or equal to {{minValue}}',
self::GREATER => '{{name}} must not have a length lower than {{maxValue}}',
self::GREATER_INCLUSIVE => '{{name}} must not have a length lower than or equal to {{maxValue}}',
self::EXACT => '{{name}} must not have a length of {{maxValue}}',
2015-10-18 03:44:47 +02:00
],
];
/**
* {@inheritDoc}
*/
protected function chooseTemplate(): string
2011-02-19 20:47:34 +01:00
{
$isInclusive = $this->getParam('inclusive');
if (!$this->getParam('minValue')) {
return $isInclusive === true ? self::GREATER_INCLUSIVE : self::GREATER;
}
if (!$this->getParam('maxValue')) {
return $isInclusive === true ? self::LOWER_INCLUSIVE : self::LOWER;
}
2016-10-30 10:39:23 +01:00
if ($this->getParam('minValue') == $this->getParam('maxValue')) {
return self::EXACT;
}
return self::BOTH;
2011-02-19 20:47:34 +01:00
}
}