@@ -18,18 +18,18 @@ discard block |
||
18 | 18 | */ |
19 | 19 | public static function encode($source, $mapping) |
20 | 20 | { |
21 | - if (isset($mapping['default']) && $source === $mapping['default']) { |
|
21 | + if (isset($mapping[ 'default' ]) && $source === $mapping[ 'default' ]) { |
|
22 | 22 | return ''; |
23 | 23 | } |
24 | 24 | |
25 | - $tag = $mapping['tag']; |
|
25 | + $tag = $mapping[ 'tag' ]; |
|
26 | 26 | switch ($tag) { |
27 | 27 | case ASN1::TYPE_SET: |
28 | 28 | case ASN1::TYPE_SEQUENCE: |
29 | 29 | $tag |= 0x20; // set the constructed bit |
30 | 30 | $value = ''; |
31 | - if (isset($mapping['min']) && isset($mapping['max'])) { |
|
32 | - $child = $mapping['children']; |
|
31 | + if (isset($mapping[ 'min' ]) && isset($mapping[ 'max' ])) { |
|
32 | + $child = $mapping[ 'children' ]; |
|
33 | 33 | foreach ($source as $content) { |
34 | 34 | $temp = static::encode($content, $child); |
35 | 35 | if ($temp === false) { |
@@ -39,23 +39,23 @@ discard block |
||
39 | 39 | } |
40 | 40 | break; |
41 | 41 | } |
42 | - if (isset($mapping['repeat'])) { |
|
42 | + if (isset($mapping[ 'repeat' ])) { |
|
43 | 43 | foreach ($source as $content) { |
44 | - $temp = static::encode($content, $mapping['repeat']); |
|
44 | + $temp = static::encode($content, $mapping[ 'repeat' ]); |
|
45 | 45 | if ($temp === false) { |
46 | 46 | return false; |
47 | 47 | } |
48 | 48 | $value .= $temp; |
49 | 49 | } |
50 | 50 | } else { |
51 | - foreach ($mapping['children'] as $key => $child) { |
|
51 | + foreach ($mapping[ 'children' ] as $key => $child) { |
|
52 | 52 | if (!array_key_exists($key, $source)) { |
53 | - if (!isset($child['optional'])) { |
|
53 | + if (!isset($child[ 'optional' ])) { |
|
54 | 54 | return false; |
55 | 55 | } |
56 | 56 | continue; |
57 | 57 | } |
58 | - $temp = static::encode($source[$key], $child); |
|
58 | + $temp = static::encode($source[ $key ], $child); |
|
59 | 59 | if ($temp === false) { |
60 | 60 | return false; |
61 | 61 | } |
@@ -68,11 +68,11 @@ discard block |
||
68 | 68 | break; |
69 | 69 | case ASN1::TYPE_CHOICE: |
70 | 70 | $temp = false; |
71 | - foreach ($mapping['children'] as $key => $child) { |
|
72 | - if (!isset($source[$key])) { |
|
71 | + foreach ($mapping[ 'children' ] as $key => $child) { |
|
72 | + if (!isset($source[ $key ])) { |
|
73 | 73 | continue; |
74 | 74 | } |
75 | - $temp = static::encode($source[$key], $child); |
|
75 | + $temp = static::encode($source[ $key ], $child); |
|
76 | 76 | if ($temp === false) { |
77 | 77 | return false; |
78 | 78 | } |
@@ -83,20 +83,20 @@ discard block |
||
83 | 83 | return $temp; |
84 | 84 | case ASN1::TYPE_INTEGER: |
85 | 85 | case ASN1::TYPE_ENUMERATED: |
86 | - if (!isset($mapping['map']) && isset($mapping['base']) && $mapping['base'] === 16) { |
|
86 | + if (!isset($mapping[ 'map' ]) && isset($mapping[ 'base' ]) && $mapping[ 'base' ] === 16) { |
|
87 | 87 | if (strlen($source) % 2 == 1) { |
88 | - $source = '0' . $source; |
|
88 | + $source = '0'.$source; |
|
89 | 89 | } |
90 | 90 | $value = hex2bin($source); |
91 | 91 | } else { |
92 | - if (!isset($mapping['map'])) { |
|
93 | - $value = ASN1::toBase256($source, isset($mapping['base']) ? $mapping['base'] : 10); |
|
92 | + if (!isset($mapping[ 'map' ])) { |
|
93 | + $value = ASN1::toBase256($source, isset($mapping[ 'base' ]) ? $mapping[ 'base' ] : 10); |
|
94 | 94 | } else { |
95 | - $value = array_search($source, $mapping['map']); |
|
95 | + $value = array_search($source, $mapping[ 'map' ]); |
|
96 | 96 | if ($value === false) { |
97 | 97 | return false; |
98 | 98 | } |
99 | - $value = ASN1::toBase256($value, isset($mapping['base']) ? (int)$mapping['base'] : 10); |
|
99 | + $value = ASN1::toBase256($value, isset($mapping[ 'base' ]) ? (int) $mapping[ 'base' ] : 10); |
|
100 | 100 | } |
101 | 101 | } |
102 | 102 | if (!strlen($value)) { |
@@ -105,24 +105,24 @@ discard block |
||
105 | 105 | break; |
106 | 106 | case ASN1::TYPE_UTC_TIME: |
107 | 107 | case ASN1::TYPE_GENERALIZED_TIME: |
108 | - $format = $mapping['tag'] == ASN1::TYPE_UTC_TIME ? 'y' : 'Y'; |
|
109 | - $format.= 'mdHis'; |
|
110 | - $value = @gmdate($format, strtotime($source)) . 'Z'; |
|
108 | + $format = $mapping[ 'tag' ] == ASN1::TYPE_UTC_TIME ? 'y' : 'Y'; |
|
109 | + $format .= 'mdHis'; |
|
110 | + $value = @gmdate($format, strtotime($source)).'Z'; |
|
111 | 111 | break; |
112 | 112 | case ASN1::TYPE_BIT_STRING: |
113 | - if (isset($mapping['map'])) { |
|
114 | - $mcnt = count($mapping['map']); |
|
113 | + if (isset($mapping[ 'map' ])) { |
|
114 | + $mcnt = count($mapping[ 'map' ]); |
|
115 | 115 | $bits = array_fill(0, $mcnt, 0); |
116 | 116 | $size = 0; |
117 | 117 | for ($i = 0; $i < $mcnt; $i++) { |
118 | - if (in_array($mapping['map'][$i], $source)) { |
|
119 | - $bits[$i] = 1; |
|
118 | + if (in_array($mapping[ 'map' ][ $i ], $source)) { |
|
119 | + $bits[ $i ] = 1; |
|
120 | 120 | $size = $i; |
121 | 121 | } |
122 | 122 | } |
123 | 123 | |
124 | - if (isset($mapping['min']) && $mapping['min'] >= 1 && $size < $mapping['min']) { |
|
125 | - $size = $mapping['min'] - 1; |
|
124 | + if (isset($mapping[ 'min' ]) && $mapping[ 'min' ] >= 1 && $size < $mapping[ 'min' ]) { |
|
125 | + $size = $mapping[ 'min' ] - 1; |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | $offset = 8 - (($size + 1) & 7); |
@@ -131,23 +131,23 @@ discard block |
||
131 | 131 | $value = chr($offset); |
132 | 132 | |
133 | 133 | for ($i = $size + 1; $i < $mcnt; $i++) { |
134 | - unset($bits[$i]); |
|
134 | + unset($bits[ $i ]); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | $bits = implode('', array_pad($bits, $size + $offset + 1, 0)); |
138 | 138 | $bytes = explode(' ', rtrim(chunk_split($bits, 8, ' '))); |
139 | 139 | foreach ($bytes as $byte) { |
140 | - $value.= chr((int)bindec($byte)); |
|
140 | + $value .= chr((int) bindec($byte)); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | break; |
144 | 144 | } |
145 | 145 | // default to octet string if no mapping is present |
146 | 146 | case ASN1::TYPE_OCTET_STRING: |
147 | - $value = isset($mapping['raw']) && $mapping['raw'] ? $source : base64_decode($source); |
|
147 | + $value = isset($mapping[ 'raw' ]) && $mapping[ 'raw' ] ? $source : base64_decode($source); |
|
148 | 148 | break; |
149 | 149 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
150 | - if (!isset($source) && $mapping['optional']) { |
|
150 | + if (!isset($source) && $mapping[ 'optional' ]) { |
|
151 | 151 | return; |
152 | 152 | } |
153 | 153 | $oid = preg_match('(^(\d+\.?)+$)', $source) ? $source : ASN1::TextToOID($source); |
@@ -155,20 +155,20 @@ discard block |
||
155 | 155 | throw new ASN1Exception('Invalid OID'); |
156 | 156 | } |
157 | 157 | $parts = explode('.', $oid); |
158 | - $value = chr(40 * $parts[0] + $parts[1]); |
|
158 | + $value = chr(40 * $parts[ 0 ] + $parts[ 1 ]); |
|
159 | 159 | $pcnt = count($parts); |
160 | 160 | for ($i = 2; $i < $pcnt; $i++) { |
161 | 161 | $temp = ''; |
162 | - if (!$parts[$i]) { |
|
162 | + if (!$parts[ $i ]) { |
|
163 | 163 | $temp = "\0"; |
164 | 164 | } else { |
165 | - while ($parts[$i]) { |
|
166 | - $temp = chr(0x80 | ($parts[$i] & 0x7F)) . $temp; |
|
167 | - $parts[$i] >>= 7; |
|
165 | + while ($parts[ $i ]) { |
|
166 | + $temp = chr(0x80 | ($parts[ $i ] & 0x7F)).$temp; |
|
167 | + $parts[ $i ] >>= 7; |
|
168 | 168 | } |
169 | - $temp[strlen($temp) - 1] = $temp[strlen($temp) - 1] & chr(0x7F); |
|
169 | + $temp[ strlen($temp) - 1 ] = $temp[ strlen($temp) - 1 ] & chr(0x7F); |
|
170 | 170 | } |
171 | - $value.= $temp; |
|
171 | + $value .= $temp; |
|
172 | 172 | } |
173 | 173 | break; |
174 | 174 | case ASN1::TYPE_ANY: |
@@ -209,18 +209,18 @@ discard block |
||
209 | 209 | } |
210 | 210 | |
211 | 211 | $length = static::length(strlen($value)); |
212 | - if (isset($mapping['name'])) { |
|
213 | - if (isset($mapping['implicit']) && $mapping['implicit']) { |
|
214 | - $tag = ((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | (ord($value[0]) & 0x20) | $mapping['name']; |
|
215 | - return chr($tag) . $length . $value; |
|
212 | + if (isset($mapping[ 'name' ])) { |
|
213 | + if (isset($mapping[ 'implicit' ]) && $mapping[ 'implicit' ]) { |
|
214 | + $tag = ((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | (ord($value[ 0 ]) & 0x20) | $mapping[ 'name' ]; |
|
215 | + return chr($tag).$length.$value; |
|
216 | 216 | } else { |
217 | - $value = chr($tag) . $length . $value; |
|
218 | - return chr(((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | 0x20 | $mapping['name']) . |
|
219 | - static::length(strlen($value)) . |
|
217 | + $value = chr($tag).$length.$value; |
|
218 | + return chr(((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | 0x20 | $mapping[ 'name' ]). |
|
219 | + static::length(strlen($value)). |
|
220 | 220 | $value; |
221 | 221 | } |
222 | 222 | } |
223 | - return chr($tag) . $length . $value; |
|
223 | + return chr($tag).$length.$value; |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | protected static function length($length) |