Binary.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Sabre\VObject\Property;
  3. use Sabre\VObject\Property;
  4. /**
  5. * BINARY property.
  6. *
  7. * This object represents BINARY values.
  8. *
  9. * Binary values are most commonly used by the iCalendar ATTACH property, and
  10. * the vCard PHOTO property.
  11. *
  12. * This property will transparently encode and decode to base64.
  13. *
  14. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  15. * @author Evert Pot (http://evertpot.com/)
  16. * @license http://sabre.io/license/ Modified BSD License
  17. */
  18. class Binary extends Property
  19. {
  20. /**
  21. * In case this is a multi-value property. This string will be used as a
  22. * delimiter.
  23. *
  24. * @var string
  25. */
  26. public $delimiter = '';
  27. /**
  28. * Updates the current value.
  29. *
  30. * This may be either a single, or multiple strings in an array.
  31. *
  32. * @param string|array $value
  33. */
  34. public function setValue($value)
  35. {
  36. if (is_array($value)) {
  37. if (1 === count($value)) {
  38. $this->value = $value[0];
  39. } else {
  40. throw new \InvalidArgumentException('The argument must either be a string or an array with only one child');
  41. }
  42. } else {
  43. $this->value = $value;
  44. }
  45. }
  46. /**
  47. * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
  48. *
  49. * This has been 'unfolded', so only 1 line will be passed. Unescaping is
  50. * not yet done, but parameters are not included.
  51. *
  52. * @param string $val
  53. */
  54. public function setRawMimeDirValue($val)
  55. {
  56. $this->value = base64_decode($val);
  57. }
  58. /**
  59. * Returns a raw mime-dir representation of the value.
  60. *
  61. * @return string
  62. */
  63. public function getRawMimeDirValue()
  64. {
  65. return base64_encode($this->value);
  66. }
  67. /**
  68. * Returns the type of value.
  69. *
  70. * This corresponds to the VALUE= parameter. Every property also has a
  71. * 'default' valueType.
  72. *
  73. * @return string
  74. */
  75. public function getValueType()
  76. {
  77. return 'BINARY';
  78. }
  79. /**
  80. * Returns the value, in the format it should be encoded for json.
  81. *
  82. * This method must always return an array.
  83. *
  84. * @return array
  85. */
  86. public function getJsonValue()
  87. {
  88. return [base64_encode($this->getValue())];
  89. }
  90. /**
  91. * Sets the json value, as it would appear in a jCard or jCal object.
  92. *
  93. * The value must always be an array.
  94. */
  95. public function setJsonValue(array $value)
  96. {
  97. $value = array_map('base64_decode', $value);
  98. parent::setJsonValue($value);
  99. }
  100. }