summaryrefslogtreecommitdiff
path: root/src/Concerns/HasAncestors.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Concerns/HasAncestors.php')
-rw-r--r--src/Concerns/HasAncestors.php37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/Concerns/HasAncestors.php b/src/Concerns/HasAncestors.php
new file mode 100644
index 0000000..248e9c8
--- /dev/null
+++ b/src/Concerns/HasAncestors.php
@@ -0,0 +1,37 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\Router\Concerns;
+
+trait HasAncestors
+{
+ private const MAX_ANCESTORY_DEPTH = 100;
+
+ /** @return self[] */
+ public function getAncestors(): array
+ {
+ $results = [];
+
+ $count = 0;
+ $instance = $this->parent;
+ while (
+ null !== $instance
+ && $count++ < self::MAX_ANCESTORY_DEPTH
+ ) {
+ $results[] = $instance;
+ $instance = $instance->parent;
+ }
+
+ return array_reverse($results);
+ }
+
+ /** @return self[] */
+ public function getAncestorsAndSelf(): array
+ {
+ $results = $this->getAncestors();
+ $results[] = $this;
+
+ return $results;
+ }
+}