Completed
Push — v2.0.x ( 7a58b6 )
by Florent
24:58
created

JWS::toJSON()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.439
cc 6
eloc 17
nc 9
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Object;
13
use Base64Url\Base64Url;
14
15
/**
16
 * Class JWS.
17
 */
18
final class JWS implements JWSInterface
19
{
20
    use JWT;
21
22
    /**
23
     * @var \Jose\Object\SignatureInterface[]
24
     */
25
    private $signatures = [];
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getEncodedPayload()
31
    {
32
        return is_string($this->getPayload())?Base64Url::encode($this->getPayload()):null;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getSignatures()
39
    {
40
        return $this->signatures;
41
    }
42
43
    /**
44
     * @param \Jose\Object\SignatureInterface $signature
45
     *
46
     * @return \Jose\Object\JWSInterface
47
     */
48
    public function addSignature(SignatureInterface $signature)
49
    {
50
        $jws = clone $this;
51
        $jws->signatures[] = $signature;
52
53
        return $jws;
54
    }
55
56
57
    /**
58
     * Returns the number of signature associated with the JWS.
59
     *
60
     * @return int
61
     */
62
    public function countSignatures()
63
    {
64
        return count($this->signatures);
65
    }
66
67
    /**
68
     * @param int $signature
69
     *
70
     * @return string
71
     */
72
    public function toCompactJSON($signature)
73
    {
74
        if (!isset($this->signatures[$signature])) {
75
            throw new \InvalidArgumentException('The signature does not exist.');
76
        }
77
78
        if (!empty($this->signatures[$signature]->getHeaders())) {
79
            throw new \InvalidArgumentException('The signature contains unprotected headers and cannot be converted into compact JSON');
80
        }
81
82
        return sprintf(
83
            '%s.%s.%s',
84
            $this->signatures[$signature]->getEncodedProtectedHeaders(),
85
            $this->getEncodedPayload(),
86
            Base64Url::encode($this->signatures[$signature]->getSignature())
87
        );
88
    }
89
90
    /**
91
     * @param int $signature
92
     *
93
     * @return string
94
     */
95
    public function toFlattenedJSON($signature)
96
    {
97
        if (!isset($this->signatures[$signature])) {
98
            throw new \InvalidArgumentException('The signature does not exist.');
99
        }
100
101
        $data = [];
102
        $values = [
103
            'payload' => $this->getEncodedPayload(),
104
            'protected' => $this->signatures[$signature]->getEncodedProtectedHeaders(),
105
            'header' => $this->signatures[$signature]->getHeaders(),
106
        ];
107
108
        foreach ($values as $key=>$value) {
109
            if (!empty($value)) {
110
                $data[$key] = $value;
111
            }
112
        }
113
        $data['signature'] = $this->signatures[$signature]->getSignature();
114
115
        return json_encode($data);
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function toJSON()
122
    {
123
        if (0 === $this->countSignatures()) {
124
            throw new \BadMethodCallException('No signature.');
125
        }
126
127
        $data = [];
128
        if (!empty($this->getEncodedPayload())) {
129
            $data['payload'] = $this->getEncodedPayload();
130
        }
131
132
        $data['signatures'] = [];
133
        foreach ($this->getSignatures() as $signature) {
134
135
            $tmp = ['signature' => $signature->getSignature()];
136
            $values = [
137
                'protected' => $signature->getEncodedProtectedHeaders(),
138
                'header' => $signature->getHeaders(),
139
            ];
140
141
            foreach ($values as $key => $value) {
142
                if (!empty($value)) {
143
                    $tmp[$key] = $value;
144
                }
145
            }
146
            $data['signatures'][] = $tmp;
147
        }
148
149
        return json_encode($data);
150
    }
151
}
152