/src/Operations/BasedDocument.php

https://github.com/GunioRobot/Google-Wave-PHP · PHP · 198 lines · 84 code · 24 blank · 90 comment · 4 complexity · a076128642690a408fb09eb1a3685ff3 MD5 · raw file

  1. <?php
  2. namespace echolibre\google_wave\Operations;
  3. use \echolibre\google_wave\Model\Blip as Blip;
  4. use \echolibre\google_wave\Model\Document as Document;
  5. use \echolibre\google_wave\Operations\Context as Context;
  6. use \echolibre\google_wave\Document\Range as Range;
  7. /**
  8. * BasedWave class
  9. *
  10. *
  11. * See the link lower for more reference.
  12. *
  13. * @see http://code.google.com/p/wave-robot-python-client/source/browse/trunk/src/waveapi/operations.py
  14. * @author David Coallier <david@echolibre.com>
  15. * @package echolibre\google_wave
  16. * @version 0.1.0
  17. * @license LGPL
  18. * @uses \echolibre\google_wave\Model\WaveData
  19. * @uses \echolibre\google_wave\Model\Wave
  20. * @uses \echolibre\google_wave\Operations\Context
  21. */
  22. class BasedDocument extends Document
  23. {
  24. private $_context;
  25. /**
  26. * Constructor
  27. *
  28. * Create a new BasedDocument object. This object generates operations.
  29. *
  30. * @param Blip $data The Blip object
  31. * @param Context $context The context instance.
  32. */
  33. public function __construct(Blip $data, Context $context)
  34. {
  35. parent::__construct($data);
  36. $this->_context = $context;
  37. }
  38. /**
  39. * Has Annotation
  40. *
  41. * This method will find if an annotation of a certain name exists
  42. *
  43. * @param string $name The name of the annotation to find
  44. * @return boolean True or False whether it finds out the annotation or not
  45. */
  46. public function hasAnnotation($name)
  47. {
  48. $annotationIterator = $this->annotations->getIterator();
  49. foreach ($annotationIterator->rewind(); $annotationIterator->valid(); $annotationIterator->next()) {
  50. // I want $it->current()['name']; .. patch accepted? yeah yeah...
  51. $current = $it->current();
  52. if ($current['name'] == $name) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. /**
  59. * Ranges for Annotation
  60. *
  61. * This does like "hasAnnotation" but returns the range of the found
  62. * annotation.
  63. *
  64. * @param string $name The name of the annotation to find.
  65. * @return mixed Either a Range ArrayObject or false if nothing is found.
  66. */
  67. public function rangesForAnnotation($name)
  68. {
  69. $annotationIterator = $this->annotations->getIterator();
  70. foreach ($annotationIterator->rewind(); $annotationIterator->valid(); $annotationIterator->next()) {
  71. $current = $it->current();
  72. if ($current['name'] == $name) {
  73. return $current['range'];
  74. }
  75. }
  76. return false;
  77. }
  78. /**
  79. * Set the text in document
  80. *
  81. * Set the text in a document and sets the content of the blip
  82. *
  83. * @param string $text The text to set
  84. * @return void
  85. */
  86. public function setText($text)
  87. {
  88. $this->clear();
  89. $this->_context->builder->insertDocument(
  90. $this->waveId, $this->waveletId, $this->blipId, $text
  91. );
  92. $this->blip->content = $text;
  93. }
  94. /**
  95. * Set the text in range
  96. *
  97. * This sets the text in a range
  98. *
  99. * @param Range $range The range object
  100. * @param string $text The string to set the text with
  101. * @return void
  102. */
  103. public function setTextInRange(Range $range, $text)
  104. {
  105. $this->deleteRange($range);
  106. $this->insertText($range->start, $text);
  107. }
  108. /**
  109. * Insert text
  110. *
  111. * This method will insert some text starting at some range.
  112. *
  113. * @param int $start Where to start in the range
  114. * @param string $text The text to insert
  115. * @return void
  116. */
  117. public function insertText($start, $text)
  118. {
  119. $this->_context->builder->insertDocument(
  120. $this->waveId, $this->waveletId, $this->blipId, $text
  121. );
  122. $left = substr($this->blip->content, 0, $start);
  123. $right = substr($this->blip->content, $start, sizeof($this->blip->content) - 1);
  124. $this->blip->content = $left . $text . $right;
  125. }
  126. /**
  127. * Append text
  128. *
  129. * Append some text to the content of a blip.
  130. *
  131. * @param string $text The new text to insert
  132. * @return void
  133. */
  134. public function appendText($text)
  135. {
  136. $this->_content->builder->appendDocument(
  137. $this->waveId, $this->waveletId, $this->blipId, $text
  138. );
  139. $this->blip->content += $text;
  140. }
  141. /**
  142. * Clear a document
  143. *
  144. * This method clears a document's content
  145. *
  146. * @return void
  147. */
  148. public function clear()
  149. {
  150. $this->_context->builder->documentDelete(
  151. $this->waveId, $this->waveletId,
  152. $this->blipId, 0, count($this->blip->content)
  153. );
  154. $this->blip->content = '';
  155. }
  156. /**
  157. * Delete Range
  158. *
  159. * This method is used to delete a range of values
  160. *
  161. * @param Range $range The range
  162. * @return void
  163. */
  164. public function deleteRange(Range $range)
  165. {
  166. $this->_context->builder->deleteDocument(
  167. $this->waveId, $this->waveletId,
  168. $this->blipId, $range->start, $range->end
  169. );
  170. $left = substr($this->blip->content, 0, $range->start);
  171. $right = substr($this->blip->content, $range->start, $range->end);
  172. $this->blip->content = $left . $right;
  173. }
  174. }