Skip to content

Commit 41dd2be

Browse files
committed
TemplateParser marked deprecated
1 parent 3381266 commit 41dd2be

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

src/ASN1/Mapper/Mapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace FG\ASN1\Mapper;
4+
45
use FG\ASN1\AbstractTaggedObject;
56
use FG\ASN1\ASN1ObjectInterface;
67
use FG\ASN1\Identifier;

src/ASN1/TemplateParser.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/*
3+
* This file is part of the PHPASN1 library.
4+
*
5+
* Copyright © Friedrich Große <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace FG\ASN1;
12+
13+
use Exception;
14+
use FG\ASN1\Exception\ParserException;
15+
use FG\ASN1\Universal\Sequence;
16+
17+
/**
18+
* @deprecated
19+
*
20+
* use \FG\ASN1\Mapper\Mapper instead
21+
*/
22+
class TemplateParser
23+
{
24+
/**
25+
* @param string $data
26+
* @param array $template
27+
*
28+
* @return \FG\ASN1\ASN1Object|Sequence
29+
* @throws ParserException if there was an issue parsing
30+
*/
31+
public function parseBase64($data, array $template)
32+
{
33+
// TODO test with invalid data
34+
return $this->parseBinary(base64_decode($data), $template);
35+
}
36+
37+
/**
38+
* @param string $binary
39+
* @param array $template
40+
*
41+
* @return \FG\ASN1\ASN1Object|Sequence
42+
* @throws ParserException if there was an issue parsing
43+
*/
44+
public function parseBinary($binary, array $template)
45+
{
46+
$parsedObject = ASN1Object::fromBinary($binary);
47+
48+
foreach ($template as $key => $value) {
49+
$this->validate($parsedObject, $key, $value);
50+
}
51+
52+
return $parsedObject;
53+
}
54+
55+
private function validate(ASN1Object $object, $key, $value)
56+
{
57+
if (is_array($value)) {
58+
$this->assertTypeId($key, $object);
59+
60+
/* @var Construct $object */
61+
$childrenCount = count($value);
62+
reset($value);
63+
for ($i = 0; $i < $childrenCount; $i++) {
64+
$this->validate($object->getChildren()[$i], key($value), current($value));
65+
next($value);
66+
}
67+
} else {
68+
$this->assertTypeId($value, $object);
69+
}
70+
}
71+
72+
private function assertTypeId($expectedTypeId, ASN1Object $object)
73+
{
74+
$actualType = $object->getIdentifier()->getTagNumber();
75+
if ($expectedTypeId !== $actualType) {
76+
throw new Exception("Expected type ($expectedTypeId) does not match actual type ($actualType");
77+
}
78+
}
79+
}

tests/ASN1/TemplateParserTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace FG\Test\ASN1;
4+
5+
use FG\ASN1\Identifier;
6+
use FG\ASN1\TemplateParser;
7+
use FG\ASN1\Universal\BitString;
8+
use FG\ASN1\Universal\Integer;
9+
use FG\ASN1\Universal\ObjectIdentifier;
10+
use FG\ASN1\Universal\Sequence;
11+
use FG\ASN1\Universal\Set;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* @deprecated
16+
*/
17+
class TemplateParserTest extends TestCase
18+
{
19+
public function testParseBase64()
20+
{
21+
$sequence = Sequence::create([
22+
Set::create([
23+
ObjectIdentifier::create('1.2.250.1.16.9'),
24+
Sequence::create([
25+
Integer::create(42),
26+
BitString::createFromHexString('A0120043'),
27+
])
28+
])
29+
]);
30+
31+
$data = base64_encode($sequence->getBinary());
32+
33+
$template = [
34+
Identifier::SEQUENCE => [
35+
Identifier::SET => [
36+
Identifier::OBJECT_IDENTIFIER,
37+
Identifier::SEQUENCE => [
38+
Identifier::INTEGER,
39+
Identifier::BITSTRING,
40+
],
41+
],
42+
],
43+
];
44+
45+
$parser = new TemplateParser();
46+
$object = $parser->parseBase64($data, $template);
47+
$this->assertInstanceOf(Set::class, $object->getChildren()[0]);
48+
$this->assertInstanceOf(ObjectIdentifier::class, $object->getChildren()[0]->getChildren()[0]);
49+
$this->assertInstanceOf(Sequence::class, $object->getChildren()[0]->getChildren()[1]);
50+
$this->assertInstanceOf(Integer::class, $object->getChildren()[0]->getChildren()[1]->getChildren()[0]);
51+
$this->assertInstanceOf(BitString::class, $object->getChildren()[0]->getChildren()[1]->getChildren()[1]);
52+
}
53+
}

0 commit comments

Comments
 (0)