|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Description: demonstrates a decorator to provide simple output formatting |
|
4
|
|
|
* on the month while still allowing the days to be accessed via the decorator |
|
5
|
|
|
* In practice you _wouldn't_ do this - each decorator comes with a performance |
|
6
|
|
|
* hit for extra method calls. For this example some simple functions could help |
|
7
|
|
|
* format the month while the days are accessed via the normal Month object. |
|
8
|
|
|
*/ |
|
9
|
|
|
if (!@include 'Calendar/Calendar.php') { |
|
10
|
|
|
define('CALENDAR_ROOT', '../../'); |
|
11
|
|
|
} |
|
12
|
|
|
require_once CALENDAR_ROOT . 'Month/Weekdays.php'; |
|
13
|
|
|
require_once CALENDAR_ROOT . 'Decorator.php'; |
|
14
|
|
|
|
|
15
|
|
|
// Decorate a Month with methods to improve formatting |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class MonthDecorator. |
|
19
|
|
|
*/ |
|
20
|
|
|
class MonthDecorator extends Calendar_Decorator |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param object $Month |
|
24
|
|
|
* |
|
25
|
|
|
* @internal param $Calendar_Month |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(&$Month) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($Month); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Override the prevMonth method to format the output. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function prevMonth() |
|
36
|
|
|
{ |
|
37
|
|
|
$prevStamp = parent::prevMonth(true); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
// Build the URL for the previous month |
|
40
|
|
|
return $_SERVER['PHP_SELF'] . '?y=' . date('Y', $prevStamp) . '&m=' . date('n', $prevStamp) . '&d=' . date('j', $prevStamp); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Override the thisMonth method to format the output. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function thisMonth() |
|
47
|
|
|
{ |
|
48
|
|
|
$thisStamp = parent::thisMonth(true); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
// A human readable string from this month |
|
51
|
|
|
return date('F Y', $thisStamp); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Override the nextMonth method to format the output. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function nextMonth() |
|
58
|
|
|
{ |
|
59
|
|
|
$nextStamp = parent::nextMonth(true); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
// Build the URL for next month |
|
62
|
|
|
return $_SERVER['PHP_SELF'] . '?y=' . date('Y', $nextStamp) . '&m=' . date('n', $nextStamp) . '&d=' . date('j', $nextStamp); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (!isset($_GET['y'])) { |
|
67
|
|
|
$_GET['y'] = date('Y'); |
|
68
|
|
|
} |
|
69
|
|
|
if (!isset($_GET['m'])) { |
|
70
|
|
|
$_GET['m'] = date('n'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Creata a month as usual |
|
74
|
|
|
$Month = new Calendar_Month_Weekdays($_GET['y'], $_GET['m']); |
|
75
|
|
|
|
|
76
|
|
|
// Pass it to the decorator and use the decorator from now on... |
|
77
|
|
|
$MonthDecorator = new MonthDecorator($Month); |
|
78
|
|
|
$MonthDecorator->build(); |
|
79
|
|
|
?> |
|
80
|
|
|
|
|
81
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|
82
|
|
|
<html> |
|
83
|
|
|
<head> |
|
84
|
|
|
<title> A Simple Decorator </title> |
|
85
|
|
|
</head> |
|
86
|
|
|
<body> |
|
87
|
|
|
<h1>A Simple Decorator</h1> |
|
88
|
|
|
<table> |
|
89
|
|
|
<caption><?php echo $MonthDecorator->thisMonth(); ?></caption> |
|
90
|
|
|
<?php |
|
91
|
|
|
while ($Day = $MonthDecorator->fetch()) { |
|
92
|
|
|
if ($Day->isFirst()) { |
|
93
|
|
|
echo "\n<tr>\n"; |
|
94
|
|
|
} |
|
95
|
|
|
if ($Day->isEmpty()) { |
|
96
|
|
|
echo '<td> </td>'; |
|
97
|
|
|
} else { |
|
98
|
|
|
echo '<td>' . $Day->thisDay() . '</td>'; |
|
99
|
|
|
} |
|
100
|
|
|
if ($Day->isLast()) { |
|
101
|
|
|
echo "\n</tr>\n"; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
?> |
|
105
|
|
|
<tr> |
|
106
|
|
|
<td><a href="<?php echo $MonthDecorator->prevMonth(); ?>">Prev</a></td> |
|
107
|
|
|
<td colspan="5"> </td> |
|
108
|
|
|
<td><a href="<?php echo $MonthDecorator->nextMonth(); ?>">Next</a></td> |
|
109
|
|
|
</tr> |
|
110
|
|
|
</table> |
|
111
|
|
|
</body> |
|
112
|
|
|
</html> |
|
113
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.