-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvent.php
executable file
·99 lines (82 loc) · 2.42 KB
/
Event.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Upcast\Test;
abstract class Event {
const NICE_DATE_FORMAT = 'D, d M Y';
const DATE_TITLE_DEFAULT = 'event date';
/**
* @var \DateTime
*/
protected $targetDate;
/**
* @var \DateTime
*/
protected $actualDate;
/**
* @var array Array of day names e.g. [Saturday,Sunday]
*
*/
protected $unavailableDays;
/**
* @var string String containing "Relative Formats" instructions
*
* @link http://php.net/manual/en/datetime.formats.relative.php
*/
protected $alternativeDay;
/**
* @var string
*/
protected $dateTitle = self::DATE_TITLE_DEFAULT;
abstract public function __construct(\DateTime $dateTime);
/**
* @param \DateTime $targetDate
*/
public function setTargetDate(\DateTime $targetDate){
$this->targetDate = $targetDate;
}
/**
* @return \DateTime
*/
public function getTargetDate(){
return $this->targetDate;
}
/**
* @param \DateTime $actualDate
*/
public function setActualDate(\DateTime $actualDate){
$this->actualDate = $actualDate;
}
/**
* @return \DateTime
*/
public function getActualDate(){
return $this->actualDate;
}
/**
* @return null|string
*/
public function getActualDateNice(){
if(!empty($this->actualDate)){
return $this->actualDate->format(self::NICE_DATE_FORMAT);
}
return null;
}
public function getDateTitle(){
return $this->dateTitle;
}
/**
* This function checks whether the target date happens on an unavailable day and uses instructions given in
* self::$alternativeDay to change the date
*/
public function checkDateAvailability(){
if(!empty($this->targetDate) && $this->alternativeDay && is_array($this->unavailableDays)){
$targetDay = $this->targetDate->format('l');
if(in_array($targetDay,$this->unavailableDays)){
$targetDate = clone $this->targetDate;
$actualTimestamp = strtotime($this->alternativeDay, $targetDate->getTimestamp());
$actualDate = new \DateTime();
$actualDate->setTimestamp($actualTimestamp);
$this->setActualDate($actualDate);
}
}
}
}