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

CountValidatorAnnotation   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 6
dl 0
loc 76
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 23 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\CountProxy;
19
use Maslosoft\Mangan\Validators\Traits\AllowEmpty;
20
use Maslosoft\Mangan\Validators\Traits\When;
21
22
/**
23
 * CountValidator validates that the attribute array elements count is of certain length.
24
 *
25
 * Note, this validator should only be used with array type attributes or
26
 * `Countable` interface instance object.
27
 *
28
 * In addition to the {@link message} property for setting a custom error message,
29
 * CountValidator has a couple custom error messages you can set that correspond to different
30
 * validation scenarios. For defining a custom message when the string is too short,
31
 * you may use the {@link tooShort} property. Similarly with {@link tooLong}. The messages may contain
32
 * placeholders that will be replaced with the actual content. In addition to the "{attribute}"
33
 * placeholder, recognized by all validators (see {@link Validator}), StringValidator allows for the following
34
 * placeholders to be specified:
35
 * <ul>
36
 * <li>{min}: when using {@link tooShort}, replaced with minimum length, {@link min}, if set.</li>
37
 * <li>{max}: when using {@link tooLong}, replaced with the maximum length, {@link max}, if set.</li>
38
 * <li>{length}: when using {@link message}, replaced with the exact required length, {@link is}, if set.</li>
39
 * </ul>
40
 *
41
 * @author Qiang Xue <[email protected]>
42
 * @version $Id$
43
 * @package system.validators
44
 * @since 1.0
45
 */
46
class CountValidatorAnnotation extends ValidatorAnnotation
47
{
48
49
	use AllowEmpty,
50
	  When;
51
52
	/**
53
	 * @var integer maximum length. Defaults to null, meaning no maximum limit.
54
	 */
55
	public $max = null;
56
57
	/**
58
	 * @var integer minimum length. Defaults to null, meaning no minimum limit.
59
	 */
60
	public $min = null;
61
62
	/**
63
	 * @var integer exact length. Defaults to null, meaning no exact length limit.
64
	 */
65
	public $is = null;
66
67
	/**
68
	 * @var string user-defined error message used when the value is too short.
69
	 */
70
	public $tooShort = null;
71
72
	/**
73
	 * @var string user-defined error message used when the value is too long.
74
	 */
75
	public $tooLong = null;
76
77
	/**
78
	 * @var string
79
	 */
80
	public $msgInvalid = '';
81
82
	/**
83
	 * @var string
84
	 */
85
	public $msgTooShort = '';
86
87
	/**
88
	 * @var string
89
	 */
90
	public $msgTooLong = '';
91
92
	/**
93
	 * @var string
94
	 */
95
	public $msgLength = '';
96
97
	public function init()
98
	{
99
		$this->proxy = CountProxy::class;
100
		$this->getEntity()->validators[] = new ValidatorMeta(ParamsExpander::expand($this, [
101
					'max',
102
					'min',
103
					'is',
104
					'when',
105
					'tooShort',
106
					'tooLong',
107
					'msgInvalid',
108
					'msgTooShort',
109
					'msgTooLong',
110
					'msgLength',
111
					'allowEmpty',
112
					'message',
113
					'skipOnError',
114
					'on',
115
					'safe',
116
					'except',
117
					'proxy'
118
		]));
119
	}
120
121
}
122