Completed
Push — master ( 4b8bfd...f58c94 )
by onixsib
02:42
created

MACValidator::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * @author: onixsib <[email protected]>
4
 * @date:   03.08.2016
5
 * @copyright onixsib
6
 * @link http://onixsib.ru/
7
 * @file MACValidator
8
 */
9
namespace onixsib\validators;
10
11
use Yii;
12
use yii\validators\Validator;
13
14
/**
15
 * Class MACValidator
16
 */
17 View Code Duplication
class MACValidator extends Validator {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
	/**
19
	 * @inheritdoc
20
	 */
21
	public function init() {
22
		parent::init();
23
		if ($this->message === null) {
24
			$this->message = Yii::t('yii', "{attribute} is invalid.");
25
		}
26
	}
27
28
	public function validateAttribute($model, $attribute) {
29
		$string = $model->{$attribute};
30
31
		// filter_var suxx
32
		if (!preg_match('/^([0-9a-F]{1,2}[\.:-]){5}([0-9a-F]{1,2})$/', $string)) {
33
			$this->addError($model, $attribute, Yii::t('yii', 'This in not valid MAC address {sample}', ['sample' => '00:00:00:00:00:01']));
34
			return false;
35
		}
36
	}
37
}
38
39
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...