-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveQuery.php
More file actions
54 lines (43 loc) · 1.24 KB
/
ActiveQuery.php
File metadata and controls
54 lines (43 loc) · 1.24 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
<?php
declare(strict_types=1);
namespace Cycle\ActiveRecord\Query;
use Cycle\ActiveRecord\Attribute\Collection;
use Cycle\ActiveRecord\Facade;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Select;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionException;
/**
* @template-covariant TEntity of object
*
* @extends Select<TEntity>
*/
class ActiveQuery extends Select
{
protected ORMInterface $orm;
/**
* @param class-string<TEntity> $class
*/
final public function __construct(protected string $class)
{
$this->orm = Facade::getOrm();
parent::__construct($this->orm, $class);
}
/**
* @throws ReflectionException
*/
public function fetchAll(): iterable
{
$reflection = new ReflectionClass(static::class);
// $reflection = new ReflectionClass($this->class);
$attributes = $reflection->getAttributes(Collection::class, ReflectionAttribute::IS_INSTANCEOF);
dd($attributes);
if ([] === $attributes) {
return parent::fetchAll();
}
$attribute = $attributes[0]->newInstance();
$collection = $this->orm->getFactory()->collection($attribute->name);
return $collection->collect($this->getIterator());
}
}