Reader.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Sabre\VObject;
  3. /**
  4. * iCalendar/vCard/jCal/jCard/xCal/xCard reader object.
  5. *
  6. * This object provides a few (static) convenience methods to quickly access
  7. * the parsers.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. class Reader
  14. {
  15. /**
  16. * If this option is passed to the reader, it will be less strict about the
  17. * validity of the lines.
  18. */
  19. const OPTION_FORGIVING = 1;
  20. /**
  21. * If this option is turned on, any lines we cannot parse will be ignored
  22. * by the reader.
  23. */
  24. const OPTION_IGNORE_INVALID_LINES = 2;
  25. /**
  26. * Parses a vCard or iCalendar object, and returns the top component.
  27. *
  28. * The options argument is a bitfield. Pass any of the OPTIONS constant to
  29. * alter the parsers' behaviour.
  30. *
  31. * You can either supply a string, or a readable stream for input.
  32. *
  33. * @param string|resource $data
  34. * @param int $options
  35. * @param string $charset
  36. *
  37. * @return Document
  38. */
  39. public static function read($data, $options = 0, $charset = 'UTF-8')
  40. {
  41. $parser = new Parser\MimeDir();
  42. $parser->setCharset($charset);
  43. $result = $parser->parse($data, $options);
  44. return $result;
  45. }
  46. /**
  47. * Parses a jCard or jCal object, and returns the top component.
  48. *
  49. * The options argument is a bitfield. Pass any of the OPTIONS constant to
  50. * alter the parsers' behaviour.
  51. *
  52. * You can either a string, a readable stream, or an array for its input.
  53. * Specifying the array is useful if json_decode was already called on the
  54. * input.
  55. *
  56. * @param string|resource|array $data
  57. * @param int $options
  58. *
  59. * @return Document
  60. */
  61. public static function readJson($data, $options = 0)
  62. {
  63. $parser = new Parser\Json();
  64. $result = $parser->parse($data, $options);
  65. return $result;
  66. }
  67. /**
  68. * Parses a xCard or xCal object, and returns the top component.
  69. *
  70. * The options argument is a bitfield. Pass any of the OPTIONS constant to
  71. * alter the parsers' behaviour.
  72. *
  73. * You can either supply a string, or a readable stream for input.
  74. *
  75. * @param string|resource $data
  76. * @param int $options
  77. *
  78. * @return Document
  79. */
  80. public static function readXML($data, $options = 0)
  81. {
  82. $parser = new Parser\XML();
  83. $result = $parser->parse($data, $options);
  84. return $result;
  85. }
  86. }