UrlValidator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 7
dl 0
loc 28
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 11 2
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\Validators\BuiltIn;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface;
18
use Maslosoft\Mangan\Meta\ManganMeta;
19
use Maslosoft\Mangan\Validators\Traits\AllowEmpty;
20
use Maslosoft\Mangan\Validators\Traits\Messages;
21
use Maslosoft\Mangan\Validators\Traits\OnScenario;
22
use Maslosoft\Mangan\Validators\Traits\Safe;
23
use Maslosoft\Mangan\Validators\Traits\SkipOnError;
24
25
/**
26
 * UrlValidator
27
 *
28
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
29
 */
30
class UrlValidator implements ValidatorInterface
31
{
32
33
	use AllowEmpty,
34
	  Messages,
35
	  OnScenario,
36
	  Safe,
37
	  SkipOnError;
38
39
	/**
40
	 * @Label('{attribute} must be valid url')
41
	 * @var string
42
	 */
43
	public $msgNotUrl = '';
44
45 4
	public function isValid(AnnotatedInterface $model, $attribute)
46
	{
47 4
		$valid = filter_var($model->$attribute, FILTER_VALIDATE_URL);
48 4
		if (!$valid)
49
		{
50 2
			$label = ManganMeta::create($model)->field($attribute)->label;
51 2
			$this->addError('msgNotUrl', ['{attribute}' => $label]);
52 2
			return false;
53
		}
54 2
		return true;
55
	}
56
57
}
58