TimeStamp.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Sabre\VObject\Property\VCard;
  3. use Sabre\VObject\DateTimeParser;
  4. use Sabre\VObject\Property\Text;
  5. use Sabre\Xml;
  6. /**
  7. * TimeStamp property.
  8. *
  9. * This object encodes TIMESTAMP values.
  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 TimeStamp extends Text
  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. * Returns the type of value.
  26. *
  27. * This corresponds to the VALUE= parameter. Every property also has a
  28. * 'default' valueType.
  29. *
  30. * @return string
  31. */
  32. public function getValueType()
  33. {
  34. return 'TIMESTAMP';
  35. }
  36. /**
  37. * Returns the value, in the format it should be encoded for json.
  38. *
  39. * This method must always return an array.
  40. *
  41. * @return array
  42. */
  43. public function getJsonValue()
  44. {
  45. $parts = DateTimeParser::parseVCardDateTime($this->getValue());
  46. $dateStr =
  47. $parts['year'].'-'.
  48. $parts['month'].'-'.
  49. $parts['date'].'T'.
  50. $parts['hour'].':'.
  51. $parts['minute'].':'.
  52. $parts['second'];
  53. // Timezone
  54. if (!is_null($parts['timezone'])) {
  55. $dateStr .= $parts['timezone'];
  56. }
  57. return [$dateStr];
  58. }
  59. /**
  60. * This method serializes only the value of a property. This is used to
  61. * create xCard or xCal documents.
  62. *
  63. * @param Xml\Writer $writer XML writer
  64. */
  65. protected function xmlSerializeValue(Xml\Writer $writer)
  66. {
  67. // xCard is the only XML and JSON format that has the same date and time
  68. // format than vCard.
  69. $valueType = strtolower($this->getValueType());
  70. $writer->writeElement($valueType, $this->getValue());
  71. }
  72. }