Query.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Node;
  12. use League\CommonMark\Node\Query\AndExpr;
  13. use League\CommonMark\Node\Query\OrExpr;
  14. final class Query
  15. {
  16. /** @var callable(Node): bool $condition */
  17. private $condition;
  18. public function __construct()
  19. {
  20. $this->condition = new AndExpr();
  21. }
  22. public function where(callable ...$conditions): self
  23. {
  24. return $this->andWhere(...$conditions);
  25. }
  26. public function andWhere(callable ...$conditions): self
  27. {
  28. if ($this->condition instanceof AndExpr) {
  29. foreach ($conditions as $condition) {
  30. $this->condition->add($condition);
  31. }
  32. } else {
  33. $this->condition = new AndExpr($this->condition, ...$conditions);
  34. }
  35. return $this;
  36. }
  37. public function orWhere(callable ...$conditions): self
  38. {
  39. if ($this->condition instanceof OrExpr) {
  40. foreach ($conditions as $condition) {
  41. $this->condition->add($condition);
  42. }
  43. } else {
  44. $this->condition = new OrExpr($this->condition, ...$conditions);
  45. }
  46. return $this;
  47. }
  48. public function findOne(Node $node): ?Node
  49. {
  50. foreach ($node->iterator() as $n) {
  51. if (\call_user_func($this->condition, $n)) {
  52. return $n;
  53. }
  54. }
  55. return null;
  56. }
  57. /**
  58. * @return iterable<Node>
  59. */
  60. public function findAll(Node $node, ?int $limit = PHP_INT_MAX): iterable
  61. {
  62. $resultCount = 0;
  63. foreach ($node->iterator() as $n) {
  64. if ($resultCount >= $limit) {
  65. break;
  66. }
  67. if (! \call_user_func($this->condition, $n)) {
  68. continue;
  69. }
  70. ++$resultCount;
  71. yield $n;
  72. }
  73. }
  74. /**
  75. * @return callable(Node): bool
  76. */
  77. public static function type(string $class): callable
  78. {
  79. return static fn (Node $node): bool => $node instanceof $class;
  80. }
  81. /**
  82. * @psalm-param ?callable(Node): bool $condition
  83. *
  84. * @return callable(Node): bool
  85. */
  86. public static function hasChild(?callable $condition = null): callable
  87. {
  88. return static function (Node $node) use ($condition): bool {
  89. foreach ($node->children() as $child) {
  90. if ($condition === null || $condition($child)) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. };
  96. }
  97. /**
  98. * @psalm-param ?callable(Node): bool $condition
  99. *
  100. * @return callable(Node): bool
  101. */
  102. public static function hasParent(?callable $condition = null): callable
  103. {
  104. return static function (Node $node) use ($condition): bool {
  105. $parent = $node->parent();
  106. if ($parent === null) {
  107. return false;
  108. }
  109. if ($condition === null) {
  110. return true;
  111. }
  112. return $condition($parent);
  113. };
  114. }
  115. }