Calendar_Util_Uri::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 6
1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4: */
4
5
/**
6
 * Contains the Calendar_Util_Uri 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
 * Utility to help building HTML links for navigating the calendar<br>
43
 * <code>
44
 * $Day = new Calendar_Day(2003, 10, 23);
45
 * $Uri = new Calendar_Util_Uri('year', 'month', 'day');
46
 * echo $Uri->prev($Day,'month'); // Displays year=2003&amp;month=10
47
 * echo $Uri->prev($Day,'day'); // Displays year=2003&amp;month=10&amp;day=22
48
 * $Uri->seperator = '/';
49
 * $Uri->scalar = true;
50
 * echo $Uri->prev($Day,'month'); // Displays 2003/10
51
 * echo $Uri->prev($Day,'day'); // Displays 2003/10/22
52
 * </code>.
53
 *
54
 * @category  Date and Time
55
 *
56
 * @author    Harry Fuecks <[email protected]>
57
 * @author    Lorenzo Alberton <[email protected]>
58
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
59
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
60
 *
61
 * @link      http://pear.php.net/package/Calendar
62
 */
63
class Calendar_Util_Uri
64
{
65
    /**
66
     * Uri fragments for year, month, day etc.
67
     *
68
     * @var array
69
     */
70
    public $uris = [];
71
72
    /**
73
     * String to separate fragments with.
74
     * Set to just & for HTML.
75
     * For a scalar URL you might use / as the seperator.
76
     *
77
     * @var string (default XHTML &amp;)
78
     */
79
    public $separator = '&amp;';
80
81
    /**
82
     * To output a "scalar" string - variable names omitted.
83
     * Used for urls like index.php/2004/8/12.
84
     *
85
     * @var bool (default false)
86
     */
87
    public $scalar = false;
88
89
    /**
90
     * Constructs Calendar_Decorator_Uri
91
     * The term "fragment" means <i>name</i> of a calendar GET variables in the URL.
92
     *
93
     * @param string $y URI fragment for year
94
     * @param string $m (optional) URI fragment for month
95
     * @param string $d (optional) URI fragment for day
96
     * @param string $h (optional) URI fragment for hour
97
     * @param string $i (optional) URI fragment for minute
98
     * @param string $s (optional) URI fragment for second
99
     */
100
    public function __construct($y, $m = null, $d = null, $h = null, $i = null, $s = null)
101
    {
102
        $this->setFragments($y, $m, $d, $h, $i, $s);
103
    }
104
105
    /**
106
     * Sets the URI fragment names.
107
     *
108
     * @param string $y URI fragment for year
109
     * @param string $m (optional) URI fragment for month
110
     * @param string $d (optional) URI fragment for day
111
     * @param string $h (optional) URI fragment for hour
112
     * @param string $i (optional) URI fragment for minute
113
     * @param string $s (optional) URI fragment for second
114
     */
115
    public function setFragments($y, $m = null, $d = null, $h = null, $i = null, $s = null)
116
    {
117
        if (null !== $y) {
0 ignored issues
show
introduced by
The condition null !== $y is always true.
Loading history...
118
            $this->uris['Year'] = $y;
119
        }
120
        if (null !== $m) {
121
            $this->uris['Month'] = $m;
122
        }
123
        if (null !== $d) {
124
            $this->uris['Day'] = $d;
125
        }
126
        if (null !== $h) {
127
            $this->uris['Hour'] = $h;
128
        }
129
        if (null !== $i) {
130
            $this->uris['Minute'] = $i;
131
        }
132
        if (null !== $s) {
133
            $this->uris['Second'] = $s;
134
        }
135
    }
136
137
    /**
138
     * Gets the URI string for the previous calendar unit.
139
     *
140
     * @param object $Calendar subclassed from Calendar e.g. Calendar_Month
141
     * @param string $unit     calendar  unit (year|month|week|day|hour|minute|second)
142
     *
143
     * @return string
144
     */
145
    public function prev($Calendar, $unit)
146
    {
147
        $method = 'prev' . $unit;
148
        $stamp  = $Calendar->{$method}('timestamp');
149
150
        return $this->buildUriString($Calendar, $method, $stamp);
151
    }
152
153
    /**
154
     * Gets the URI string for the current calendar unit.
155
     *
156
     * @param object $Calendar subclassed from Calendar e.g. Calendar_Month
157
     * @param string $unit     calendar  unit (year|month|week|day|hour|minute|second)
158
     *
159
     * @return string
160
     */
161
    public function this($Calendar, $unit)
162
    {
163
        $method = 'this' . $unit;
164
        $stamp  = $Calendar->{$method}('timestamp');
165
166
        return $this->buildUriString($Calendar, $method, $stamp);
167
    }
168
169
    /**
170
     * Gets the URI string for the next calendar unit.
171
     *
172
     * @param object $Calendar subclassed from Calendar e.g. Calendar_Month
173
     * @param string $unit     calendar unit (year|month|week|day|hour|minute|second)
174
     *
175
     * @return string
176
     */
177
    public function next($Calendar, $unit)
178
    {
179
        $method = 'next' . $unit;
180
        $stamp  = $Calendar->{$method}('timestamp');
181
182
        return $this->buildUriString($Calendar, $method, $stamp);
183
    }
184
185
    /**
186
     * Build the URI string.
187
     *
188
     * @param object $Calendar subclassed from Calendar e.g. Calendar_Month
189
     * @param string $method   method substring
190
     * @param int    $stamp    timestamp
191
     *
192
     * @return string build uri string
193
     */
194
    public function buildUriString($Calendar, $method, $stamp)
195
    {
196
        $uriString = '';
197
        $cE        = $Calendar->getEngine();
198
        $separator = '';
199
        foreach ($this->uris as $unit => $uri) {
200
            $call      = 'stampTo' . $unit;
201
            $uriString .= $separator;
202
            if (!$this->scalar) {
203
                $uriString .= $uri . '=';
204
            }
205
            $uriString .= $cE->{$call}($stamp);
206
            $separator = $this->separator;
207
        }
208
209
        return $uriString;
210
    }
211
}
212