Completed
Push — master ( 90a4d3...4d725e )
by Jean-Christophe
01:38
created

AbstractDateTimeValidator::hasWarnings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ubiquity\contents\validation\validators\dates;
4
5
use Ubiquity\contents\validation\validators\Validator;
6
use Ubiquity\contents\validation\validators\ConstraintViolation;
7
8
abstract class AbstractDateTimeValidator extends Validator {
9
	
10
	protected $ref;
11
	protected $strict=true;
12
	
13
	protected $warnings=[];
14
	
15
	public function validate($value) {
16
		parent::validate($value);
17
		$value = (string) $value;
18
		\DateTime::createFromFormat($this->ref, $value);
19
		$errors = \DateTime::getLastErrors();
20
		foreach ($errors['warnings'] as $warning) {
21
			$this->warnings[]=new ConstraintViolation($warning, $value, $this->member, get_class($this), 'warning');
22
		}
23
		return $errors['error_count']<=0 && (!$this->strict || $errors['warning_count']<=0);
24
	}
25
	
26
	/**
27
	 * {@inheritDoc}
28
	 * @see \Ubiquity\contents\validation\validators\Validator::getParameters()
29
	 */
30
	public function getParameters(): array {
31
		return ["ref","value"];
32
	}
33
	
34
	/**
35
	 * @return mixed
36
	 */
37
	public function getWarnings() {
38
		return $this->warnings;
39
	}
40
	
41
	public function hasWarnings(){
42
		return sizeof($this->warnings)>0;
43
	}
44
45
46
}
47
48