AttCertValidityPeriod::toASN1()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\AttributeCertificate;
6
7
use ASN1\Type\Constructed\Sequence;
8
use ASN1\Type\Primitive\GeneralizedTime;
9
use X509\Feature\DateTimeHelper;
10
11
/**
12
 * Implements <i>AttCertValidityPeriod</i> ASN.1 type.
13
 *
14
 * @link https://tools.ietf.org/html/rfc5755#section-4.1
15
 */
16
class AttCertValidityPeriod
17
{
18
    use DateTimeHelper;
19
    
20
    /**
21
     * Not before time.
22
     *
23
     * @var \DateTimeImmutable
24
     */
25
    protected $_notBeforeTime;
26
    
27
    /**
28
     * Not after time.
29
     *
30
     * @var \DateTimeImmutable
31
     */
32
    protected $_notAfterTime;
33
    
34
    /**
35
     * Constructor.
36
     *
37
     * @param \DateTimeImmutable $nb
38
     * @param \DateTimeImmutable $na
39
     */
40 9
    public function __construct(\DateTimeImmutable $nb, \DateTimeImmutable $na)
41
    {
42 9
        $this->_notBeforeTime = $nb;
43 9
        $this->_notAfterTime = $na;
44 9
    }
45
    
46
    /**
47
     * Initialize from ASN.1.
48
     *
49
     * @param Sequence $seq
50
     * @return self
51
     */
52 6
    public static function fromASN1(Sequence $seq): self
53
    {
54 6
        $nb = $seq->at(0)
55 6
            ->asGeneralizedTime()
56 6
            ->dateTime();
57 6
        $na = $seq->at(1)
58 6
            ->asGeneralizedTime()
59 6
            ->dateTime();
60 6
        return new self($nb, $na);
61
    }
62
    
63
    /**
64
     * Initialize from date strings.
65
     *
66
     * @param string|null $nb_date Not before date
67
     * @param string|null $na_date Not after date
68
     * @param string|null $tz Timezone string
69
     * @return self
70
     */
71 2
    public static function fromStrings($nb_date, $na_date, $tz = null): self
72
    {
73 2
        $nb = self::_createDateTime($nb_date, $tz);
74 2
        $na = self::_createDateTime($na_date, $tz);
75 2
        return new self($nb, $na);
76
    }
77
    
78
    /**
79
     * Get not before time.
80
     *
81
     * @return \DateTimeImmutable
82
     */
83 7
    public function notBeforeTime(): \DateTimeImmutable
84
    {
85 7
        return $this->_notBeforeTime;
86
    }
87
    
88
    /**
89
     * Get not after time.
90
     *
91
     * @return \DateTimeImmutable
92
     */
93 6
    public function notAfterTime(): \DateTimeImmutable
94
    {
95 6
        return $this->_notAfterTime;
96
    }
97
    
98
    /**
99
     * Generate ASN.1 structure.
100
     *
101
     * @return Sequence
102
     */
103 20
    public function toASN1(): Sequence
104
    {
105 20
        return new Sequence(new GeneralizedTime($this->_notBeforeTime),
106 20
            new GeneralizedTime($this->_notAfterTime));
107
    }
108
}
109