Binary.php 2.7 KB

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