RelativeObjectIdentifier.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * This file is part of the PHPASN1 library.
  4. *
  5. * Copyright © Friedrich Große <friedrich.grosse@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FG\ASN1\Universal;
  11. use Exception;
  12. use FG\ASN1\Parsable;
  13. use FG\ASN1\Identifier;
  14. use FG\ASN1\Exception\ParserException;
  15. class RelativeObjectIdentifier extends ObjectIdentifier implements Parsable
  16. {
  17. public function __construct($subIdentifiers)
  18. {
  19. $this->value = $subIdentifiers;
  20. $this->subIdentifiers = explode('.', $subIdentifiers);
  21. $nrOfSubIdentifiers = count($this->subIdentifiers);
  22. for ($i = 0; $i < $nrOfSubIdentifiers; $i++) {
  23. if (is_numeric($this->subIdentifiers[$i])) {
  24. // enforce the integer type
  25. $this->subIdentifiers[$i] = intval($this->subIdentifiers[$i]);
  26. } else {
  27. throw new Exception("[{$subIdentifiers}] is no valid object identifier (sub identifier ".($i + 1).' is not numeric)!');
  28. }
  29. }
  30. }
  31. public function getType()
  32. {
  33. return Identifier::RELATIVE_OID;
  34. }
  35. public static function fromBinary(&$binaryData, &$offsetIndex = 0)
  36. {
  37. self::parseIdentifier($binaryData[$offsetIndex], Identifier::RELATIVE_OID, $offsetIndex++);
  38. $contentLength = self::parseContentLength($binaryData, $offsetIndex, 1);
  39. try {
  40. $oidString = self::parseOid($binaryData, $offsetIndex, $contentLength);
  41. } catch (ParserException $e) {
  42. throw new ParserException('Malformed ASN.1 Relative Object Identifier', $e->getOffset());
  43. }
  44. $parsedObject = new self($oidString);
  45. $parsedObject->setContentLength($contentLength);
  46. return $parsedObject;
  47. }
  48. }