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

ICanBoogie / ActiveRecord / 6362433236

30 Sep 2023 11:14AM UTC coverage: 85.731% (+5.6%) from 80.178%
6362433236

push

github

olvlvl
Rename StaticModelProvider methods

1436 of 1675 relevant lines covered (85.73%)

29.41 hits per line

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

92.5
/lib/ActiveRecord/RelationCollection.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\ActiveRecord;
13

14
use ArrayAccess;
15
use Closure;
16
use ICanBoogie\ActiveRecord;
17
use ICanBoogie\OffsetNotWritable;
18

19
use function is_string;
20

21
/**
22
 * Relation collection of a model.
23
 *
24
 * @implements ArrayAccess<string, Relation>
25
 *     Where _key_ is a getter name e.g. 'comments'
26
 */
27
class RelationCollection implements ArrayAccess
28
{
29
    /**
30
     * Relations.
31
     *
32
     * @var array<string, Relation>
33
     *     Where _key_ is a getter name e.g. 'comments'
34
     */
35
    private array $relations;
36

37
    public function __construct(
38
        public readonly Model $model,
39
        ?ActiveRecord\Config\Association $association,
40
    ) {
41
        $this->apply_association($association);
82✔
42
    }
43

44
    private function apply_association(?ActiveRecord\Config\Association $association): void
45
    {
46
        if (!$association) {
82✔
47
            return;
2✔
48
        }
49

50
        foreach ($association->belongs_to as $r) {
82✔
51
            $this->belongs_to(
69✔
52
                related: $r->associate,
69✔
53
                local_key: $r->local_key,
69✔
54
                foreign_key: $r->foreign_key,
69✔
55
                as: $r->as,
69✔
56
            );
69✔
57
        }
58

59
        foreach ($association->has_many as $r) {
82✔
60
            $this->has_many(
70✔
61
                related: $r->associate,
70✔
62
                foreign_key: $r->foreign_key,
70✔
63
                as: $r->as,
70✔
64
                through: $r->through,
70✔
65
            );
70✔
66
        }
67
    }
68

69
    /**
70
     * Checks if a relation exists.
71
     *
72
     * @param string $offset Relation name.
73
     */
74
    public function offsetExists(mixed $offset): bool
75
    {
76
        return isset($this->relations[$offset]);
2✔
77
    }
78

79
    /**
80
     * Returns a relation.
81
     *
82
     * @param string $offset Relation name.
83
     *
84
     * @throws RelationNotDefined if the relation is not defined.
85
     */
86
    public function offsetGet(mixed $offset): Relation
87
    {
88
        return $this->relations[$offset]
3✔
89
            ?? throw new RelationNotDefined($offset, $this);
3✔
90
    }
91

92
    /**
93
     * @throws OffsetNotWritable because relations cannot be set.
94
     */
95
    public function offsetSet(mixed $offset, mixed $value): void
96
    {
97
        throw new OffsetNotWritable([ $offset, $this ]);
×
98
    }
99

100
    /**
101
     * @throws OffsetNotWritable because relations cannot be unset.
102
     */
103
    public function offsetUnset(mixed $offset): void
104
    {
105
        throw new OffsetNotWritable([ $offset, $this ]);
×
106
    }
107

108
    /**
109
     * Adds a {@link BelongsToRelation} relation.
110
     *
111
     * @param class-string<ActiveRecord> $related
112
     * @param non-empty-string $local_key
113
     * @param non-empty-string $foreign_key
114
     * @param non-empty-string $as
115
     */
116
    public function belongs_to(
117
        string $related,
118
        string $local_key,
119
        string $foreign_key,
120
        string $as,
121
    ): void {
122
        $this->relations[$as] = new BelongsToRelation(
69✔
123
            owner: $this->model,
69✔
124
            related: $related,
69✔
125
            local_key: $local_key,
69✔
126
            foreign_key: $foreign_key,
69✔
127
            as: $as,
69✔
128
        );
69✔
129
    }
130

131
    /**
132
     * Adds a {@link HasManyRelation} relation.
133
     *
134
     * @param class-string<ActiveRecord> $related
135
     * @param non-empty-string $foreign_key
136
     * @param non-empty-string $as
137
     * @param class-string<ActiveRecord>|null $through
138
     */
139
    public function has_many(
140
        string $related,
141
        string $foreign_key,
142
        string $as,
143
        ?string $through = null,
144
    ): void {
145
        assert(is_string($this->model->primary));
146

147
        $this->relations[$as] = new HasManyRelation(
70✔
148
            owner: $this->model,
70✔
149
            related: $related,
70✔
150
            foreign_key: $foreign_key,
70✔
151
            as: $as,
70✔
152
            through: $through,
70✔
153
        );
70✔
154
    }
155

156
    /**
157
     * @param (Closure(Relation, string $as): bool) $predicate
158
     */
159
    public function find(Closure $predicate): ?Relation
160
    {
161
        foreach ($this->relations as $as => $relation) {
3✔
162
            if ($predicate($relation, $as)) {
3✔
163
                return $relation;
3✔
164
            }
165
        }
166

167
        return null;
×
168
    }
169
}
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