Added ToManyRelationship::getNthIdentifier()

This commit is contained in:
Matias Griese 2022-05-05 11:03:06 +03:00
parent 1237f0a6d6
commit 32a486f1d4
2 changed files with 21 additions and 0 deletions

View File

@ -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

View File

@ -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