| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @file |
| 5 |
* This file is part of the PdfParser library. |
| 6 |
* |
| 7 |
* @author Sébastien MALOT <sebastien@malot.fr> |
| 8 |
* |
| 9 |
* @date 2017-01-03 |
| 10 |
* |
| 11 |
* @license LGPLv3 |
| 12 |
* |
| 13 |
* @url <https://github.com/smalot/pdfparser> |
| 14 |
* |
| 15 |
* PdfParser is a pdf library written in PHPi, extraction oriented. |
| 16 |
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr> |
| 17 |
* |
| 18 |
* This program is free software: you can redistribute it and/or modify |
| 19 |
* it under the terms of the GNU Lesser General Public License as published by |
| 20 |
* the Free Software Foundation, either version 3 of the License, or |
| 21 |
* (at your option) any later version. |
| 22 |
* |
| 23 |
* This program is distributed in the hope that it will be useful, |
| 24 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 |
* GNU Lesser General Public License for more details. |
| 27 |
* |
| 28 |
* You should have received a copy of the GNU Lesser General Public License |
| 29 |
* along with this program. |
| 30 |
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
| 31 |
*/ |
| 32 |
|
| 33 |
namespace Smalot\PdfParser\Element; |
| 34 |
|
| 35 |
use Smalot\PdfParser\Document; |
| 36 |
|
| 37 |
/** |
| 38 |
* Class ElementDate |
| 39 |
*/ |
| 40 |
class ElementDate extends ElementString |
| 41 |
{ |
| 42 |
/** |
| 43 |
* @var array<int,string> |
| 44 |
*/ |
| 45 |
protected static $formats = [ |
| 46 |
4 => 'Y', |
| 47 |
6 => 'Ym', |
| 48 |
8 => 'Ymd', |
| 49 |
10 => 'YmdH', |
| 50 |
12 => 'YmdHi', |
| 51 |
14 => 'YmdHis', |
| 52 |
15 => 'YmdHise', |
| 53 |
17 => 'YmdHisO', |
| 54 |
18 => 'YmdHisO', |
| 55 |
19 => 'YmdHisO', |
| 56 |
]; |
| 57 |
|
| 58 |
/** |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
protected $format = 'c'; |
| 62 |
|
| 63 |
/** |
| 64 |
* @var \DateTime |
| 65 |
*/ |
| 66 |
protected $value; |
| 67 |
|
| 68 |
public function __construct($value) |
| 69 |
{ |
| 70 |
if (!($value instanceof \DateTime)) { |
| 71 |
throw new \Exception('DateTime required.'); // FIXME: Sometimes strings are passed to this function |
| 72 |
} |
| 73 |
|
| 74 |
parent::__construct($value); |
| 75 |
} |
| 76 |
|
| 77 |
public function setFormat(string $format) |
| 78 |
{ |
| 79 |
$this->format = $format; |
| 80 |
} |
| 81 |
|
| 82 |
public function equals($value): bool |
| 83 |
{ |
| 84 |
if ($value instanceof \DateTime) { |
| 85 |
$timestamp = $value->getTimeStamp(); |
| 86 |
} else { |
| 87 |
$timestamp = strtotime($value); |
| 88 |
} |
| 89 |
|
| 90 |
return $timestamp == $this->value->getTimeStamp(); |
| 91 |
} |
| 92 |
|
| 93 |
public function __toString(): string |
| 94 |
{ |
| 95 |
return (string) $this->value->format($this->format); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return bool|ElementDate |
| 100 |
*/ |
| 101 |
public static function parse(string $content, ?Document $document = null, int &$offset = 0) |
| 102 |
{ |
| 103 |
if (preg_match('/^\s*\(D\:(?P<name>.*?)\)/s', $content, $match)) { |
| 104 |
$name = $match['name']; |
| 105 |
$name = str_replace("'", '', $name); |
| 106 |
$date = false; |
| 107 |
|
| 108 |
// Smallest format : Y |
| 109 |
// Full format : YmdHisP |
| 110 |
if (preg_match('/^\d{4}(\d{2}(\d{2}(\d{2}(\d{2}(\d{2}(Z(\d{2,4})?|[\+-]?\d{2}(\d{2})?)?)?)?)?)?)?$/', $name)) { |
| 111 |
if ($pos = strpos($name, 'Z')) { |
| 112 |
$name = substr($name, 0, $pos + 1); |
| 113 |
} elseif (18 == \strlen($name) && preg_match('/[^\+-]0000$/', $name)) { |
| 114 |
$name = substr($name, 0, -4).'+0000'; |
| 115 |
} |
| 116 |
|
| 117 |
$format = self::$formats[\strlen($name)]; |
| 118 |
$date = \DateTime::createFromFormat($format, $name, new \DateTimeZone('UTC')); |
| 119 |
} else { |
| 120 |
// special cases |
| 121 |
if (preg_match('/^\d{1,2}-\d{1,2}-\d{4},?\s+\d{2}:\d{2}:\d{2}[\+-]\d{4}$/', $name)) { |
| 122 |
$name = str_replace(',', '', $name); |
| 123 |
$format = 'n-j-Y H:i:sO'; |
| 124 |
$date = \DateTime::createFromFormat($format, $name, new \DateTimeZone('UTC')); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if (!$date) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
$offset += strpos($content, '(D:') + \strlen($match['name']) + 4; // 1 for '(D:' and ')' |
| 133 |
|
| 134 |
return new self($date); |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|