Completed
Push — master ( 184221...41c13d )
by Peter
05:18
created

UniqueValidatorAnnotation::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
crap 1
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\ValidatorMeta;
18
use Maslosoft\Mangan\Validators\Proxy\UniqueProxy;
19
use Maslosoft\Mangan\Validators\Traits\AllowEmpty;
20
21
/**
22
 * UniqueValidator
23
 *
24
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
25
 */
26
class UniqueValidatorAnnotation extends ValidatorAnnotation
27
{
28
29
	use AllowEmpty;
30
31
	/**
32
	 * @var string the ActiveRecord class name that should be used to
33
	 * look for the attribute value being validated. Defaults to null, meaning using
34
	 * the class of the object currently being validated.
35
	 * You may use path alias to reference a class name here.
36
	 * @see attributeName
37
	 * @since 1.0.8
38
	 */
39
	public $className;
40
41
	/**
42
	 * @var string the ActiveRecord class attribute name that should be
43
	 * used to look for the attribute value being validated. Defaults to null,
44
	 * meaning using the name of the attribute being validated.
45
	 * @see className
46
	 * @since 1.0.8
47
	 */
48
	public $attributeName;
49
50
	/**
51
	 * @var array additional query criteria. This will be combined with the condition
52
	 * that checks if the attribute value exists in the corresponding table column.
53
	 * This array will be used to instantiate a {@link Criteria} object.
54
	 * @since 1.0.8
55
	 */
56
	public $criteria;
57
58
	/**
59
	 * @var string the user-defined error message. The placeholders "{attribute}" and "{value}"
60
	 * are recognized, which will be replaced with the actual attribute name and value, respectively.
61
	 */
62
	public $message;
63
64 1
	public function init()
65
	{
66 1
		$this->proxy = UniqueProxy::class;
67 1
		$this->getEntity()->validators[] = new ValidatorMeta(ParamsExpander::expand($this, [
68 1
					'allowEmpty',
69
					'className',
70
					'attributeName',
71
					'criteria',
72
					'message',
73
					'skipOnError',
74
					'on',
75
					'safe',
76
					'enableClientValidation',
77
					'except',
78
					'proxy'
79
		]));
80 1
	}
81
82
}
83