LengthValidatorAnnotation   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 88
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 88
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

1 Method

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