Test Failed
Pull Request — master (#614)
by
unknown
02:15
created

ElementDate   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 45
c 3
b 0
f 0
dl 0
loc 98
ccs 36
cts 36
cp 1
rs 10
wmc 14
1
<?php
2
3
/**
4
 * @file
5
 *          This file is part of the PdfParser library.
6
 *
7
 * @author  Sébastien MALOT <[email protected]>
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 <[email protected]>
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
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 47
    public function __construct($value)
69
    {
70 47
        if (!($value instanceof \DateTime)) {
71 1
            throw new \Exception('DateTime required.'); // FIXME: Sometimes strings are passed to this function
72
        }
73
74 46
        parent::__construct($value);
75 46
    }
76
77 3
    public function setFormat(string $format)
78
    {
79 3
        $this->format = $format;
80 3
    }
81
82 2
    public function equals($value): bool
83
    {
84 2
        if ($value instanceof \DateTime) {
85 1
            $timestamp = $value->getTimeStamp();
86
        } else {
87 2
            $timestamp = strtotime($value);
88
        }
89
90 2
        return $timestamp == $this->value->getTimeStamp();
91
    }
92
93 41
    public function __toString(): string
94
    {
95 41
        return (string) $this->value->format($this->format);
96
    }
97
98
    /**
99
     * @return bool|ElementDate
100
     */
101 48
    public static function parse(string $content, Document $document = null, int &$offset = 0)
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_VARIABLE, expecting T_STRING or T_NAME_QUALIFIED or T_NAME_FULLY_QUALIFIED or T_NAME_RELATIVE on line 101 at column 82
Loading history...
102
    {
103 48
        if (preg_match('/^\s*\(D\:(?P<name>.*?)\)/s', $content, $match)) {
104 42
            $name = $match['name'];
105 42
            $name = str_replace("'", '', $name);
106 42
            $date = false;
107
108
            // Smallest format : Y
109
            // Full format     : YmdHisP
110 42
            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 42
                if ($pos = strpos($name, 'Z')) {
112 6
                    $name = substr($name, 0, $pos + 1);
113 41
                } elseif (18 == \strlen($name) && preg_match('/[^\+-]0000$/', $name)) {
114 1
                    $name = substr($name, 0, -4).'+0000';
115
                }
116
117 42
                $format = self::$formats[\strlen($name)];
118 42
                $date = \DateTime::createFromFormat($format, $name, new \DateTimeZone('UTC'));
119
            } else {
120
                // special cases
121 1
                if (preg_match('/^\d{1,2}-\d{1,2}-\d{4},?\s+\d{2}:\d{2}:\d{2}[\+-]\d{4}$/', $name)) {
122 1
                    $name = str_replace(',', '', $name);
123 1
                    $format = 'n-j-Y H:i:sO';
124 1
                    $date = \DateTime::createFromFormat($format, $name, new \DateTimeZone('UTC'));
125
                }
126
            }
127
128 42
            if (!$date) {
129 1
                return false;
130
            }
131
132 42
            $offset += strpos($content, '(D:') + \strlen($match['name']) + 4; // 1 for '(D:' and ')'
133
134 42
            return new self($date);
135
        }
136
137 48
        return false;
138
    }
139
}
140