| 1 |
<?php |
| 2 |
/** |
| 3 |
* iCalcreator, the PHP class package managing iCal (rfc2445/rfc5445) calendar information. |
| 4 |
* |
| 5 |
* copyright (c) 2007-2021 Kjell-Inge Gustafsson, kigkonsult, All rights reserved |
| 6 |
* Link https://kigkonsult.se |
| 7 |
* Package iCalcreator |
| 8 |
* Version 2.30.10 |
| 9 |
* License Subject matter of licence is the software iCalcreator. |
| 10 |
* The above copyright, link, package and version notices, |
| 11 |
* this licence notice and the invariant [rfc5545] PRODID result use |
| 12 |
* as implemented and invoked in iCalcreator shall be included in |
| 13 |
* all copies or substantial portions of the iCalcreator. |
| 14 |
* |
| 15 |
* iCalcreator is free software: you can redistribute it and/or modify |
| 16 |
* it under the terms of the GNU Lesser General Public License as published |
| 17 |
* by the Free Software Foundation, either version 3 of the License, |
| 18 |
* or (at your option) any later version. |
| 19 |
* |
| 20 |
* iCalcreator is distributed in the hope that it will be useful, |
| 21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
* GNU Lesser General Public License for more details. |
| 24 |
* |
| 25 |
* You should have received a copy of the GNU Lesser General Public License |
| 26 |
* along with iCalcreator. If not, see <https://www.gnu.org/licenses/>. |
| 27 |
* |
| 28 |
* This file is a part of iCalcreator. |
| 29 |
*/ |
| 30 |
|
| 31 |
namespace Kigkonsult\Icalcreator\Util; |
| 32 |
|
| 33 |
use DateTime; |
| 34 |
use DateTimeInterface; |
| 35 |
use DateTimeZone; |
| 36 |
use Exception; |
| 37 |
use Kigkonsult\Icalcreator\Vcalendar; |
| 38 |
use RuntimeException; |
| 39 |
|
| 40 |
use function explode; |
| 41 |
use function get_object_vars; |
| 42 |
use function is_array; |
| 43 |
use function is_object; |
| 44 |
use function sprintf; |
| 45 |
use function substr; |
| 46 |
|
| 47 |
/** |
| 48 |
* iCalcreator DateTime support class |
| 49 |
* |
| 50 |
* @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se> |
| 51 |
* @since 2.30.10 - 2021-12-01 |
| 52 |
*/ |
| 53 |
class UtilDateTime extends DateTime |
| 54 |
{ |
| 55 |
/** |
| 56 |
* @var string default object instance date[-time] 'key' |
| 57 |
*/ |
| 58 |
public $key = null; |
| 59 |
|
| 60 |
/** |
| 61 |
* @var array date[-time] origin |
| 62 |
*/ |
| 63 |
public $SCbools = []; |
| 64 |
|
| 65 |
/** |
| 66 |
* @var string default date[-time] format |
| 67 |
*/ |
| 68 |
public $dateFormat = null; |
| 69 |
|
| 70 |
/** |
| 71 |
* Constructor for UtilDateTime |
| 72 |
* |
| 73 |
* @param string $time |
| 74 |
* @param DateTimeZone $timezone |
| 75 |
* @throws Exception |
| 76 |
* @since 2.27.8 - 2019-01-12 |
| 77 |
*/ |
| 78 |
public function __construct( $time = "now" , DateTimeZone $timezone = null ) |
| 79 |
{ |
| 80 |
parent::__construct( $time, $timezone ); |
| 81 |
$this->dateFormat = DateTimeFactory::$YMDHISe; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @link https://php.net/manual/en/language.oop5.cloning.php#116329 |
| 86 |
*/ |
| 87 |
public function __clone() |
| 88 |
{ |
| 89 |
$object_vars = get_object_vars( $this ); |
| 90 |
|
| 91 |
foreach( $object_vars as $attr_name => $attr_value ) { |
| 92 |
if( is_object($this->$attr_name )) { |
| 93 |
$this->$attr_name = clone $this->$attr_name; |
| 94 |
} |
| 95 |
else if( is_array( $this->$attr_name )) { |
| 96 |
// Note: This copies only one dimension arrays |
| 97 |
foreach( $this->$attr_name as &$attr_array_value ) { |
| 98 |
if( is_object( $attr_array_value )) { |
| 99 |
$attr_array_value = clone $attr_array_value; |
| 100 |
} |
| 101 |
unset( $attr_array_value); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Return clone |
| 109 |
* |
| 110 |
* @return static |
| 111 |
* @since 2.26.2 - 2018-11-14 |
| 112 |
*/ |
| 113 |
public function getClone() |
| 114 |
{ |
| 115 |
return clone $this; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return time (His) array |
| 120 |
* |
| 121 |
* @return array |
| 122 |
* @since 2.23.20 - 2017-02-07 |
| 123 |
*/ |
| 124 |
public function getTime() |
| 125 |
{ |
| 126 |
static $H_I_S = 'H:i:s'; |
| 127 |
$res = []; |
| 128 |
foreach( explode( Util::$COLON, $this->format( $H_I_S )) as $t ) { |
| 129 |
$res[] = (int) $t; |
| 130 |
} |
| 131 |
return $res; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* set date and time from YmdHis string |
| 136 |
* |
| 137 |
* @param string $YmdHisString |
| 138 |
* @since 2.26.2 - 2018-11-14 |
| 139 |
*/ |
| 140 |
public function setDateTimeFromString( $YmdHisString ) |
| 141 |
{ |
| 142 |
$this->setDate( |
| 143 |
(int) substr( $YmdHisString, 0, 4 ), |
| 144 |
(int) substr( $YmdHisString, 4, 2 ), |
| 145 |
(int) substr( $YmdHisString, 6, 2 ) |
| 146 |
); |
| 147 |
$this->setTime( |
| 148 |
(int) substr( $YmdHisString, 8, 2 ), |
| 149 |
(int) substr( $YmdHisString, 10, 2 ), |
| 150 |
(int) substr( $YmdHisString, 12, 2 ) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Return the timezone name |
| 156 |
* |
| 157 |
* @return string |
| 158 |
* @since 2.21.7 - 2015-03-07 |
| 159 |
*/ |
| 160 |
public function getTimezoneName() |
| 161 |
{ |
| 162 |
$tz = $this->getTimezone(); |
| 163 |
return $tz->getName(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Return formatted date |
| 168 |
* |
| 169 |
* @param string $format |
| 170 |
* @return string |
| 171 |
* @since 2.21.7 - 2015-03-07 |
| 172 |
*/ |
| 173 |
public function format( $format = null ) |
| 174 |
{ |
| 175 |
if( empty( $format ) && isset( $this->dateFormat )) { |
| 176 |
$format = $this->dateFormat; |
| 177 |
} |
| 178 |
return parent::format( $format ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Return UtilDateTime object instance based on date array and timezone(s) |
| 183 |
* |
| 184 |
* @param DateTimeInterface $date |
| 185 |
* @param array $params |
| 186 |
* @param string $dtstartTz |
| 187 |
* @return static |
| 188 |
* @throws Exception |
| 189 |
* @throws RuntimeException |
| 190 |
* @static |
| 191 |
* @since 2.30.10 - 2021-12-01 |
| 192 |
*/ |
| 193 |
public static function factory( DateTimeInterface $date, $params = null, $dtstartTz = null ) |
| 194 |
{ |
| 195 |
static $Y_M_D = 'Y-m-d'; |
| 196 |
static $MSG1 = '#%d Can\'t create DateTimeZone from \'%s\''; |
| 197 |
static $MSG2 = '#%d Can\'t create (to-)DateTime : \'%s\''; |
| 198 |
static $MSG4 = '#%s Can\'t set DateTimeZone \'%s\''; |
| 199 |
|
| 200 |
$YmdHise = $date->format( DateTimeFactory::$YMDHISe ); |
| 201 |
try { |
| 202 |
$iCaldateTime = new UtilDateTime( $YmdHise ); |
| 203 |
} |
| 204 |
catch( Exception $e ) { |
| 205 |
throw new RuntimeException( sprintf( $MSG2, 3, $YmdHise ), null, $e ); // -- #1 |
| 206 |
} |
| 207 |
if( Vcalendar::Z == $dtstartTz ) { |
| 208 |
$dtstartTz = Vcalendar::UTC; |
| 209 |
} |
| 210 |
if( ! empty( $dtstartTz ) && |
| 211 |
( $dtstartTz != $iCaldateTime->getTimezone()->getName())) { |
| 212 |
// set the same timezone as dtstart |
| 213 |
if( $dtstartTz != $iCaldateTime->getTimezoneName()) { |
| 214 |
try { |
| 215 |
$timeZone = DateTimeZoneFactory::factory( $dtstartTz ); |
| 216 |
} |
| 217 |
catch( Exception $e ) { |
| 218 |
throw new RuntimeException( // -- #2 |
| 219 |
sprintf( $MSG1, 5, $dtstartTz ), |
| 220 |
null, |
| 221 |
$e |
| 222 |
); |
| 223 |
} |
| 224 |
if( false === $iCaldateTime->setTimezone( $timeZone )) { |
| 225 |
throw new RuntimeException( // -- #3 |
| 226 |
sprintf( $MSG4, 6, $dtstartTz ) |
| 227 |
); |
| 228 |
} |
| 229 |
} |
| 230 |
} // end if |
| 231 |
if( ParameterFactory::isParamsValueSet( |
| 232 |
[ Util::$LCparams => $params ], |
| 233 |
Vcalendar::DATE ) |
| 234 |
) { |
| 235 |
$iCaldateTime->dateFormat = $Y_M_D; |
| 236 |
$iCaldateTime->key = $iCaldateTime->format( DateTimeFactory::$Ymd ); |
| 237 |
} |
| 238 |
else { |
| 239 |
$iCaldateTime->key = $iCaldateTime->format( DateTimeFactory::$YmdHis ); |
| 240 |
} |
| 241 |
return $iCaldateTime; |
| 242 |
} |
| 243 |
} |
| 244 |
|