You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.3 KiB

  1. <?php namespace Sensory5\Shortcode\Classes;
  2. /**
  3. * Shortcodes
  4. *
  5. * Original idea from Pingpong Labs (https://github.com/pingpong-labs/shortcode)
  6. *
  7. * Changed to fit closer to the Thunderer\Shortcode syntax.
  8. */
  9. use Countable;
  10. use Thunder\Shortcode\HandlerContainer\HandlerContainer;
  11. use Thunder\Shortcode\Parser\RegexParser;
  12. use Thunder\Shortcode\Processor\Processor;
  13. use Thunder\Shortcode\Shortcode\ShortcodeInterface;
  14. class Shortcode implements Countable
  15. {
  16. /** @var HandlerContainer */
  17. private $handlers;
  18. /**
  19. * The constructor.
  20. */
  21. public function __construct()
  22. {
  23. $this->handlers = new HandlerContainer();
  24. }
  25. /**
  26. * Get the names for all registered shortcodes.
  27. *
  28. * @return array
  29. */
  30. public function getNames()
  31. {
  32. return $this->handlers->getNames();
  33. }
  34. /**
  35. * Add a new shortcode to the handler container.
  36. *
  37. * @param string $name
  38. * @param mixed $callback
  39. */
  40. public function add($name, $callback)
  41. {
  42. $this->handlers->add($name, $callback);
  43. }
  44. /**
  45. * Remove the specified shortcode name from the handler.
  46. *
  47. * @param string $name
  48. */
  49. public function remove($name)
  50. {
  51. if ($this->exists($name)) {
  52. $this->handlers->remove($name);
  53. }
  54. return $this;
  55. }
  56. /**
  57. * Remove all registered shortcodes
  58. *
  59. * @return self
  60. */
  61. public function destroyAll()
  62. {
  63. $this->handlers = new HandlerContainer();
  64. return $this;
  65. }
  66. /**
  67. * Strip any shortcodes from the content.
  68. *
  69. * @param string $content
  70. *
  71. * @return string
  72. */
  73. public function strip($content)
  74. {
  75. $handlers = new HandlerContainer();
  76. $handlers->setDefault(function(ShortcodeInterface $s) { return $s->getContent(); });
  77. $processor = new Processor(new RegexParser(), $handlers);
  78. return $processor->process($content);
  79. }
  80. /**
  81. * Get count from all shortcodes.
  82. *
  83. * @return int
  84. */
  85. public function count()
  86. {
  87. return count($this->handlers->getNames());
  88. }
  89. /**
  90. * Return true is the given name exist in shortcodes array.
  91. *
  92. * @param string $name
  93. *
  94. * @return bool
  95. */
  96. public function exists($name)
  97. {
  98. return $this->handlers->has($name);
  99. }
  100. /**
  101. * Return true is the given content contains the named shortcode.
  102. *
  103. * @param string $content
  104. * @param string $name
  105. *
  106. * @return bool
  107. */
  108. public function contains($content, $name)
  109. {
  110. $hasShortcode = false;
  111. $handlers = new HandlerContainer();
  112. $handlers->setDefault(function(ShortcodeInterface $s) use($name, &$hasShortcode) {
  113. if($s->getName() === $name) {
  114. $hasShortcode = true;
  115. }
  116. });
  117. $processor = new Processor(new RegexParser(), $handlers);
  118. $processor->process($content);
  119. return $hasShortcode;
  120. }
  121. /**
  122. * Parse content and replace parts of it using registered handlers
  123. *
  124. * @param $content
  125. *
  126. * @return string
  127. */
  128. public function parse($content)
  129. {
  130. $processor = new Processor(new RegexParser(), $this->handlers);
  131. return $processor->process($content);
  132. }
  133. }