-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClassRepository.php
More file actions
98 lines (86 loc) · 2.21 KB
/
ClassRepository.php
File metadata and controls
98 lines (86 loc) · 2.21 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
/*
* UserFrosting Framework (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/framework
* @copyright Copyright (c) 2013-2024 Alexander Weissman, Louis Charette, Jordan Mele
* @license https://github.com/userfrosting/framework/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Support;
use ArrayIterator;
use Countable;
use Iterator;
use IteratorAggregate;
use UserFrosting\Support\Exception\ClassNotFoundException;
/**
* Handle a PHP class repository.
*
* @template T of object
*
* @implements ClassRepositoryInterface<T>
*/
abstract class ClassRepository implements ClassRepositoryInterface
{
/**
* Return all classes.
*
* @return T[] A list of classes instances.
*/
abstract public function all(): array;
/**
* Returns the same list as all, but as a list of class names.
*
* @return class-string[] A list class FQN.
*/
public function list(): array
{
return array_map(function ($m) {
return get_class($m);
}, $this->all());
}
/**
* Return the requested class instance from the repository.
*
* @param class-string $class Class FQN.
*
* @return T
*/
public function get(string $class): object
{
if (!$this->has($class)) {
throw new ClassNotFoundException("Class `$class` not found.");
}
$results = array_filter($this->all(), function ($m) use ($class) {
return get_class($m) === $class;
});
return array_values($results)[0]; // TODO : Test array_values with filter not being on key 0
}
/**
* Validate if a specific class exist.
*
* @param class-string $class Class FQN.
*
* @return bool
*/
public function has(string $class): bool
{
return in_array($class, $this->list(), true);
}
/**
* Countable implementation.
*/
public function count(): int
{
return count($this->all());
}
/**
* IteratorAggregate implementation.
*
* @return Iterator<int, T>
*/
public function getIterator(): Iterator
{
return new ArrayIterator($this->all());
}
}