FriendsOfPear /
Calendar
| 1 | <?php |
||
| 2 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |
||
| 3 | |||
| 4 | /** |
||
| 5 | * Contains the Calendar_Util_Uri 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 | * 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&month=10 |
||
| 47 | * echo $Uri->prev($Day,'day'); // Displays year=2003&month=10&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 | * @package Calendar |
||
| 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 | * @link http://pear.php.net/package/Calendar |
||
| 61 | * @access public |
||
| 62 | */ |
||
| 63 | class Uri |
||
| 64 | { |
||
| 65 | /** |
||
| 66 | * Uri fragments for year, month, day etc. |
||
| 67 | * @var array |
||
| 68 | * @access private |
||
| 69 | */ |
||
| 70 | var $uris = array(); |
||
| 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 | * @var string (default XHTML &) |
||
| 77 | * @access public |
||
| 78 | */ |
||
| 79 | var $separator = '&'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * To output a "scalar" string - variable names omitted. |
||
| 83 | * Used for urls like index.php/2004/8/12 |
||
| 84 | * @var boolean (default false) |
||
| 85 | * @access public |
||
| 86 | */ |
||
| 87 | var $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 | * @access public |
||
| 101 | */ |
||
| 102 | function __construct($y, $m=null, $d=null, $h=null, $i=null, $s=null) |
||
|
0 ignored issues
–
show
|
|||
| 103 | { |
||
| 104 | $this->setFragments($y, $m, $d, $h, $i, $s); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Sets the URI fragment names |
||
| 109 | * |
||
| 110 | * @param string $y URI fragment for year |
||
| 111 | * @param string $m (optional) URI fragment for month |
||
| 112 | * @param string $d (optional) URI fragment for day |
||
| 113 | * @param string $h (optional) URI fragment for hour |
||
| 114 | * @param string $i (optional) URI fragment for minute |
||
| 115 | * @param string $s (optional) URI fragment for second |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | * @access public |
||
| 119 | */ |
||
| 120 | function setFragments($y, $m=null, $d=null, $h=null, $i=null, $s=null) |
||
|
0 ignored issues
–
show
|
|||
| 121 | { |
||
| 122 | if (!is_null($y)) $this->uris['Year'] = $y; |
||
|
0 ignored issues
–
show
|
|||
| 123 | if (!is_null($m)) $this->uris['Month'] = $m; |
||
| 124 | if (!is_null($d)) $this->uris['Day'] = $d; |
||
| 125 | if (!is_null($h)) $this->uris['Hour'] = $h; |
||
| 126 | if (!is_null($i)) $this->uris['Minute'] = $i; |
||
| 127 | if (!is_null($s)) $this->uris['Second'] = $s; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Gets the URI string for the previous calendar unit |
||
| 132 | * |
||
| 133 | * @param object $Calendar subclassed from Calendar e.g. Calendar_Month |
||
| 134 | * @param string $unit calendar unit (year|month|week|day|hour|minute|second) |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | * @access public |
||
| 138 | */ |
||
| 139 | function prev($Calendar, $unit) |
||
|
0 ignored issues
–
show
|
|||
| 140 | { |
||
| 141 | $method = 'prev'.$unit; |
||
| 142 | $stamp = $Calendar->{$method}('timestamp'); |
||
| 143 | return $this->buildUriString($Calendar, $method, $stamp); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Gets the URI string for the current calendar unit |
||
| 148 | * |
||
| 149 | * @param object $Calendar subclassed from Calendar e.g. Calendar_Month |
||
| 150 | * @param string $unit calendar unit (year|month|week|day|hour|minute|second) |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | * @access public |
||
| 154 | */ |
||
| 155 | function this($Calendar, $unit) |
||
|
0 ignored issues
–
show
|
|||
| 156 | { |
||
| 157 | $method = 'this'.$unit; |
||
| 158 | $stamp = $Calendar->{$method}('timestamp'); |
||
| 159 | return $this->buildUriString($Calendar, $method, $stamp); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Gets the URI string for the next calendar unit |
||
| 164 | * |
||
| 165 | * @param object $Calendar subclassed from Calendar e.g. Calendar_Month |
||
| 166 | * @param string $unit calendar unit (year|month|week|day|hour|minute|second) |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | * @access public |
||
| 170 | */ |
||
| 171 | function next($Calendar, $unit) |
||
|
0 ignored issues
–
show
|
|||
| 172 | { |
||
| 173 | $method = 'next'.$unit; |
||
| 174 | $stamp = $Calendar->{$method}('timestamp'); |
||
| 175 | return $this->buildUriString($Calendar, $method, $stamp); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Build the URI string |
||
| 180 | * |
||
| 181 | * @param object $Calendar subclassed from Calendar e.g. Calendar_Month |
||
| 182 | * @param string $method method substring |
||
| 183 | * @param int $stamp timestamp |
||
| 184 | * |
||
| 185 | * @return string build uri string |
||
| 186 | * @access private |
||
| 187 | */ |
||
| 188 | function buildUriString($Calendar, $method, $stamp) |
||
|
0 ignored issues
–
show
|
|||
| 189 | { |
||
| 190 | $uriString = ''; |
||
| 191 | $cE = & $Calendar->getEngine(); |
||
| 192 | $separator = ''; |
||
| 193 | foreach ($this->uris as $unit => $uri) { |
||
| 194 | $call = 'stampTo'.$unit; |
||
| 195 | $uriString .= $separator; |
||
| 196 | if (!$this->scalar) { |
||
| 197 | $uriString .= $uri.'='; |
||
| 198 | } |
||
| 199 | $uriString .= $cE->{$call}($stamp); |
||
| 200 | $separator = $this->separator; |
||
| 201 | } |
||
| 202 | return $uriString; |
||
| 203 | } |
||
| 204 | } |
||
| 205 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.