A fork of potaka/bbcode to handle future maintenance
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.

63 lines
1.1KB

  1. <?php
  2. namespace Knockout\BbCode\TagBag;
  3. use Knockout\BbCode\Tag\TagInterface;
  4. /**
  5. * Container for Tags
  6. *
  7. * @author po_taka <angel.koilov@gmail.com>
  8. */
  9. class TagBag
  10. {
  11. /**
  12. * @var TagInterface[]
  13. */
  14. private $tags = [];
  15. /**
  16. * @param TagInterface[] $tags
  17. */
  18. public function __construct(array $tags = [])
  19. {
  20. foreach ($tags as $tag) {
  21. $this->addTag($tag);
  22. }
  23. }
  24. public function addTag(TagInterface $tag)
  25. {
  26. $this->tags[] = $tag;
  27. }
  28. public function contains(TagInterface $tagTocheck)
  29. {
  30. foreach ($this->tags as $tag) {
  31. if ($tag === $tagTocheck) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. /**
  38. * Return intersection with given tags
  39. *
  40. * @param array $tags
  41. * @return self
  42. */
  43. public function intersect(array $tags)
  44. {
  45. $result = new self();
  46. foreach ($tags as $tag) {
  47. if ($this->contains($tag)) {
  48. $result->addTag($tag);
  49. }
  50. }
  51. return $result;
  52. }
  53. }