ValidatorAnnotation::init()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 11
cts 12
cp 0.9167
rs 9.1608
c 0
b 0
f 0
cc 5
nc 3
nop 0
crap 5.0144
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\Annotations\Validators;
15
16
use Maslosoft\Addendum\Helpers\ParamsExpander;
17
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
18
use Maslosoft\Mangan\Meta\ValidatorMeta;
19
use Maslosoft\Mangan\Validators\Proxy\ClassValidatorProxy;
20
use UnexpectedValueException;
21
22
/**
23
 * Base class for validator annotations
24
 *
25
 * @author Piotr
26
 */
27
class ValidatorAnnotation extends ManganPropertyAnnotation
28
{
29
30
	const Ns = __NAMESPACE__;
31
32
	/**
33
	 * @var string the user-defined error message. Different validators may define various
34
	 * placeholders in the message that are to be replaced with actual values. All validators
35
	 * recognize "{attribute}" placeholder, which will be replaced with the label of the attribute.
36
	 */
37
	public $message;
38
39
	/**
40
	 * @var boolean whether this validation rule should be skipped when there is already a validation
41
	 * error for the current attribute. Defaults to false.
42
	 * @since 1.1.1
43
	 */
44
	public $skipOnError = false;
45
46
	/**
47
	 * @var array list of scenarios that the validator should be applied.
48
	 * Each array value refers to a scenario name with the same name as its array key.
49
	 */
50
	public $on;
51
52
	/**
53
	 * @var boolean whether attributes listed with this validator should be considered safe for massive assignment.
54
	 * Defaults to true.
55
	 * @since 1.1.4
56
	 */
57
	public $safe = true;
58
59
	/**
60
	 * @var array list of scenarios that the validator should not be applied to.
61
	 * Each array value refers to a scenario name with the same name as its array key.
62
	 * @since 1.1.11
63
	 */
64
	public $except;
65
66
	/**
67
	 * Validator proxy class
68
	 * @var string
69
	 */
70
	public $proxy = '';
71
	public $class = '';
72
	public $value = '';
73
74 3
	public function init()
75
	{
76
		$params = [
77 3
			'class'
78
		];
79 3
		if (is_string($this->value))
80
		{
81 2
			$this->class = $this->value;
82
		}
83 1
		elseif (is_array($this->value))
84
		{
85 1
			foreach (array_keys($this->value) as $key)
86
			{
87 1
				if (!is_numeric($key))
88
				{
89 1
					$params[] = $key;
90
				}
91
			}
92
		}
93
		else
94
		{
95
			throw new UnexpectedValueException(sprintf("Expected class name (for @Validator) for field `%s` of model `%s`, got: `%s`", $this->getEntity()->name, $this->getMeta()->type()->name, $this->value));
96
		}
97
98
99 3
		$this->proxy = ClassValidatorProxy::class;
100 3
		$this->getEntity()->validators[] = new ValidatorMeta(ParamsExpander::expand($this, $params));
101 3
	}
102
103
}
104