Passed
Push — master ( 5534ff...bbc089 )
by tsms
01:47
created

Textual   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 80
dl 0
loc 225
rs 10
c 0
b 0
f 0
wmc 23

9 Methods

Rating   Name   Duplication   Size   Complexity  
A prevMonthName() 0 4 1
A monthNames() 0 26 5
A thisMonthName() 0 4 1
A weekdayNames() 0 26 5
A nextMonthName() 0 4 1
A nextDayName() 0 6 1
A thisDayName() 0 6 1
A prevDayName() 0 6 1
B orderedWeekdays() 0 32 7
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
        $day = date('w', $stamp);
200
        return $days[$day];
201
    }
202
203
    /**
204
     * Returns textual representation of the day of week of the decorated calendar object
205
     * <b>Note:</b> Requires PEAR::Date
206
     *
207
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
208
     * @param string $format   (optional) format of returned months (one,two,short or long)
209
     *
210
     * @return string
211
     * @access public
212
     * @static
213
     */
214
    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...
215
    {
216
        $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

216
        /** @scrutinizer ignore-call */ 
217
        $days = Textual::weekdayNames($format);
Loading history...
217
        $stamp = $Calendar->thisDay('timestamp');
218
        $day = date('w', $stamp);
219
        return $days[$day];
220
    }
221
222
    /**
223
     * Returns textual representation of the next day of week of the decorated calendar object
224
     *
225
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
226
     * @param string $format   (optional) format of returned months (one,two,short or long)
227
     *
228
     * @return string
229
     * @access public
230
     * @static
231
     */
232
    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...
233
    {
234
        $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

234
        /** @scrutinizer ignore-call */ 
235
        $days = Textual::weekdayNames($format);
Loading history...
235
        $stamp = $Calendar->nextDay('timestamp');
236
        $day = date('w', $stamp);
237
        return $days[$day];
238
    }
239
240
    /**
241
     * Returns the days of the week using the order defined in the decorated
242
     * calendar object. Only useful for Calendar_Month_Weekdays, Calendar_Month_Weeks
243
     * and Calendar_Week. Otherwise the returned array will begin on Sunday
244
     *
245
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
246
     * @param string $format   (optional) format of returned months (one,two,short or long)
247
     *
248
     * @return array ordered array of week day names
249
     * @access public
250
     * @static
251
     */
252
    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...
253
    {
254
        $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

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