12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace FG\ASN1;
- use Exception;
- use FG\ASN1\Exception\ParserException;
- use FG\ASN1\Universal\Sequence;
- class TemplateParser
- {
-
- public function parseBase64($data, array $template)
- {
-
- return $this->parseBinary(base64_decode($data), $template);
- }
-
- public function parseBinary($binary, array $template)
- {
- $parsedObject = ASNObject::fromBinary($binary);
- foreach ($template as $key => $value) {
- $this->validate($parsedObject, $key, $value);
- }
- return $parsedObject;
- }
- private function validate(ASNObject $object, $key, $value)
- {
- if (is_array($value)) {
- $this->assertTypeId($key, $object);
-
- foreach ($value as $key => $child) {
- $this->validate($object->current(), $key, $child);
- $object->next();
- }
- } else {
- $this->assertTypeId($value, $object);
- }
- }
- private function assertTypeId($expectedTypeId, ASNObject $object)
- {
- $actualType = $object->getType();
- if ($expectedTypeId != $actualType) {
- throw new Exception("Expected type ($expectedTypeId) does not match actual type ($actualType");
- }
- }
- }
|