Calendar_Month_Weekdays   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 35
dl 0
loc 107
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setWeekMarkers() 0 7 2
A shiftDays() 0 5 2
A buildEmptyDaysAfter() 0 9 2
A buildEmptyDaysBefore() 0 9 2
A build() 0 11 1
1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4: */
4
5
/**
6
 * Contains the Calendar_Month_Weekdays class.
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * LICENSE: Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 * 3. The name of the author may not be used to endorse or promote products
18
 *    derived from this software without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
21
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
24
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 *
31
 * @category  Date and Time
32
 *
33
 * @author    Harry Fuecks <[email protected]>
34
 * @copyright 2003-2007 Harry Fuecks
35
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
36
 *
37
 * @link      http://pear.php.net/package/Calendar
38
 */
39
40
/**
41
 * Allows Calendar include path to be redefined.
42
 *
43
 * @ignore
44
 */
45
if (!defined('CALENDAR_ROOT')) {
46
    define('CALENDAR_ROOT', 'Calendar/');
47
}
48
49
/**
50
 * Load Calendar base class.
51
 */
52
require_once CALENDAR_ROOT . 'Calendar.php';
53
54
/**
55
 * Load base month.
56
 */
57
require_once CALENDAR_ROOT . 'Month.php';
58
59
/**
60
 * Represents a Month and builds Days in tabular form<br>
61
 * <code>
62
 * require_once __DIR__ . '/Calendar/Month/Weekdays.php';
63
 * $Month = new Calendar_Month_Weekdays(2003, 10); // Oct 2003
64
 * $Month->build(); // Build Calendar_Day objects
65
 * while ($Day = $Month->fetch()) {
66
 *     if ($Day->isFirst()) {
67
 *         echo '<tr>';
68
 *     }
69
 *     if ($Day->isEmpty()) {
70
 *         echo '<td>&nbsp;</td>';
71
 *     } else {
72
 *         echo '<td>'.$Day->thisDay().'</td>';
73
 *     }
74
 *     if ($Day->isLast()) {
75
 *         echo '</tr>';
76
 *     }
77
 * }
78
 * </code>.
79
 *
80
 * @category  Date and Time
81
 *
82
 * @author    Harry Fuecks <[email protected]>
83
 * @copyright 2003-2007 Harry Fuecks
84
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
85
 *
86
 * @link      http://pear.php.net/package/Calendar
87
 */
88
class Calendar_Month_Weekdays extends Calendar_Month
89
{
90
    /**
91
     * Instance of Calendar_Table_Helper.
92
     *
93
     * @var Calendar_Table_Helper
94
     */
95
    public $tableHelper;
96
97
    /**
98
     * First day of the week.
99
     *
100
     * @var string
101
     */
102
    public $firstDay;
103
104
    /**
105
     * Constructs Calendar_Month_Weekdays.
106
     *
107
     * @param int $y        year e.g. 2003
108
     * @param int $m        month e.g. 5
109
     * @param int $firstDay (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.)
110
     */
111
    public function __construct($y, $m, $firstDay = null)
112
    {
113
        $this->firstDay = $firstDay;
114
        parent::__construct($y, $m, $firstDay);
115
    }
116
117
    /**
118
     * Builds Day objects in tabular form, to allow display of calendar month
119
     * with empty cells if the first day of the week does not fall on the first
120
     * day of the month.
121
     *
122
     * @param array $sDates (optional) Calendar_Day objects representing selected dates
123
     *
124
     * @return bool
125
     *
126
     * @see    Calendar_Day::isEmpty()
127
     * @see    Calendar_Day_Base::isFirst()
128
     * @see    Calendar_Day_Base::isLast()
129
     */
130
    public function build($sDates = [])
131
    {
132
        require_once CALENDAR_ROOT . 'Table/Helper.php';
133
        $this->tableHelper = new Calendar_Table_Helper($this, $this->firstDay);
0 ignored issues
show
Bug introduced by
$this->firstDay of type string is incompatible with the type integer expected by parameter $firstDay of Calendar_Table_Helper::__construct(). ( Ignorable by Annotation )

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

133
        $this->tableHelper = new Calendar_Table_Helper($this, /** @scrutinizer ignore-type */ $this->firstDay);
Loading history...
134
        Calendar_Month::build($sDates);
135
        $this->buildEmptyDaysBefore();
136
        $this->shiftDays();
137
        $this->buildEmptyDaysAfter();
138
        $this->setWeekMarkers();
139
140
        return true;
141
    }
142
143
    /**
144
     * Prepends empty days before the real days in the month.
145
     */
146
    public function buildEmptyDaysBefore()
147
    {
148
        $eBefore = $this->tableHelper->getEmptyDaysBefore();
149
        for ($i = 0; $i < $eBefore; ++$i) {
150
            $stamp = $this->cE->dateToStamp($this->year, $this->month, -$i);
151
            $Day   = new Calendar_Day($this->cE->stampToYear($stamp), $this->cE->stampToMonth($stamp), $this->cE->stampToDay($stamp));
152
            $Day->setEmpty();
153
            $Day->adjust();
154
            array_unshift($this->children, $Day);
155
        }
156
    }
157
158
    /**
159
     * Shifts the array of children forward, if necessary.
160
     */
161
    public function shiftDays()
162
    {
163
        if (isset($this->children[0])) {
164
            array_unshift($this->children, null);
165
            unset($this->children[0]);
166
        }
167
    }
168
169
    /**
170
     * Appends empty days after the real days in the month.
171
     */
172
    public function buildEmptyDaysAfter()
173
    {
174
        $eAfter = $this->tableHelper->getEmptyDaysAfter();
175
        $sDOM   = $this->tableHelper->getNumTableDaysInMonth();
176
        for ($i = 1; $i <= $sDOM - $eAfter; ++$i) {
177
            $Day = new Calendar_Day($this->year, $this->month + 1, $i);
178
            $Day->setEmpty();
179
            $Day->adjust();
180
            array_push($this->children, $Day);
181
        }
182
    }
183
184
    /**
185
     * Sets the "markers" for the beginning and of a of week, in the
186
     * built Calendar_Day children.
187
     */
188
    public function setWeekMarkers()
189
    {
190
        $dIW  = $this->cE->getDaysInWeek($this->thisYear(), $this->thisMonth(), $this->thisDay());
191
        $sDOM = $this->tableHelper->getNumTableDaysInMonth();
192
        for ($i = 1; $i <= $sDOM; $i += $dIW) {
193
            $this->children[$i]->setFirst();
194
            $this->children[$i + ($dIW - 1)]->setLast();
195
        }
196
    }
197
}
198