-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithubRepositoryList.php
More file actions
53 lines (46 loc) · 1.54 KB
/
GithubRepositoryList.php
File metadata and controls
53 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace Horde\GithubApiClient;
use ArrayIterator;
use IteratorAggregate;
use Traversable;
use OutOfBoundsException;
use Stringable;
/** @implements \IteratorAggregate<int, GithubRepository> */
class GithubRepositoryList implements IteratorAggregate
{
/**
* @var GithubRepository[]
*/
private array $repositories = [];
/**
* @param iterable<GithubRepository|array<string|Stringable|int|null>> $elements
*/
public function __construct(iterable $elements = [])
{
foreach ($elements as $element) {
if ($element instanceof GithubRepository) {
$this->repositories[$element->getFullName()] = $element;
} elseif (
is_array($element)
&& array_key_exists('name', $element)
&& array_key_exists('full_name', $element)
&& array_key_exists('clone_url', $element)) {
$repository = GithubRepository::fromApiArray($element);
$this->repositories[$repository->getFullName()] = $repository;
}
// TODO: Exception on inappropriate type
}
}
public function getRepositoryByFullName(string $fullName): GithubRepository
{
if (array_key_exists($fullName, $this->repositories)) {
return $this->repositories[$fullName];
}
throw new OutOfBoundsException("Repository not found");
}
public function getIterator(): Traversable
{
return new ArrayIterator($this->repositories);
}
}