• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

valkyrjaio / valkyrja / 16106580260

07 Jul 2025 03:04AM UTC coverage: 42.196% (+2.2%) from 40.004%
16106580260

push

github

MelechMizrachi
Orm: Restructuring and simplifying component.

13 of 452 new or added lines in 38 files covered. (2.88%)

9 existing lines in 3 files now uncovered.

3969 of 9406 relevant lines covered (42.2%)

10.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Valkyrja/Orm/Repository/Repository.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Orm\Repository;
15

16
use Valkyrja\Orm\Contract\Manager;
17
use Valkyrja\Orm\Data\Value;
18
use Valkyrja\Orm\Data\Where;
19
use Valkyrja\Orm\Entity\Contract\Entity;
20
use Valkyrja\Orm\Repository\Contract\Repository as Contract;
21

22
/**
23
 * Class Repository.
24
 *
25
 * @author Melech Mizrachi
26
 *
27
 * @template T of Entity
28
 *
29
 * @implements Contract<T>
30
 */
31
class Repository implements Contract
32
{
33
    /**
34
     * @param class-string<T> $entity
35
     */
36
    public function __construct(
37
        protected Manager $manager,
38
        protected string $entity,
39
    ) {
40
    }
×
41

42
    /**
43
     * @inheritDoc
44
     *
45
     * @return T|null
46
     */
47
    public function find(int|string $id): Entity|null
48
    {
49
        /** @var T $entity */
NEW
50
        $entity = $this->entity;
×
NEW
51
        $where  = new Where(
×
NEW
52
            value: new Value(
×
NEW
53
                name: $entity::getIdField(),
×
NEW
54
                value: $id
×
NEW
55
            ),
×
NEW
56
        );
×
57

58
        // TODO: Implement find() method.
59

NEW
60
        return $this->findBy($where);
×
61
    }
62

63
    /**
64
     * @inheritDoc
65
     *
66
     * @return T|null
67
     */
68
    public function findBy(Where ...$where): Entity|null
69
    {
NEW
70
        $table  = $this->entity::getTableName();
×
NEW
71
        $select = $this->manager->createQueryBuilder()->select($table);
×
NEW
72
        $select->withWhere(...$where);
×
73
        // TODO: Implement findBy() method.
74

NEW
75
        $statement = $this->manager->prepare((string) $select);
×
76

NEW
77
        return $this->mapResultsToEntity($statement->fetchAll())[0] ?? null;
×
78
    }
79

80
    /**
81
     * @inheritDoc
82
     *
83
     * @return T[]
84
     */
85
    public function all(): array
86
    {
NEW
87
        return $this->allBy();
×
88
    }
89

90
    /**
91
     * @inheritDoc
92
     *
93
     * @return T[]
94
     */
95
    public function allBy(Where ...$where): array
96
    {
NEW
97
        $table  = $this->entity::getTableName();
×
NEW
98
        $select = $this->manager->createQueryBuilder()->select($table);
×
NEW
99
        $select->withWhere(...$where);
×
100
        // TODO: Implement allBy() method.
101

NEW
102
        $statement = $this->manager->prepare((string) $select);
×
103

NEW
104
        return $this->mapResultsToEntity($statement->fetchAll());
×
105
    }
106

107
    /**
108
     * @inheritDoc
109
     *
110
     * @param T $entity The entity
111
     */
112
    public function create(Entity $entity): void
113
    {
NEW
114
        $table  = $entity::getTableName();
×
NEW
115
        $create = $this->manager->createQueryBuilder()->insert($table);
×
116
        // TODO: Implement create() method.
117

NEW
118
        $statement = $this->manager->prepare((string) $create);
×
119

NEW
120
        $this->manager->lastInsertId($table, $entity::getIdField());
×
121
    }
122

123
    /**
124
     * @inheritDoc
125
     *
126
     * @param T $entity The entity
127
     */
128
    public function update(Entity $entity): void
129
    {
NEW
130
        $table  = $entity::getTableName();
×
NEW
131
        $update = $this->manager->createQueryBuilder()->update($table);
×
132
        // TODO: Implement update() method.
133

NEW
134
        $this->manager->prepare((string) $update);
×
135
    }
136

137
    /**
138
     * @inheritDoc
139
     *
140
     * @param T $entity The entity
141
     */
142
    public function delete(Entity $entity): void
143
    {
NEW
144
        $table  = $entity::getTableName();
×
NEW
145
        $delete = $this->manager->createQueryBuilder()->delete($table);
×
146
        // TODO: Implement delete() method.
147

NEW
148
        $this->manager->prepare((string) $delete);
×
149
    }
150

151
    /**
152
     * @param array<string, mixed>[] $results The results
153
     *
154
     * @return T[]
155
     */
156
    protected function mapResultsToEntity(array $results): array
157
    {
158
        /** @var T $entity */
NEW
159
        $entity = $this->entity;
×
160

NEW
161
        return array_map(
×
NEW
162
            static fn (array $data): Entity => $entity::fromArray($data),
×
NEW
163
            $results
×
NEW
164
        );
×
165
    }
166
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc