MonthDecorator::thisMonth()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Description: demonstrates a decorator to provide simple output formatting
5
 * on the month while still allowing the days to be accessed via the decorator
6
 * In practice you _wouldn't_ do this - each decorator comes with a performance
7
 * hit for extra method calls. For this example some simple functions could help
8
 * format the month while the days are accessed via the normal Month object.
9
 */
10
if (!@require_once __DIR__ . '/Calendar/Calendar.php') {
11
    define('CALENDAR_ROOT', '../../');
12
}
13
require_once CALENDAR_ROOT . 'Month/Weekdays.php';
14
require_once CALENDAR_ROOT . 'Decorator.php';
15
16
// Decorate a Month with methods to improve formatting
17
18
/**
19
 * Class MonthDecorator.
20
 */
21
class MonthDecorator extends Calendar_Decorator
22
{
23
    /**
24
     * @param object $Month
25
     *
26
     * @internal param $Calendar_Month
27
     */
28
    public function __construct(&$Month)
29
    {
30
        parent::__construct($Month);
31
    }
32
33
    /**
34
     * Override the prevMonth method to format the output.
35
     */
36
    public function prevMonth()
37
    {
38
        $prevStamp = parent::prevMonth(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $format of Calendar_Decorator::prevMonth(). ( Ignorable by Annotation )

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

38
        $prevStamp = parent::prevMonth(/** @scrutinizer ignore-type */ true);
Loading history...
39
40
        // Build the URL for the previous month
41
        return $_SERVER['SCRIPT_NAME'] . '?y=' . date('Y', $prevStamp) . '&m=' . date('n', $prevStamp) . '&d=' . date('j', $prevStamp);
42
    }
43
44
    /**
45
     * Override the thisMonth method to format the output.
46
     */
47
    public function thisMonth()
48
    {
49
        $thisStamp = parent::thisMonth(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $format of Calendar_Decorator::thisMonth(). ( Ignorable by Annotation )

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

49
        $thisStamp = parent::thisMonth(/** @scrutinizer ignore-type */ true);
Loading history...
50
51
        // A human readable string from this month
52
        return date('F Y', $thisStamp);
53
    }
54
55
    /**
56
     * Override the nextMonth method to format the output.
57
     */
58
    public function nextMonth()
59
    {
60
        $nextStamp = parent::nextMonth(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $format of Calendar_Decorator::nextMonth(). ( Ignorable by Annotation )

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

60
        $nextStamp = parent::nextMonth(/** @scrutinizer ignore-type */ true);
Loading history...
61
62
        // Build the URL for next month
63
        return $_SERVER['SCRIPT_NAME'] . '?y=' . date('Y', $nextStamp) . '&m=' . date('n', $nextStamp) . '&d=' . date('j', $nextStamp);
64
    }
65
}
66
67
if (!isset($_GET['y'])) {
68
    $_GET['y'] = date('Y');
69
}
70
if (!isset($_GET['m'])) {
71
    $_GET['m'] = date('n');
72
}
73
74
// Creata a month as usual
75
$Month = new Calendar_Month_Weekdays($_GET['y'], $_GET['m']);
76
77
// Pass it to the decorator and use the decorator from now on...
78
$MonthDecorator = new MonthDecorator($Month);
79
$MonthDecorator->build();
80
?>
81
82
<!DOCTYPE html>
83
<html>
84
<head>
85
    <title> A Simple Decorator </title>
86
</head>
87
<body>
88
<h1>A Simple Decorator</h1>
89
<table>
90
    <caption><?php echo $MonthDecorator->thisMonth(); ?></caption>
91
    <?php
92
    while (false !== ($Day = $MonthDecorator->fetch())) {
93
        if ($Day->isFirst()) {
94
            echo "\n<tr>\n";
95
        }
96
        if ($Day->isEmpty()) {
97
            echo '<td>&nbsp;</td>';
98
        } else {
99
            echo '<td>' . $Day->thisDay() . '</td>';
100
        }
101
        if ($Day->isLast()) {
102
            echo "\n</tr>\n";
103
        }
104
    }
105
    ?>
106
    <tr>
107
        <td><a href="<?php echo $MonthDecorator->prevMonth(); ?>">Prev</a></td>
108
        <td colspan="5">&nbsp;</td>
109
        <td><a href="<?php echo $MonthDecorator->nextMonth(); ?>">Next</a></td>
110
    </tr>
111
</table>
112
</body>
113
</html>
114