Writer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Util;
  18. use JMS\SerializerBundle\Exception\RuntimeException;
  19. /**
  20. * A writer implementation.
  21. *
  22. * This may be used to simplify writing well-formatted code.
  23. *
  24. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25. */
  26. class Writer
  27. {
  28. public $indentationSpaces = 4;
  29. public $indentationLevel = 0;
  30. public $content = '';
  31. public $changeCount = 0;
  32. private $changes = array();
  33. public function indent()
  34. {
  35. $this->indentationLevel += 1;
  36. return $this;
  37. }
  38. public function outdent()
  39. {
  40. $this->indentationLevel -= 1;
  41. if ($this->indentationLevel < 0) {
  42. throw new RuntimeException('The identation level cannot be less than zero.');
  43. }
  44. return $this;
  45. }
  46. public function writeln($content)
  47. {
  48. $this->write($content."\n");
  49. return $this;
  50. }
  51. public function revert()
  52. {
  53. $change = array_pop($this->changes);
  54. $this->changeCount -=1 ;
  55. $this->content = substr($this->content, 0, -1 * strlen($change));
  56. }
  57. public function write($content)
  58. {
  59. $contentEndsWithNewLine = "\n" === substr($this->content, -1);
  60. $addition = '';
  61. $lines = explode("\n", $content);
  62. for ($i=0,$c=count($lines); $i<$c; $i++) {
  63. if ($this->indentationLevel > 0
  64. && !empty($lines[$i])
  65. && ((empty($addition) && "\n" === substr($this->content, -1)) || "\n" === substr($addition, -1))) {
  66. $addition .= str_repeat(' ', $this->indentationLevel * $this->indentationSpaces);
  67. }
  68. $addition .= $lines[$i];
  69. if ($i+1 < $c) {
  70. $addition .= "\n";
  71. }
  72. }
  73. $this->content .= $addition;
  74. $this->changes[] = $addition;
  75. $this->changeCount += 1;
  76. return $this;
  77. }
  78. public function rtrim($preserveNewLines = true)
  79. {
  80. if (!$preserveNewLines) {
  81. $this->content = rtrim($this->content);
  82. return $this;
  83. }
  84. $addNl = "\n" === substr($this->content, -1);
  85. $this->content = rtrim($this->content);
  86. if ($addNl) {
  87. $this->content .= "\n";
  88. }
  89. return $this;
  90. }
  91. public function reset()
  92. {
  93. $this->content = '';
  94. $this->indentationLevel = 0;
  95. return $this;
  96. }
  97. public function getContent()
  98. {
  99. return $this->content;
  100. }
  101. }