Completed
Push — master ( 77291c...632b2b )
by Peter
06:20
created

RequiredValidator::isValid()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 6.1403
c 0
b 0
f 0
cc 8
eloc 13
nc 5
nop 2
crap 8
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Validators\BuiltIn;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface;
18
use Maslosoft\Mangan\Meta\ManganMeta;
19
use Maslosoft\Mangan\Validators\Traits\Messages;
20
use Maslosoft\Mangan\Validators\Traits\OnScenario;
21
use Maslosoft\Mangan\Validators\Traits\Safe;
22
use Maslosoft\Mangan\Validators\Traits\SkipOnError;
23
use Maslosoft\Mangan\Validators\Traits\Strict;
24
use Maslosoft\Mangan\Validators\Traits\When;
25
26
/**
27
 * RequiredValidator
28
 *
29
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
30
 */
31
class RequiredValidator implements ValidatorInterface
32
{
33
34
	use Strict,
35
	  Messages,
36
	  SkipOnError,
37
	  OnScenario,
38
	  Safe,
39
	  When;
40
41
	/**
42
	 * @var mixed the desired value that the attribute must have.
43
	 * If this is null, the validator will validate that the specified attribute does not have null or empty value.
44
	 * If this is set as a value that is not null, the validator will validate that
45
	 * the attribute has a value that is the same as this property value.
46
	 * Defaults to null.
47
	 */
48
	public $requiredValue = null;
49
50
	/**
51
	 * @var boolean whether the value should be trimmed with php trim() function when comparing strings.
52
	 * When set to false, the attribute value is not considered empty when it contains spaces.
53
	 * Defaults to true, meaning the value will be trimmed.
54
	 * @since 1.1.14
55
	 */
56
	public $trim = true;
57
58
	/**
59
	 * @Label('{attribute} must be {value}')
60
	 * @var string
61
	 */
62
	public $msgExact = '';
63
64
	/**
65
	 * @Label('{attribute} cannot be blank')
66
	 * @var string
67
	 */
68
	public $msgBlank = '';
69
70 21
	public function isValid(AnnotatedInterface $model, $attribute)
71
	{
72 21
		if (!$this->whenValidate($model))
73
		{
74 2
			return true;
75
		}
76 21
		$value = $model->$attribute;
77 21
		$label = ManganMeta::create($model)->field($attribute)->label;
78 21
		if (!empty($this->requiredValue))
79
		{
80 2
			if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue)
81
			{
82 2
				$this->addError('msgExact', ['{attribute}' => $label, '{value}' => $this->requiredValue]);
83 2
				return false;
84
			}
85
		}
86 19
		elseif ($this->isEmpty($value, $this->trim))
87
		{
88 19
			$this->addError('msgBlank', ['{attribute}' => $label]);
89 19
			return false;
90
		}
91 13
		return true;
92
	}
93
94
	/**
95
	 * Checks if the given value is empty.
96
	 * A value is considered empty if it is null, an empty array, or the trimmed result is an empty string.
97
	 * Note that this method is different from PHP empty(). It will return false when the value is 0.
98
	 * @param mixed $value the value to be checked
99
	 * @param boolean $trim whether to perform trimming before checking if the string is empty. Defaults to false.
100
	 * @return boolean whether the value is empty
101
	 */
102 19
	protected function isEmpty($value, $trim = false)
103
	{
104 19
		return $value === null || $value === array() || $value === '' || $trim && is_scalar($value) && trim($value) === '';
105
	}
106
107
}
108