| 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\Util; |
| 32 |
|
| 33 |
use InvalidArgumentException; |
| 34 |
use function array_key_exists; |
| 35 |
use function in_array; |
| 36 |
use function is_array; |
| 37 |
use function strtolower; |
| 38 |
use function strtoupper; |
| 39 |
use function ucfirst; |
| 40 |
|
| 41 |
/** |
| 42 |
* iCalcreator utility/support class |
| 43 |
* |
| 44 |
* @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se> |
| 45 |
* @since 2.27.2 - 2018-12-21 |
| 46 |
*/ |
| 47 |
class Util |
| 48 |
{ |
| 49 |
/** |
| 50 |
* @var string misc. values |
| 51 |
* @static |
| 52 |
*/ |
| 53 |
public static $LCvalue = 'value'; |
| 54 |
public static $LCparams = 'params'; |
| 55 |
public static $ISLOCALTIME = 'ISLOCALTIME'; |
| 56 |
public static $CRLF = "\r\n"; |
| 57 |
public static $COMMA = ','; |
| 58 |
public static $COLON = ':'; |
| 59 |
public static $SEMIC = ';'; |
| 60 |
public static $MINUS = '-'; |
| 61 |
public static $PLUS = '+'; |
| 62 |
public static $SP0 = ''; |
| 63 |
public static $SP1 = ' '; |
| 64 |
public static $ZERO = '0'; |
| 65 |
public static $DOT = '.'; |
| 66 |
public static $SLASH = '/'; |
| 67 |
|
| 68 |
/** |
| 69 |
* Return bool true if compType is in array |
| 70 |
* |
| 71 |
* @param string $compType component name |
| 72 |
* @param array $compList list of components |
| 73 |
* @return bool |
| 74 |
* @static |
| 75 |
* @since 2.26 - 2018-11-03 |
| 76 |
*/ |
| 77 |
public static function isCompInList( $compType, array $compList ) |
| 78 |
{ |
| 79 |
if( empty( $compType )) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
return in_array( ucfirst( strtolower( $compType )), $compList); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Return bool true if property is in array |
| 87 |
* |
| 88 |
* @param string $propName property name |
| 89 |
* @param array $propList list of properties |
| 90 |
* @return bool |
| 91 |
* @static |
| 92 |
* @since 2.26 - 2018-11-04 |
| 93 |
*/ |
| 94 |
public static function isPropInList( $propName, array $propList ) |
| 95 |
{ |
| 96 |
return in_array( strtoupper( $propName ), $propList); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Return bool true if array key is isset and not empty |
| 101 |
* |
| 102 |
* @param mixed $array |
| 103 |
* @param string $key |
| 104 |
* @return bool |
| 105 |
* @static |
| 106 |
* @since 2.26.14 - 2019-01-28 |
| 107 |
*/ |
| 108 |
public static function issetAndNotEmpty( $array = null, $key = null) |
| 109 |
{ |
| 110 |
if( empty( $array ) || |
| 111 |
! is_array( $array ) || |
| 112 |
! array_key_exists( $key, $array )) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
return ( isset( $array[$key] ) && ! empty( $array[$key] )); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return bool true if array key is set and equals value |
| 120 |
* |
| 121 |
* @param mixed $base |
| 122 |
* @param string $key |
| 123 |
* @param string $value |
| 124 |
* @return bool |
| 125 |
* @static |
| 126 |
* @since 2.26.14 - 2019-03-01 |
| 127 |
*/ |
| 128 |
public static function issetKeyAndEquals( $base, $key, $value ) |
| 129 |
{ |
| 130 |
if( empty( $base ) || |
| 131 |
! is_array( $base ) || |
| 132 |
! array_key_exists( $key, $base )) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
return ( $value == $base[$key] ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Assert value is integer |
| 140 |
* |
| 141 |
* @param mixed $value |
| 142 |
* @param string $propName |
| 143 |
* @param int $rangeMin |
| 144 |
* @param int $rangeMax |
| 145 |
* @throws InvalidArgumentException |
| 146 |
* @static |
| 147 |
* @since 2.27.14 - 2019-02-19 |
| 148 |
*/ |
| 149 |
public static function assertInteger( |
| 150 |
$value, |
| 151 |
$propName, |
| 152 |
$rangeMin = null, |
| 153 |
$rangeMax = null |
| 154 |
) { |
| 155 |
static $ERR1 = '%s expects integer value, got %s'; |
| 156 |
static $ERR2 = '%s value %s not in range (%d-%d)'; |
| 157 |
if( ! is_scalar( $value ) || ! ctype_digit( (string) $value )) { |
| 158 |
throw new InvalidArgumentException( |
| 159 |
sprintf( $ERR1, $propName, var_export( $value, true )) |
| 160 |
); |
| 161 |
} |
| 162 |
if( ( ! is_null( $rangeMin ) && ( $rangeMin > $value )) || |
| 163 |
( ! is_null( $rangeMax )) && ( $rangeMax < $value )) { |
| 164 |
throw new InvalidArgumentException( |
| 165 |
sprintf( $ERR2, $propName, $value, $rangeMin, $rangeMax ) |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Assert value is string |
| 173 |
* |
| 174 |
* @param mixed $value |
| 175 |
* @param string $propName |
| 176 |
* @throws InvalidArgumentException |
| 177 |
* @static |
| 178 |
* @since 2.29.14 - 2019-09-03 |
| 179 |
*/ |
| 180 |
public static function assertString( $value, $propName ) |
| 181 |
{ |
| 182 |
static $ERR1 = '%s expects string value, got (%s) %s'; |
| 183 |
if( ! is_scalar( $value )) { |
| 184 |
throw new InvalidArgumentException( |
| 185 |
sprintf( |
| 186 |
$ERR1, |
| 187 |
$propName, |
| 188 |
gettype( $value ), |
| 189 |
var_export( $value, true ) |
| 190 |
) |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Assert value in enumeration |
| 197 |
* |
| 198 |
* @param mixed $value |
| 199 |
* @param array $enumeration - all upper case |
| 200 |
* @param string $propName |
| 201 |
* @throws InvalidArgumentException |
| 202 |
* @static |
| 203 |
* @since 2.27.2 - 2019-01-04 |
| 204 |
*/ |
| 205 |
public static function assertInEnumeration( |
| 206 |
$value, |
| 207 |
array $enumeration, |
| 208 |
$propName |
| 209 |
) { |
| 210 |
static $ERR = 'Invalid %s value : %s'; |
| 211 |
if( ! in_array( strtoupper( $value ), $enumeration )) { |
| 212 |
throw new InvalidArgumentException( sprintf( $ERR, $propName, var_export( $value, true ))); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|