FloatValue.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Sabre\VObject\Property;
  3. use Sabre\VObject\Property;
  4. use Sabre\Xml;
  5. /**
  6. * Float property.
  7. *
  8. * This object represents FLOAT values. These can be 1 or more floating-point
  9. * numbers.
  10. *
  11. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://sabre.io/license/ Modified BSD License
  14. */
  15. class FloatValue extends Property
  16. {
  17. /**
  18. * In case this is a multi-value property. This string will be used as a
  19. * delimiter.
  20. *
  21. * @var string
  22. */
  23. public $delimiter = ';';
  24. /**
  25. * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
  26. *
  27. * This has been 'unfolded', so only 1 line will be passed. Unescaping is
  28. * not yet done, but parameters are not included.
  29. *
  30. * @param string $val
  31. */
  32. public function setRawMimeDirValue($val)
  33. {
  34. $val = explode($this->delimiter, $val);
  35. foreach ($val as &$item) {
  36. $item = (float) $item;
  37. }
  38. $this->setParts($val);
  39. }
  40. /**
  41. * Returns a raw mime-dir representation of the value.
  42. *
  43. * @return string
  44. */
  45. public function getRawMimeDirValue()
  46. {
  47. return implode(
  48. $this->delimiter,
  49. $this->getParts()
  50. );
  51. }
  52. /**
  53. * Returns the type of value.
  54. *
  55. * This corresponds to the VALUE= parameter. Every property also has a
  56. * 'default' valueType.
  57. *
  58. * @return string
  59. */
  60. public function getValueType()
  61. {
  62. return 'FLOAT';
  63. }
  64. /**
  65. * Returns the value, in the format it should be encoded for JSON.
  66. *
  67. * This method must always return an array.
  68. *
  69. * @return array
  70. */
  71. public function getJsonValue()
  72. {
  73. $val = array_map('floatval', $this->getParts());
  74. // Special-casing the GEO property.
  75. //
  76. // See:
  77. // http://tools.ietf.org/html/draft-ietf-jcardcal-jcal-04#section-3.4.1.2
  78. if ('GEO' === $this->name) {
  79. return [$val];
  80. }
  81. return $val;
  82. }
  83. /**
  84. * Hydrate data from a XML subtree, as it would appear in a xCard or xCal
  85. * object.
  86. */
  87. public function setXmlValue(array $value)
  88. {
  89. $value = array_map('floatval', $value);
  90. parent::setXmlValue($value);
  91. }
  92. /**
  93. * This method serializes only the value of a property. This is used to
  94. * create xCard or xCal documents.
  95. *
  96. * @param Xml\Writer $writer XML writer
  97. */
  98. protected function xmlSerializeValue(Xml\Writer $writer)
  99. {
  100. // Special-casing the GEO property.
  101. //
  102. // See:
  103. // http://tools.ietf.org/html/rfc6321#section-3.4.1.2
  104. if ('GEO' === $this->name) {
  105. $value = array_map('floatval', $this->getParts());
  106. $writer->writeElement('latitude', $value[0]);
  107. $writer->writeElement('longitude', $value[1]);
  108. } else {
  109. parent::xmlSerializeValue($writer);
  110. }
  111. }
  112. }