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

valkyrjaio / valkyrja / 15659546660

15 Jun 2025 04:39AM UTC coverage: 47.202% (-0.4%) from 47.589%
15659546660

push

github

MelechMizrachi
Update Config.

5331 of 11294 relevant lines covered (47.2%)

16.11 hits per line

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

0.0
/src/Valkyrja/Orm/Repository/RelationshipCapableRepository.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\Entity\Contract\Entity;
17
use Valkyrja\Type\BuiltIn\Support\StrCase;
18

19
/**
20
 * Trait RelationshipCapableRepository.
21
 *
22
 * @author Melech Mizrachi
23
 *
24
 * @template Entity of Entity
25
 */
26
trait RelationshipCapableRepository
27
{
28
    /**
29
     * The relationships to get with each result.
30
     *
31
     * @var string[]|null
32
     */
33
    protected array|null $relationships = null;
34

35
    /**
36
     * Whether to get relations.
37
     *
38
     * @var bool
39
     */
40
    protected bool $getRelations = false;
41

42
    /**
43
     * @inheritDoc
44
     */
45
    public function find(): static
46
    {
47
        $this->resetRelationships();
×
48

49
        parent::find();
×
50

51
        return $this;
×
52
    }
53

54
    /**
55
     * @inheritDoc
56
     */
57
    public function findOne(int|string $id): static
58
    {
59
        $this->resetRelationships();
×
60

61
        parent::findOne($id);
×
62

63
        return $this;
×
64
    }
65

66
    /**
67
     * @inheritDoc
68
     */
69
    public function count(): static
70
    {
71
        $this->resetRelationships();
×
72

73
        parent::count();
×
74

75
        return $this;
×
76
    }
77

78
    /**
79
     * @inheritDoc
80
     *
81
     * @param string[]|null $relationships [optional] The relationships to get
82
     *
83
     * @return static
84
     */
85
    public function withRelationships(array|null $relationships = null): static
86
    {
87
        $this->getRelations  = true;
×
88
        $this->relationships = $relationships;
×
89

90
        return $this;
×
91
    }
92

93
    /**
94
     * @inheritDoc
95
     *
96
     * @return static
97
     */
98
    public function withAllRelationships(): static
99
    {
100
        $this->getRelations  = true;
×
101
        $this->relationships = $this->entity::getRelationshipProperties();
×
102

103
        return $this;
×
104
    }
105

106
    /**
107
     * @inheritDoc
108
     *
109
     * @return static
110
     */
111
    public function withoutRelationships(): static
112
    {
113
        $this->resetRelationships();
×
114

115
        return $this;
×
116
    }
117

118
    /**
119
     * @inheritDoc
120
     *
121
     * @return Entity[]
122
     */
123
    public function getResult(): array
124
    {
125
        $results = parent::getResult();
×
126

127
        $this->setRelationshipsOnEntities(...$results);
×
128

129
        return $results;
×
130
    }
131

132
    /**
133
     * Reset the relationship properties.
134
     *
135
     * @return void
136
     */
137
    protected function resetRelationships(): void
138
    {
139
        $this->getRelations  = false;
×
140
        $this->relationships = null;
×
141
    }
142

143
    /**
144
     * Set relationships on the entities from results.
145
     *
146
     * @param Entity ...$entities The entities to add relationships to
147
     *
148
     * @return void
149
     */
150
    protected function setRelationshipsOnEntities(Entity ...$entities): void
151
    {
152
        $relationships = $this->relationships;
×
153

154
        if ($relationships === null || $relationships === [] || ! $this->getRelations || $entities === []) {
×
155
            return;
×
156
        }
157

158
        // Iterate through the rows found
159
        foreach ($entities as $entity) {
×
160
            // Get the entity relations
161
            $this->setRelationshipsOnEntity($relationships, $entity);
×
162
        }
163
    }
164

165
    /**
166
     * Set relationships on an entity.
167
     *
168
     * @param string[] $relationships The relationships to set
169
     * @param Entity   $entity        The entity
170
     *
171
     * @return void
172
     */
173
    protected function setRelationshipsOnEntity(array $relationships, Entity $entity): void
174
    {
175
        // Iterate through the rows found
176
        foreach ($relationships as $relationship) {
×
177
            // Set the entity relations
178
            $this->setRelationship($entity, $relationship);
×
179
        }
180
    }
181

182
    /**
183
     * Set a relationship property.
184
     *
185
     * @param Entity $entity       The entity
186
     * @param string $relationship The relationship to set
187
     *
188
     * @return void
189
     */
190
    protected function setRelationship(Entity $entity, string $relationship): void
191
    {
192
        $methodName = 'set' . StrCase::toStudlyCase($relationship) . 'Relationship';
×
193

194
        if (method_exists($this, $methodName)) {
×
195
            $this->$methodName($entity);
×
196
        }
197
    }
198
}
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