RequiredValidatorAnnotation::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 5
cts 5
cp 1
rs 9.7333
c 0
b 0
f 0
cc 1
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\RequiredProxy;
19
use Maslosoft\Mangan\Validators\Traits\Strict;
20
use Maslosoft\Mangan\Validators\Traits\When;
21
22
/**
23
 * RequiredValidator validates that the specified attribute does not have null or empty value.
24
 *
25
 * When using the {@link message} property to define a custom error message, the message
26
 * may contain additional placeholders that will be replaced with the actual content. In addition
27
 * to the "{attribute}" placeholder, recognized by all validators (see {@link Validator}),
28
 * RequiredValidator allows for the following placeholders to be specified:
29
 * <ul>
30
 * <li>{value}: replaced with the desired value {@link requiredValue}.</li>
31
 * </ul>
32
 *
33
 * @author Qiang Xue <[email protected]>
34
 * @version $Id$
35
 * @package system.validators
36
 * @since 1.0
37
 */
38
class RequiredValidatorAnnotation extends ValidatorAnnotation
39
{
40
41
	use Strict,
42
	  When;
43
44
	/**
45
	 * @var mixed the desired value that the attribute must have.
46
	 * If this is null, the validator will validate that the specified attribute does not have null or empty value.
47
	 * If this is set as a value that is not null, the validator will validate that
48
	 * the attribute has a value that is the same as this property value.
49
	 * Defaults to null.
50
	 */
51
	public $requiredValue = NULL;
52
53 10
	public function init()
54
	{
55 10
		$this->proxy = RequiredProxy::class;
56 10
		$this->getEntity()->validators[] = new ValidatorMeta(ParamsExpander::expand($this, [
57 10
					'requiredValue',
58
					'when',
59
					'strict',
60
					'message',
61
					'skipOnError',
62
					'on',
63
					'safe',
64
					'enableClientValidation',
65
					'except',
66
					'proxy'
67
		]));
68 10
	}
69
70
}
71