Binary.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * This file is part of the PHPExiftool package.
  4. *
  5. * (c) Alchemy <support@alchemy.fr>
  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 PHPExiftool\Driver\Value;
  11. use PHPExiftool\Exception\InvalidArgumentException;
  12. class Binary implements ValueInterface
  13. {
  14. protected $value;
  15. public function __construct($value)
  16. {
  17. $this->set($value);
  18. }
  19. public function getType()
  20. {
  21. return self::TYPE_BINARY;
  22. }
  23. public function asString()
  24. {
  25. return $this->value;
  26. }
  27. public function asArray()
  28. {
  29. return (array) $this->value;
  30. }
  31. public function asBase64()
  32. {
  33. return base64_encode($this->value);
  34. }
  35. public function set($value)
  36. {
  37. $this->value = $value;
  38. return $this;
  39. }
  40. public function setBase64Value($base64Value)
  41. {
  42. if (false === $value = base64_decode($base64Value, true)) {
  43. throw new InvalidArgumentException('The value should be base64 encoded');
  44. }
  45. $this->value = $value;
  46. return $this;
  47. }
  48. public static function loadFromBase64($base64Value)
  49. {
  50. if (false === $value = base64_decode($base64Value, true)) {
  51. throw new InvalidArgumentException('The value should be base64 encoded');
  52. }
  53. return new static($value);
  54. }
  55. }