| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the PHPASN1 library. |
| 4 |
* |
| 5 |
* Copyright © Friedrich Große <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WP_DEFENDER_VENDOR\FG\ASN1; |
| 12 |
|
| 13 |
use DateInterval; |
| 14 |
use DateTime; |
| 15 |
use DateTimeZone; |
| 16 |
use Exception; |
| 17 |
|
| 18 |
abstract class AbstractTime extends ASNObject |
| 19 |
{ |
| 20 |
/** @var DateTime */ |
| 21 |
protected $value; |
| 22 |
|
| 23 |
public function __construct($dateTime = null, $dateTimeZone = 'UTC') |
| 24 |
{ |
| 25 |
if ($dateTime == null || is_string($dateTime)) { |
| 26 |
$timeZone = new DateTimeZone($dateTimeZone); |
| 27 |
$dateTimeObject = new DateTime($dateTime, $timeZone); |
| 28 |
if ($dateTimeObject == false) { |
| 29 |
$errorMessage = $this->getLastDateTimeErrors(); |
| 30 |
$className = Identifier::getName($this->getType()); |
| 31 |
throw new Exception(sprintf("Could not create %s from date time string '%s': %s", $className, $dateTime, $errorMessage)); |
| 32 |
} |
| 33 |
$dateTime = $dateTimeObject; |
| 34 |
} elseif (!$dateTime instanceof DateTime) { |
| 35 |
throw new Exception('Invalid first argument for some instance of AbstractTime constructor'); |
| 36 |
} |
| 37 |
|
| 38 |
$this->value = $dateTime; |
| 39 |
} |
| 40 |
|
| 41 |
public function getContent() |
| 42 |
{ |
| 43 |
return $this->value; |
| 44 |
} |
| 45 |
|
| 46 |
protected function getLastDateTimeErrors() |
| 47 |
{ |
| 48 |
$messages = ''; |
| 49 |
$lastErrors = DateTime::getLastErrors() ?: ['errors' => []]; |
| 50 |
foreach ($lastErrors['errors'] as $errorMessage) { |
| 51 |
$messages .= "{$errorMessage}, "; |
| 52 |
} |
| 53 |
|
| 54 |
return substr($messages, 0, -2); |
| 55 |
} |
| 56 |
|
| 57 |
public function __toString() |
| 58 |
{ |
| 59 |
return $this->value->format("Y-m-d\tH:i:s"); |
| 60 |
} |
| 61 |
|
| 62 |
protected static function extractTimeZoneData(&$binaryData, &$offsetIndex, DateTime $dateTime) |
| 63 |
{ |
| 64 |
$sign = $binaryData[$offsetIndex++]; |
| 65 |
$timeOffsetHours = intval(substr($binaryData, $offsetIndex, 2)); |
| 66 |
$timeOffsetMinutes = intval(substr($binaryData, $offsetIndex + 2, 2)); |
| 67 |
$offsetIndex += 4; |
| 68 |
|
| 69 |
$interval = new DateInterval("PT{$timeOffsetHours}H{$timeOffsetMinutes}M"); |
| 70 |
if ($sign == '+') { |
| 71 |
$dateTime->sub($interval); |
| 72 |
} else { |
| 73 |
$dateTime->add($interval); |
| 74 |
} |
| 75 |
|
| 76 |
return $dateTime; |
| 77 |
} |
| 78 |
} |
| 79 |
|