Passed
Pull Request — master (#2)
by tsms
01:39
created

Calendar_Decorator_Weekday   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 38
dl 0
loc 117
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A nextWeekDay() 0 12 1
A thisWeekDay() 0 10 1
A setFirstDay() 0 3 1
A prevWeekDay() 0 12 1
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
4
/**
5
 * Contains the Calendar_Decorator_Weekday class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  Date and Time
31
 * @package   Calendar
32
 * @author    Harry Fuecks <[email protected]>
33
 * @author    Lorenzo Alberton <[email protected]>
34
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
35
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
36
 * @version   CVS: $Id$
37
 * @link      http://pear.php.net/package/Calendar
38
 */
39
namespace Pear\Calendar\Decorator;
40
41
/**
42
 * Allows Calendar include path to be redefined
43
 * @ignore
44
 */
45
if (!defined('CALENDAR_ROOT')) {
46
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
47
}
48
49
/**
50
 * Load Calendar decorator base class
51
 */
52
require_once CALENDAR_ROOT.'Decorator.php';
53
54
/**
55
 * Load a Calendar_Day
56
 */
57
require_once CALENDAR_ROOT.'Day.php';
58
/**
59
 * Decorator for fetching the day of the week
60
 * <code>
61
 * $Day = new Calendar_Day(2003, 10, 23);
62
 * $Weekday = new Calendar_Decorator_Weekday($Day);
63
 * $Weekday->setFirstDay(0); // Set first day of week to Sunday (default Mon)
64
 * echo $Weekday->thisWeekDay(); // Displays 5 - fifth day of week relative to Sun
65
 * </code>
66
 *
67
 * @category  Date and Time
68
 * @package   Calendar
69
 * @author    Harry Fuecks <[email protected]>
70
 * @author    Lorenzo Alberton <[email protected]>
71
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
72
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
73
 * @link      http://pear.php.net/package/Calendar
74
 * @access    public
75
 */
76
class Calendar_Decorator_Weekday extends Calendar_Decorator
77
{
78
    /**
79
     * First day of week
80
     * @var int (default = 1 for Monday)
81
     * @access private
82
     */
83
    var $firstDay = 1;
84
85
    /**
86
     * Constructs Calendar_Decorator_Weekday
87
     *
88
     * @param object &$Calendar subclass of Calendar
89
     *
90
     * @access public
91
     */
92
    function __construct(&$Calendar)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
    {
94
        parent::__construct($Calendar);
95
    }
96
97
    /**
98
     * Sets the first day of the week (0 = Sunday, 1 = Monday (default) etc)
99
     *
100
     * @param int $firstDay first day of week
101
     *
102
     * @return void
103
     * @access public
104
     */
105
    function setFirstDay($firstDay) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
    {
107
        $this->firstDay = (int)$firstDay;
108
    }
109
110
    /**
111
     * Returns the previous weekday
112
     *
113
     * @param string $format (default = 'int') return value format
114
     *
115
     * @return int $format numeric day of week or timestamp
116
     * @access public
117
     */
118
    function prevWeekDay($format = 'int')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
119
    {
120
        $ts  = $this->calendar->prevDay('timestamp');
121
        $Day = new Calendar_Day(2000, 1, 1);
0 ignored issues
show
Bug introduced by
The type Pear\Calendar\Decorator\Calendar_Day was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
122
        $Day->setTimeStamp($ts);
123
        $day = $this->calendar->cE->getDayOfWeek(
124
            $Day->thisYear(),
125
            $Day->thisMonth(),
126
            $Day->thisDay()
127
        );
128
        $day = $this->adjustWeekScale($day);
129
        return $this->returnValue('Day', $format, $ts, $day);
130
    }
131
132
    /**
133
     * Returns the current weekday
134
     *
135
     * @param string $format (default = 'int') return value format
136
     *
137
     * @return int numeric day of week or timestamp
138
     * @access public
139
     */
140
    function thisWeekDay($format = 'int')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
141
    {
142
        $ts  = $this->calendar->thisDay('timestamp');
143
        $day = $this->calendar->cE->getDayOfWeek(
144
            $this->calendar->year,
145
            $this->calendar->month,
146
            $this->calendar->day
147
        );
148
        $day = $this->adjustWeekScale($day);
149
        return $this->returnValue('Day', $format, $ts, $day);
150
    }
151
152
    /**
153
     * Returns the next weekday
154
     *
155
     * @param string $format (default = 'int') return value format
156
     *
157
     * @return int numeric day of week or timestamp
158
     * @access public
159
     */
160
    function nextWeekDay($format = 'int')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
161
    {
162
        $ts  = $this->calendar->nextDay('timestamp');
163
        $Day = new Calendar_Day(2000, 1, 1);
164
        $Day->setTimeStamp($ts);
165
        $day = $this->calendar->cE->getDayOfWeek(
166
            $Day->thisYear(),
167
            $Day->thisMonth(),
168
            $Day->thisDay()
169
        );
170
        $day = $this->adjustWeekScale($day);
171
        return $this->returnValue('Day', $format, $ts, $day);
172
    }
173
174
    /**
175
     * Adjusts the day of the week relative to the first day of the week
176
     *
177
     * @param int $dayOfWeek day of week calendar from Calendar_Engine
178
     *
179
     * @return int day of week adjusted to first day
180
     * @access private
181
     */
182
    function adjustWeekScale($dayOfWeek) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
    {
184
        $dayOfWeek = $dayOfWeek - $this->firstDay;
185
        if ($dayOfWeek >= 0) {
186
            return $dayOfWeek;
187
        } else {
188
            return $this->calendar->cE->getDaysInWeek(
189
                $this->calendar->year,
190
                $this->calendar->month,
191
                $this->calendar->day
192
            ) + $dayOfWeek;
193
        }
194
    }
195
}
196