/usr/share/php/sabre21/Sabre/VObject/Property/VCard/DateAndOrTime.php is in php-sabre-vobject-3 3.5.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | <?php
namespace Sabre\VObject\Property\VCard;
use
Sabre\VObject\DateTimeParser,
Sabre\VObject\Property\Text,
Sabre\VObject\Property,
DateTime;
/**
* DateAndOrTime property
*
* This object encodes DATE-AND-OR-TIME values.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class DateAndOrTime extends Property {
/**
* Field separator
*
* @var null|string
*/
public $delimiter = null;
/**
* Returns the type of value.
*
* This corresponds to the VALUE= parameter. Every property also has a
* 'default' valueType.
*
* @return string
*/
public function getValueType() {
return "DATE-AND-OR-TIME";
}
/**
* Sets a multi-valued property.
*
* You may also specify DateTime objects here.
*
* @param array $parts
* @return void
*/
public function setParts(array $parts) {
if (count($parts)>1) {
throw new \InvalidArgumentException('Only one value allowed');
}
if (isset($parts[0]) && $parts[0] instanceof \DateTime) {
$this->setDateTime($parts[0]);
} else {
parent::setParts($parts);
}
}
/**
* Updates the current value.
*
* This may be either a single, or multiple strings in an array.
*
* Instead of strings, you may also use DateTime here.
*
* @param string|array|\DateTime $value
* @return void
*/
public function setValue($value) {
if ($value instanceof \DateTime) {
$this->setDateTime($value);
} else {
parent::setValue($value);
}
}
/**
* Sets the property as a DateTime object.
*
* @param \DateTime $dt
* @return void
*/
public function setDateTime(\DateTime $dt) {
$values = array();
$tz = null;
$isUtc = false;
$tz = $dt->getTimeZone();
$isUtc = in_array($tz->getName() , array('UTC', 'GMT', 'Z'));
if ($isUtc) {
$value = $dt->format('Ymd\\THis\\Z');
} else {
// Calculating the offset.
$value = $dt->format('Ymd\\THisO');
}
$this->value = $value;
}
/**
* Returns a date-time value.
*
* Note that if this property contained more than 1 date-time, only the
* first will be returned. To get an array with multiple values, call
* getDateTimes.
*
* If no time was specified, we will always use midnight (in the default
* timezone) as the time.
*
* If parts of the date were omitted, such as the year, we will grab the
* current values for those. So at the time of writing, if the year was
* omitted, we would have filled in 2014.
*
* @return \DateTime
*/
public function getDateTime() {
$dts = array();
$now = new DateTime();
$tzFormat = $now->getTimezone()->getOffset($now)===0?'\\Z':'O';
$nowParts = DateTimeParser::parseVCardDateTime($now->format('Ymd\\This' . $tzFormat));
$value = $this->getValue();
$dateParts = DateTimeParser::parseVCardDateTime($this->getValue());
// This sets all the missing parts to the current date/time.
// So if the year was missing for a birthday, we're making it 'this
// year'.
foreach($dateParts as $k=>$v) {
if (is_null($v)) {
$dateParts[$k] = $nowParts[$k];
}
}
return new DateTime("$dateParts[year]-$dateParts[month]-$dateParts[date] $dateParts[hour]:$dateParts[minute]:$dateParts[second] $dateParts[timezone]");
}
/**
* Returns the value, in the format it should be encoded for json.
*
* This method must always return an array.
*
* @return array
*/
public function getJsonValue() {
$parts = DateTimeParser::parseVCardDateTime($this->getValue());
$dateStr = '';
// Year
if (!is_null($parts['year'])) {
$dateStr.=$parts['year'];
if (!is_null($parts['month'])) {
// If a year and a month is set, we need to insert a separator
// dash.
$dateStr.='-';
}
} else {
if (!is_null($parts['month']) || !is_null($parts['date'])) {
// Inserting two dashes
$dateStr.='--';
}
}
// Month
if (!is_null($parts['month'])) {
$dateStr.=$parts['month'];
if (isset($parts['date'])) {
// If month and date are set, we need the separator dash.
$dateStr.='-';
}
} else {
if (isset($parts['date'])) {
// If the month is empty, and a date is set, we need a 'empty
// dash'
$dateStr.='-';
}
}
// Date
if (!is_null($parts['date'])) {
$dateStr.=$parts['date'];
}
// Early exit if we don't have a time string.
if (is_null($parts['hour']) && is_null($parts['minute']) && is_null($parts['second'])) {
return array($dateStr);
}
$dateStr.='T';
// Hour
if (!is_null($parts['hour'])) {
$dateStr.=$parts['hour'];
if (!is_null($parts['minute'])) {
$dateStr.=':';
}
} else {
// We know either minute or second _must_ be set, so we insert a
// dash for an empty value.
$dateStr.='-';
}
// Minute
if (!is_null($parts['minute'])) {
$dateStr.=$parts['minute'];
if (!is_null($parts['second'])) {
$dateStr.=':';
}
} else {
if (isset($parts['second'])) {
// Dash for empty minute
$dateStr.='-';
}
}
// Second
if (!is_null($parts['second'])) {
$dateStr.=$parts['second'];
}
// Timezone
if (!is_null($parts['timezone'])) {
$dateStr.=$parts['timezone'];
}
return array($dateStr);
}
/**
* Sets a raw value coming from a mimedir (iCalendar/vCard) file.
*
* This has been 'unfolded', so only 1 line will be passed. Unescaping is
* not yet done, but parameters are not included.
*
* @param string $val
* @return void
*/
public function setRawMimeDirValue($val) {
$this->setValue($val);
}
/**
* Returns a raw mime-dir representation of the value.
*
* @return string
*/
public function getRawMimeDirValue() {
return implode($this->delimiter, $this->getParts());
}
/**
* Validates the node for correctness.
*
* The following options are supported:
* Node::REPAIR - May attempt to automatically repair the problem.
*
* This method returns an array with detected problems.
* Every element has the following properties:
*
* * level - problem level.
* * message - A human-readable string describing the issue.
* * node - A reference to the problematic node.
*
* The level means:
* 1 - The issue was repaired (only happens if REPAIR was turned on)
* 2 - An inconsequential issue
* 3 - A severe issue.
*
* @param int $options
* @return array
*/
public function validate($options = 0) {
$messages = parent::validate($options);
$value = $this->getValue();
try {
DateTimeParser::parseVCardDateTime($value);
} catch (\InvalidArgumentException $e) {
$messages[] = array(
'level' => 3,
'message' => 'The supplied value (' . $value . ') is not a correct DATE-AND-OR-TIME property',
'node' => $this,
);
}
return $messages;
}
}
|