| 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 |
| 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\Traits; |
| 32 |
|
| 33 |
use DateTimeInterface; |
| 34 |
use Exception; |
| 35 |
use InvalidArgumentException; |
| 36 |
use Kigkonsult\Icalcreator\Util\DateIntervalFactory; |
| 37 |
use Kigkonsult\Icalcreator\Util\DateTimeFactory; |
| 38 |
use Kigkonsult\Icalcreator\Util\ParameterFactory; |
| 39 |
use Kigkonsult\Icalcreator\Util\RexdateFactory; |
| 40 |
use Kigkonsult\Icalcreator\Util\Util; |
| 41 |
use Kigkonsult\Icalcreator\Vcalendar; |
| 42 |
|
| 43 |
use function count; |
| 44 |
use function is_array; |
| 45 |
use function reset; |
| 46 |
|
| 47 |
/** |
| 48 |
* RDATE property functions |
| 49 |
* |
| 50 |
* @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se> |
| 51 |
* @since 2.29.2 2019-06-23 |
| 52 |
*/ |
| 53 |
trait RDATEtrait |
| 54 |
{ |
| 55 |
/** |
| 56 |
* @var array component property RDATE value |
| 57 |
*/ |
| 58 |
protected $rdate = null; |
| 59 |
|
| 60 |
/** |
| 61 |
* Return formatted output for calendar component property rdate |
| 62 |
* |
| 63 |
* @return string |
| 64 |
* @throws Exception |
| 65 |
*/ |
| 66 |
public function createRdate() |
| 67 |
{ |
| 68 |
if( empty( $this->rdate )) { |
| 69 |
return null; |
| 70 |
} |
| 71 |
try { |
| 72 |
$res = RexdateFactory::formatRdate( |
| 73 |
$this->rdate, |
| 74 |
$this->getConfig( self::ALLOWEMPTY ), |
| 75 |
$this->getCompType() |
| 76 |
); |
| 77 |
} |
| 78 |
catch( Exception $e ) { |
| 79 |
throw $e; |
| 80 |
} |
| 81 |
return $res; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Delete calendar component property rdate |
| 86 |
* |
| 87 |
* @param int $propDelIx specific property in case of multiply occurrence |
| 88 |
* @return bool |
| 89 |
* @since 2.27.1 - 2018-12-15 |
| 90 |
*/ |
| 91 |
public function deleteRdate( $propDelIx = null ) |
| 92 |
{ |
| 93 |
if( empty( $this->rdate )) { |
| 94 |
unset( $this->propDelIx[self::RDATE] ); |
| 95 |
return false; |
| 96 |
} |
| 97 |
return $this->deletePropertyM( $this->rdate, self::RDATE, $propDelIx ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get calendar component property rdate |
| 102 |
* |
| 103 |
* @param int $propIx specific property in case of multiply occurrence |
| 104 |
* @param bool $inclParam |
| 105 |
* @return bool|array |
| 106 |
* @throws Exception |
| 107 |
* @since 2.29.2 2019-06-23 |
| 108 |
*/ |
| 109 |
public function getRdate( $propIx = null, $inclParam = false ) |
| 110 |
{ |
| 111 |
if( empty( $this->rdate )) { |
| 112 |
unset( $this->propIx[self::RDATE] ); |
| 113 |
return false; |
| 114 |
} |
| 115 |
$output = $this->getPropertyM( $this->rdate, self::RDATE, $propIx, $inclParam ); |
| 116 |
if( empty( $output )) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
if( empty( $output[Util::$LCvalue] )) { |
| 120 |
return $output; |
| 121 |
} |
| 122 |
if( isset( $output[Util::$LCvalue] )) { |
| 123 |
foreach( $output[Util::$LCvalue] as $rIx => $rdatePart ) { |
| 124 |
if( is_array( $rdatePart ) && isset( $rdatePart[1] ) && |
| 125 |
DateIntervalFactory::isDateIntervalArrayInvertSet( $rdatePart[1] )) { |
| 126 |
try {// fix pre 7.0.5 bug |
| 127 |
$output[Util::$LCvalue][$rIx][1] = |
| 128 |
DateIntervalFactory::DateIntervalArr2DateInterval( $rdatePart[1] ); |
| 129 |
} |
| 130 |
catch( Exception $e ) { |
| 131 |
throw $e; |
| 132 |
} |
| 133 |
} |
| 134 |
} // end foreach |
| 135 |
} // end if |
| 136 |
else { |
| 137 |
foreach( $output as $rIx => $rdatePart ) { |
| 138 |
if( is_array( $rdatePart ) && isset( $rdatePart[1] ) && |
| 139 |
DateIntervalFactory::isDateIntervalArrayInvertSet( $rdatePart[1] )) { |
| 140 |
try { // fix pre 7.0.5 bug |
| 141 |
$output[$rIx][1] = |
| 142 |
DateIntervalFactory::DateIntervalArr2DateInterval( $rdatePart[1] ); |
| 143 |
} |
| 144 |
catch( Exception $e ) { |
| 145 |
throw $e; |
| 146 |
} |
| 147 |
} |
| 148 |
} // end foreach |
| 149 |
} // end else |
| 150 |
return $output; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Set calendar component property rdate |
| 155 |
* |
| 156 |
* @param array $value |
| 157 |
* @param array $params |
| 158 |
* @param integer $index |
| 159 |
* @return static |
| 160 |
* @throws Exception |
| 161 |
* @throws InvalidArgumentException |
| 162 |
* @since 2.29.2 2019-06-23 |
| 163 |
*/ |
| 164 |
public function setRdate( $value = null, $params = [], $index = null ) |
| 165 |
{ |
| 166 |
if( empty( $value ) || |
| 167 |
( is_array( $value) && ( 1 == count( $value )) && empty( reset( $value ))) |
| 168 |
) { |
| 169 |
$this->assertEmptyValue( $value, self::RDATE ); |
| 170 |
$this->setMval( $this->rdate, Util::$SP0, [], null, $index ); |
| 171 |
return $this; |
| 172 |
} |
| 173 |
$value = self::checkSingleRdates( |
| 174 |
$value, |
| 175 |
ParameterFactory::isParamsValueSet( |
| 176 |
[ Util::$LCparams => $params ], |
| 177 |
self::PERIOD |
| 178 |
) |
| 179 |
); |
| 180 |
if( Util::isCompInList( $this->getCompType(), Vcalendar::$TZCOMPS )) { |
| 181 |
$params[Util::$ISLOCALTIME] = true; |
| 182 |
} |
| 183 |
try { |
| 184 |
$input = RexdateFactory::prepInputRdate( $value, $params ); |
| 185 |
} |
| 186 |
catch( Exception $e ) { |
| 187 |
throw $e; |
| 188 |
} |
| 189 |
$this->setMval( |
| 190 |
$this->rdate, |
| 191 |
$input[Util::$LCvalue], |
| 192 |
$input[Util::$LCparams], |
| 193 |
null, |
| 194 |
$index |
| 195 |
); |
| 196 |
return $this; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Return Rdates is single input |
| 201 |
* |
| 202 |
* @param array $rDates |
| 203 |
* @param bool $isPeriod |
| 204 |
* @return array |
| 205 |
* @throws Exception |
| 206 |
* @throws InvalidArgumentException |
| 207 |
* @static |
| 208 |
* @since 2.29.16 2020-01-24 |
| 209 |
*/ |
| 210 |
private static function checkSingleRdates( $rDates, $isPeriod ) |
| 211 |
{ |
| 212 |
if( $rDates instanceof DateTimeInterface ) { |
| 213 |
return [ DateTimeFactory::cnvrtDateTimeInterface( $rDates ) ]; |
| 214 |
} |
| 215 |
if( DateTimeFactory::isStringAndDate( $rDates )) { |
| 216 |
return [ $rDates ]; |
| 217 |
} |
| 218 |
if( $isPeriod && is_array( $rDates ) && ( 2 == count( $rDates ))) { |
| 219 |
$first = reset( $rDates ); |
| 220 |
if( $first instanceof DateTimeInterface ) { |
| 221 |
return [ $rDates ]; |
| 222 |
} |
| 223 |
if( DateTimeFactory::isStringAndDate( $first )) { |
| 224 |
return [ $rDates ]; |
| 225 |
} |
| 226 |
} |
| 227 |
return $rDates; |
| 228 |
} |
| 229 |
} |
| 230 |
|