Boolean.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 FG\ASN1\ASNObject;
  12. use FG\ASN1\Parsable;
  13. use FG\ASN1\Identifier;
  14. use FG\ASN1\Exception\ParserException;
  15. class Boolean extends ASNObject implements Parsable
  16. {
  17. private $value;
  18. /**
  19. * @param bool $value
  20. */
  21. public function __construct($value)
  22. {
  23. $this->value = $value;
  24. }
  25. public function getType()
  26. {
  27. return Identifier::BOOLEAN;
  28. }
  29. protected function calculateContentLength()
  30. {
  31. return 1;
  32. }
  33. protected function getEncodedValue()
  34. {
  35. if ($this->value == false) {
  36. return chr(0x00);
  37. } else {
  38. return chr(0xFF);
  39. }
  40. }
  41. public function getContent()
  42. {
  43. if ($this->value == true) {
  44. return 'TRUE';
  45. } else {
  46. return 'FALSE';
  47. }
  48. }
  49. public static function fromBinary(&$binaryData, &$offsetIndex = 0)
  50. {
  51. self::parseIdentifier($binaryData[$offsetIndex], Identifier::BOOLEAN, $offsetIndex++);
  52. $contentLength = self::parseContentLength($binaryData, $offsetIndex);
  53. if ($contentLength != 1) {
  54. throw new ParserException("An ASN.1 Boolean should not have a length other than one. Extracted length was {$contentLength}", $offsetIndex);
  55. }
  56. $value = ord($binaryData[$offsetIndex++]);
  57. $booleanValue = $value == 0xFF ? true : false;
  58. $parsedObject = new self($booleanValue);
  59. $parsedObject->setContentLength($contentLength);
  60. return $parsedObject;
  61. }
  62. }