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

Calendar_Util_Textual::orderedWeekdays()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 32
rs 8.6506
c 0
b 0
f 0
cc 7
nc 8
nop 2
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
4
/**
5
 * Contains the Calendar_Util_Textual 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\Util;
40
41
/**
42
 * @package Calendar
43
 * @version $Id$
44
 */
45
46
/**
47
 * Static utlities to help with fetching textual representations of months and
48
 * days of the week.
49
 *
50
 * @category  Date and Time
51
 * @package   Calendar
52
 * @author    Harry Fuecks <[email protected]>
53
 * @author    Lorenzo Alberton <[email protected]>
54
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
55
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
56
 * @link      http://pear.php.net/package/Calendar
57
 * @access    public
58
 */
59
class Textual
60
{
61
62
    /**
63
     * Returns an array of 12 month names (first index = 1)
64
     *
65
     * @param string $format (optional) format of returned months (one|two|short|long)
66
     *
67
     * @return array
68
     * @access public
69
     * @static
70
     */
71
    function monthNames($format = 'long')
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...
72
    {
73
        $formats = array(
74
            'one'   => '%b', 
75
            'two'   => '%b', 
76
            'short' => '%b', 
77
            'long'  => '%B',
78
        );
79
        if (!array_key_exists($format, $formats)) {
80
            $format = 'long';
81
        }
82
        $months = array();
83
        for ($i=1; $i<=12; $i++) {
84
            $stamp = mktime(0, 0, 0, $i, 1, 2003);
85
            $month = strftime($formats[$format], $stamp);
86
            switch($format) {
87
            case 'one':
88
                $month = substr($month, 0, 1);
89
                break;
90
            case 'two':
91
                $month = substr($month, 0, 2);
92
                break;
93
            }
94
            $months[$i] = $month;
95
        }
96
        return $months;
97
    }
98
99
    /**
100
     * Returns an array of 7 week day names (first index = 0)
101
     *
102
     * @param string $format (optional) format of returned days (one,two,short or long)
103
     *
104
     * @return array
105
     * @access public
106
     * @static
107
     */
108
    function weekdayNames($format = 'long')
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...
109
    {
110
        $formats = array(
111
            'one'   => '%a', 
112
            'two'   => '%a', 
113
            'short' => '%a', 
114
            'long'  => '%A',
115
        );
116
        if (!array_key_exists($format, $formats)) {
117
            $format = 'long';
118
        }
119
        $days = array();
120
        for ($i=0; $i<=6; $i++) {
121
            $stamp = mktime(0, 0, 0, 11, $i+2, 2003);
122
            $day = strftime($formats[$format], $stamp);
123
            switch($format) {
124
            case 'one':
125
                $day = substr($day, 0, 1);
126
                break;
127
            case 'two':
128
                $day = substr($day, 0, 2);
129
                break;
130
            }
131
            $days[$i] = $day;
132
        }
133
        return $days;
134
    }
135
136
    /**
137
     * Returns textual representation of the previous month of the decorated calendar object
138
     *
139
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
140
     * @param string $format   (optional) format of returned months (one,two,short or long)
141
     *
142
     * @return string
143
     * @access public
144
     * @static
145
     */
146
    function prevMonthName($Calendar, $format = 'long')
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...
147
    {
148
        $months = Textual::monthNames($format);
0 ignored issues
show
Bug Best Practice introduced by
The method Pear\Calendar\Util\Textual::monthNames() is not static, but was called statically. ( Ignorable by Annotation )

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

148
        /** @scrutinizer ignore-call */ 
149
        $months = Textual::monthNames($format);
Loading history...
149
        return $months[$Calendar->prevMonth()];
150
    }
151
152
    /**
153
     * Returns textual representation of the month of the decorated calendar object
154
     *
155
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
156
     * @param string $format   (optional) format of returned months (one,two,short or long)
157
     *
158
     * @return string
159
     * @access public
160
     * @static
161
     */
162
    function thisMonthName($Calendar, $format = 'long')
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...
163
    {
164
        $months = Textual::monthNames($format);
0 ignored issues
show
Bug Best Practice introduced by
The method Pear\Calendar\Util\Textual::monthNames() is not static, but was called statically. ( Ignorable by Annotation )

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

164
        /** @scrutinizer ignore-call */ 
165
        $months = Textual::monthNames($format);
Loading history...
165
        return $months[$Calendar->thisMonth()];
166
    }
167
168
    /**
169
     * Returns textual representation of the next month of the decorated calendar object
170
     *
171
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
172
     * @param string $format   (optional) format of returned months (one,two,short or long)
173
     *
174
     * @return string
175
     * @access public
176
     * @static
177
     */
178
    function nextMonthName($Calendar, $format = 'long')
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...
179
    {
180
        $months = Textual::monthNames($format);
0 ignored issues
show
Bug Best Practice introduced by
The method Pear\Calendar\Util\Textual::monthNames() is not static, but was called statically. ( Ignorable by Annotation )

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

180
        /** @scrutinizer ignore-call */ 
181
        $months = Textual::monthNames($format);
Loading history...
181
        return $months[$Calendar->nextMonth()];
182
    }
183
184
    /**
185
     * Returns textual representation of the previous day of week of the decorated calendar object
186
     * <b>Note:</b> Requires PEAR::Date
187
     *
188
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
189
     * @param string $format   (optional) format of returned months (one,two,short or long)
190
     *
191
     * @return string
192
     * @access public
193
     * @static
194
     */
195
    function prevDayName($Calendar, $format = 'long')
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...
196
    {
197
        $days = Textual::weekdayNames($format);
0 ignored issues
show
Bug Best Practice introduced by
The method Pear\Calendar\Util\Textual::weekdayNames() is not static, but was called statically. ( Ignorable by Annotation )

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

197
        /** @scrutinizer ignore-call */ 
198
        $days = Textual::weekdayNames($format);
Loading history...
198
        $stamp = $Calendar->prevDay('timestamp');
199
        $cE = $Calendar->getEngine();
200
        include_once 'Date/Calc.php';
201
        $day = Date_Calc::dayOfWeek($cE->stampToDay($stamp),
0 ignored issues
show
Bug introduced by
The type Pear\Calendar\Util\Date_Calc 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...
202
            $cE->stampToMonth($stamp), $cE->stampToYear($stamp));
203
        return $days[$day];
204
    }
205
206
    /**
207
     * Returns textual representation of the day of week of the decorated calendar object
208
     * <b>Note:</b> Requires PEAR::Date
209
     *
210
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
211
     * @param string $format   (optional) format of returned months (one,two,short or long)
212
     *
213
     * @return string
214
     * @access public
215
     * @static
216
     */
217
    function thisDayName($Calendar, $format='long')
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...
218
    {
219
        $days = Textual::weekdayNames($format);
0 ignored issues
show
Bug Best Practice introduced by
The method Pear\Calendar\Util\Textual::weekdayNames() is not static, but was called statically. ( Ignorable by Annotation )

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

219
        /** @scrutinizer ignore-call */ 
220
        $days = Textual::weekdayNames($format);
Loading history...
220
        include_once 'Date/Calc.php';
221
        $day = Date_Calc::dayOfWeek($Calendar->thisDay(), $Calendar->thisMonth(), $Calendar->thisYear());
222
        return $days[$day];
223
    }
224
225
    /**
226
     * Returns textual representation of the next day of week of the decorated calendar object
227
     *
228
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
229
     * @param string $format   (optional) format of returned months (one,two,short or long)
230
     *
231
     * @return string
232
     * @access public
233
     * @static
234
     */
235
    function nextDayName($Calendar, $format='long')
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...
236
    {
237
        $days = Textual::weekdayNames($format);
0 ignored issues
show
Bug Best Practice introduced by
The method Pear\Calendar\Util\Textual::weekdayNames() is not static, but was called statically. ( Ignorable by Annotation )

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

237
        /** @scrutinizer ignore-call */ 
238
        $days = Textual::weekdayNames($format);
Loading history...
238
        $stamp = $Calendar->nextDay('timestamp');
239
        $cE = $Calendar->getEngine();
240
        include_once 'Date/Calc.php';
241
        $day = Date_Calc::dayOfWeek($cE->stampToDay($stamp),
242
            $cE->stampToMonth($stamp), $cE->stampToYear($stamp));
243
        return $days[$day];
244
    }
245
246
    /**
247
     * Returns the days of the week using the order defined in the decorated
248
     * calendar object. Only useful for Calendar_Month_Weekdays, Calendar_Month_Weeks
249
     * and Calendar_Week. Otherwise the returned array will begin on Sunday
250
     *
251
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
252
     * @param string $format   (optional) format of returned months (one,two,short or long)
253
     *
254
     * @return array ordered array of week day names
255
     * @access public
256
     * @static
257
     */
258
    function orderedWeekdays($Calendar, $format = 'long')
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...
259
    {
260
        $days = Textual::weekdayNames($format);
0 ignored issues
show
Bug Best Practice introduced by
The method Pear\Calendar\Util\Textual::weekdayNames() is not static, but was called statically. ( Ignorable by Annotation )

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

260
        /** @scrutinizer ignore-call */ 
261
        $days = Textual::weekdayNames($format);
Loading history...
261
        
262
        if (isset($Calendar->tableHelper)) {
263
            $ordereddays = $Calendar->tableHelper->getDaysOfWeek();
264
        } else {
265
            //default: start from Sunday
266
            $firstDay = 0;
267
            //check if defined / set
268
            if (defined('CALENDAR_FIRST_DAY_OF_WEEK')) {
269
                $firstDay = CALENDAR_FIRST_DAY_OF_WEEK;
270
            } elseif(isset($Calendar->firstDay)) {
271
                $firstDay = $Calendar->firstDay;
272
            }
273
            $ordereddays = array();
274
            for ($i = $firstDay; $i < 7; $i++) {
275
                $ordereddays[] = $i;
276
            }
277
            for ($i = 0; $i < $firstDay; $i++) {
278
                $ordereddays[] = $i;
279
            }
280
        }
281
        
282
        $ordereddays = array_flip($ordereddays);
283
        $i = 0;
284
        $returndays = array();
285
        foreach ($ordereddays as $key => $value) {
286
            $returndays[$i] = $days[$key];
287
            $i++;
288
        }
289
        return $returndays;
290
    }
291
}
292