From 32a486f1d41fce987e8904151d427fe52bc84342 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 5 May 2022 11:03:06 +0300 Subject: [PATCH] Added ToManyRelationship::getNthIdentifier() --- .../Relationships/ToManyRelationshipInterface.php | 6 ++++++ .../Relationships/ToManyRelationship.php | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/system/src/Grav/Framework/Contracts/Relationships/ToManyRelationshipInterface.php b/system/src/Grav/Framework/Contracts/Relationships/ToManyRelationshipInterface.php index 4ad5af1da..723bef690 100644 --- a/system/src/Grav/Framework/Contracts/Relationships/ToManyRelationshipInterface.php +++ b/system/src/Grav/Framework/Contracts/Relationships/ToManyRelationshipInterface.php @@ -13,6 +13,12 @@ use Grav\Framework\Contracts\Object\IdentifierInterface; */ interface ToManyRelationshipInterface extends RelationshipInterface { + /** + * @param positive-int $pos + * @return IdentifierInterface|null + */ + public function getNthIdentifier(int $pos): ?IdentifierInterface; + /** * @param string $id * @param string|null $type diff --git a/system/src/Grav/Framework/Relationships/ToManyRelationship.php b/system/src/Grav/Framework/Relationships/ToManyRelationship.php index 73dbc6cce..26b138e43 100644 --- a/system/src/Grav/Framework/Relationships/ToManyRelationship.php +++ b/system/src/Grav/Framework/Relationships/ToManyRelationship.php @@ -88,6 +88,21 @@ class ToManyRelationship implements ToManyRelationshipInterface return $this->getIdentifier($id, $type) !== null; } + /** + * @param positive-int $pos + * @return IdentifierInterface|null + */ + public function getNthIdentifier(int $pos): ?IdentifierInterface + { + $items = array_keys($this->identifiers); + $key = $items[$pos - 1] ?? null; + if (null === $key) { + return null; + } + + return $this->identifiers[$key] ?? null; + } + /** * @param string $id * @param string|null $type