Passed
Branch scrutinizer (a663d7)
by Wanderson
01:44
created

DateTime::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Calendar;
4
5
use \DateTime as DateTimePHP;
6
7
/**
8
 * Data e Hora
9
 */
10
class DateTime extends DateTimePHP {
11
12
	const BR_DATE = 'd/m/Y';
13
	const BR_DATE_TIME = 'd/m/Y H:i:s';
14
	const SQL_DATE = 'Y-m-d';
15
	const SQL_DATE_TIME = 'Y-m-d H:i:s';
16
	const ZERO_DATE_TIME = '00/00/0000 00:00:00';
17
18
	/** @return boolean */
19
	public function isEmpty() {
20
		return (boolean) ($this->format('y') < 0);
21
	}
22
23
	/** @return string */
24
	public function getMonthName() {
25
		return Month::getName($this->format('m'));
0 ignored issues
show
Bug introduced by
$this->format('m') of type string is incompatible with the type integer expected by parameter $month of Win\Calendar\Month::getName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
		return Month::getName(/** @scrutinizer ignore-type */ $this->format('m'));
Loading history...
26
	}
27
28
	/** @return string */
29
	public function getMonthAbbre() {
30
		return Month::getNameAbbre($this->format('m'));
0 ignored issues
show
Bug introduced by
$this->format('m') of type string is incompatible with the type integer expected by parameter $month of Win\Calendar\Month::getNameAbbre(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
		return Month::getNameAbbre(/** @scrutinizer ignore-type */ $this->format('m'));
Loading history...
31
	}
32
33
	/** @return int */
34
	public function getAge($date = 'NOW') {
35
		return $this->diff($date)->format('%y');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->diff($date)->format('%y') returns the type string which is incompatible with the documented return type integer.
Loading history...
36
	}
37
38
	/** @return string */
39
	public function toHtml() {
40
		return $this->format(static::BR_DATE_TIME);
41
	}
42
43
	/** @return string */
44
	public function __toString() {
45
		return $this->format(static::BR_DATE);
46
	}
47
48
	/** @return string */
49
	public function toSql() {
50
		return $this->format(static::SQL_DATE_TIME);
51
	}
52
53
54
	/**
55
	 * Retorna TRUE se a data é valida
56
	 * @param string $date
57
	 * @param string|null $format
58
	 * @return boolean
59
	 */
60
	public static function isValid($date, $format = null) {
61
		return (static::createFromFormat(!is_null($format) ? $format : static::BR_DATE_TIME, $date) !== false);
62
	}
63
64
	/**
65
	 * @param string $format
66
	 * @param string $time
67
	 * @return boolean|static
68
	 */
69
	public static function create($format, $time) {
70
		$dateTimePHP = parent::createFromFormat($format, $time);
71
		return new DateTime($dateTimePHP->format(static::SQL_DATE_TIME));
72
	}
73
74
	/**
75
	 * Cria uma data vazia: 00/00/0000
76
	 * @return static
77
	 */
78
	public static function createEmpty() {
79
		return static::create(static::BR_DATE_TIME, static::ZERO_DATE_TIME);
80
	}
81
82
}
83