Calendar_Util_Textual::weekdayNames()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 27
rs 9.2888
c 0
b 0
f 0
cc 5
nc 8
nop 1
1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4: */
4
5
/**
6
 * Contains the Calendar_Util_Textual 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
 * @author    Lorenzo Alberton <[email protected]>
35
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
36
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
37
 *
38
 * @link      http://pear.php.net/package/Calendar
39
 */
40
41
/**
42
 * Allows Calendar include path to be redefined.
43
 *
44
 * @ignore
45
 */
46
if (!defined('CALENDAR_ROOT')) {
47
    define('CALENDAR_ROOT', 'Calendar/');
48
}
49
50
/**
51
 * Load Calendar decorator base class.
52
 */
53
require_once CALENDAR_ROOT . 'Decorator.php';
54
55
/**
56
 * Static utlities to help with fetching textual representations of months and
57
 * days of the week.
58
 *
59
 * @category  Date and Time
60
 *
61
 * @author    Harry Fuecks <[email protected]>
62
 * @author    Lorenzo Alberton <[email protected]>
63
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
64
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
65
 *
66
 * @link      http://pear.php.net/package/Calendar
67
 */
68
class Calendar_Util_Textual
69
{
70
    /**
71
     * Returns an array of 12 month names (first index = 1).
72
     *
73
     * @param string $format (optional) format of returned months (one|two|short|long)
74
     *
75
     * @return array
76
     * @static
77
     */
78
    public static function monthNames($format = 'long')
79
    {
80
        $formats = [
81
            'one'   => '%b',
82
            'two'   => '%b',
83
            'short' => '%b',
84
            'long'  => '%B',
85
        ];
86
        if (!array_key_exists($format, $formats)) {
87
            $format = 'long';
88
        }
89
        $months = [];
90
        for ($i = 1; $i <= 12; ++$i) {
91
            $stamp = mktime(0, 0, 0, $i, 1, 2003);
92
            $month = strftime($formats[$format], $stamp);
93
            switch ($format) {
94
                case 'one':
95
                    $month = mb_substr($month, 0, 1);
96
                    break;
97
                case 'two':
98
                    $month = mb_substr($month, 0, 2);
99
                    break;
100
            }
101
            $months[$i] = $month;
102
        }
103
104
        return $months;
105
    }
106
107
    /**
108
     * Returns an array of 7 week day names (first index = 0).
109
     *
110
     * @param string $format (optional) format of returned days (one,two,short or long)
111
     *
112
     * @return array
113
     * @static
114
     */
115
    public static function weekdayNames($format = 'long')
116
    {
117
        $formats = [
118
            'one'   => '%a',
119
            'two'   => '%a',
120
            'short' => '%a',
121
            'long'  => '%A',
122
        ];
123
        if (!array_key_exists($format, $formats)) {
124
            $format = 'long';
125
        }
126
        $days = [];
127
        for ($i = 0; $i <= 6; ++$i) {
128
            $stamp = mktime(0, 0, 0, 11, $i + 2, 2003);
129
            $day   = strftime($formats[$format], $stamp);
130
            switch ($format) {
131
                case 'one':
132
                    $day = mb_substr($day, 0, 1);
133
                    break;
134
                case 'two':
135
                    $day = mb_substr($day, 0, 2);
136
                    break;
137
            }
138
            $days[$i] = $day;
139
        }
140
141
        return $days;
142
    }
143
144
    /**
145
     * Returns textual representation of the previous month of the decorated calendar object.
146
     *
147
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
148
     * @param string $format   (optional) format of returned months (one,two,short or long)
149
     *
150
     * @return string
151
     * @static
152
     */
153
    public static function prevMonthName($Calendar, $format = 'long')
154
    {
155
        $months = self::monthNames($format);
156
157
        return $months[$Calendar->prevMonth()];
158
    }
159
160
    /**
161
     * Returns textual representation of the month of the decorated calendar object.
162
     *
163
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
164
     * @param string $format   (optional) format of returned months (one,two,short or long)
165
     *
166
     * @return string
167
     * @static
168
     */
169
    public static function thisMonthName($Calendar, $format = 'long')
170
    {
171
        $months = self::monthNames($format);
172
173
        return $months[$Calendar->thisMonth()];
174
    }
175
176
    /**
177
     * Returns textual representation of the next month of the decorated calendar object.
178
     *
179
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
180
     * @param string $format   (optional) format of returned months (one,two,short or long)
181
     *
182
     * @return string
183
     * @static
184
     */
185
    public static function nextMonthName($Calendar, $format = 'long')
186
    {
187
        $months = self::monthNames($format);
188
189
        return $months[$Calendar->nextMonth()];
190
    }
191
192
    /**
193
     * Returns textual representation of the previous day of week of the decorated calendar object
194
     * <b>Note:</b> Requires PEAR::Date.
195
     *
196
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
197
     * @param string $format   (optional) format of returned months (one,two,short or long)
198
     *
199
     * @return string
200
     * @static
201
     */
202
    public static function prevDayName($Calendar, $format = 'long')
203
    {
204
        $days  = self::weekdayNames($format);
205
        $stamp = $Calendar->prevDay('timestamp');
206
        $cE    = $Calendar->getEngine();
207
        require_once __DIR__ . '/Date/Calc.php';
208
        $day = Date_Calc::dayOfWeek($cE->stampToDay($stamp), $cE->stampToMonth($stamp), $cE->stampToYear($stamp));
0 ignored issues
show
Bug introduced by
The type 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...
209
210
        return $days[$day];
211
    }
212
213
    /**
214
     * Returns textual representation of the day of week of the decorated calendar object
215
     * <b>Note:</b> Requires PEAR::Date.
216
     *
217
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
218
     * @param string $format   (optional) format of returned months (one,two,short or long)
219
     *
220
     * @return string
221
     * @static
222
     */
223
    public static function thisDayName($Calendar, $format = 'long')
224
    {
225
        $days = self::weekdayNames($format);
226
        require_once __DIR__ . '/Date/Calc.php';
227
        $day = Date_Calc::dayOfWeek($Calendar->thisDay(), $Calendar->thisMonth(), $Calendar->thisYear());
228
229
        return $days[$day];
230
    }
231
232
    /**
233
     * Returns textual representation of the next day of week of the decorated calendar object.
234
     *
235
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
236
     * @param string $format   (optional) format of returned months (one,two,short or long)
237
     *
238
     * @return string
239
     * @static
240
     */
241
    public static function nextDayName($Calendar, $format = 'long')
242
    {
243
        $days  = self::weekdayNames($format);
244
        $stamp = $Calendar->nextDay('timestamp');
245
        $cE    = $Calendar->getEngine();
246
        require_once __DIR__ . '/Date/Calc.php';
247
        $day = Date_Calc::dayOfWeek($cE->stampToDay($stamp), $cE->stampToMonth($stamp), $cE->stampToYear($stamp));
248
249
        return $days[$day];
250
    }
251
252
    /**
253
     * Returns the days of the week using the order defined in the decorated
254
     * calendar object. Only useful for Calendar_Month_Weekdays, Calendar_Month_Weeks
255
     * and Calendar_Week. Otherwise the returned array will begin on Sunday.
256
     *
257
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
258
     * @param string $format   (optional) format of returned months (one,two,short or long)
259
     *
260
     * @return array ordered array of week day names
261
     * @static
262
     */
263
    public static function orderedWeekdays($Calendar, $format = 'long')
264
    {
265
        $days = self::weekdayNames($format);
266
267
        if (isset($Calendar->tableHelper)) {
268
            $ordereddays = $Calendar->tableHelper->getDaysOfWeek();
269
        } else {
270
            //default: start from Sunday
271
            $firstDay = 0;
272
            //check if defined / set
273
            if (defined('CALENDAR_FIRST_DAY_OF_WEEK')) {
274
                $firstDay = CALENDAR_FIRST_DAY_OF_WEEK;
275
            } elseif (isset($Calendar->firstDay)) {
276
                $firstDay = $Calendar->firstDay;
277
            }
278
            $ordereddays = [];
279
            for ($i = $firstDay; $i < 7; ++$i) {
280
                $ordereddays[] = $i;
281
            }
282
            for ($i = 0; $i < $firstDay; ++$i) {
283
                $ordereddays[] = $i;
284
            }
285
        }
286
287
        $ordereddays = array_flip($ordereddays);
288
        $i           = 0;
289
        $returndays  = [];
290
        foreach ($ordereddays as $key => $value) {
291
            $returndays[$i] = $days[$key];
292
            ++$i;
293
        }
294
295
        return $returndays;
296
    }
297
}
298