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

valkyrjaio / valkyrja / 12764367500

14 Jan 2025 09:06AM UTC coverage: 47.411% (+0.07%) from 47.338%
12764367500

push

github

MelechMizrachi
Adding more specific array types.

24 of 68 new or added lines in 20 files covered. (35.29%)

13 existing lines in 8 files now uncovered.

5174 of 10913 relevant lines covered (47.41%)

19.07 hits per line

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

0.0
/src/Valkyrja/Orm/Query/Query.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\Query;
15

16
use stdClass;
17
use Valkyrja\Orm\Adapter\Contract\Adapter;
18
use Valkyrja\Orm\Entity\Contract\Entity;
19
use Valkyrja\Orm\Exception\NotFoundException;
20
use Valkyrja\Orm\Query\Contract\Query as QueryContract;
21
use Valkyrja\Orm\Statement\Contract\Statement;
22
use Valkyrja\Orm\Support\Helpers;
23

24
use function assert;
25
use function is_array;
26

27
/**
28
 * Class Query.
29
 *
30
 * @author Melech Mizrachi
31
 */
32
class Query implements QueryContract
33
{
34
    /**
35
     * The statement.
36
     *
37
     * @var Statement
38
     */
39
    protected Statement $statement;
40

41
    /**
42
     * The table to query on.
43
     *
44
     * @var string|null
45
     */
46
    protected string|null $table = null;
47

48
    /**
49
     * The entity to query with.
50
     *
51
     * @var class-string<Entity>|null
52
     */
53
    protected string|null $entity = null;
54

55
    /**
56
     * Query constructor.
57
     *
58
     * @param Adapter $adapter
59
     */
60
    public function __construct(
61
        protected Adapter $adapter
62
    ) {
UNCOV
63
    }
×
64

65
    /**
66
     * @inheritDoc
67
     */
68
    public function table(string $table): static
69
    {
70
        $this->table = $table;
×
71

72
        return $this;
×
73
    }
74

75
    /**
76
     * @inheritDoc
77
     */
78
    public function entity(string $entity): static
79
    {
80
        assert(is_a($entity, Entity::class, true));
×
81

82
        $this->entity = $entity;
×
83

84
        return $this;
×
85
    }
86

87
    /**
88
     * @inheritDoc
89
     */
90
    public function prepare(string $query): static
91
    {
NEW
92
        $entity = $this->entity;
×
93

NEW
94
        if ($entity !== null) {
×
UNCOV
95
            $query = str_replace($entity, $entity::getTableName(), $query);
×
96
        }
97

98
        $this->statement = $this->adapter->prepare($query);
×
99

100
        if ($this->table !== null) {
×
101
            $this->bindValue('table', $this->table);
×
102
        }
103

104
        return $this;
×
105
    }
106

107
    /**
108
     * @inheritDoc
109
     */
110
    public function bindValue(string $property, mixed $value): static
111
    {
112
        if (is_array($value)) {
×
113
            foreach ($value as $key => $item) {
×
114
                $this->bindValue($property . $key, $item);
×
115
            }
116

117
            return $this;
×
118
        }
119

120
        // And bind each value to the column
121
        $this->statement->bindValue(Helpers::getColumnForValueBind($property), $value);
×
122

123
        return $this;
×
124
    }
125

126
    /**
127
     * @inheritDoc
128
     */
129
    public function execute(): bool
130
    {
131
        return $this->statement->execute();
×
132
    }
133

134
    /**
135
     * @inheritDoc
136
     */
137
    public function getResult(): array
138
    {
139
        $results = $this->statement->fetchAll();
×
140

141
        /** @var class-string<Entity>|null $entity */
142
        $entity = $this->entity;
×
143

144
        // If there is no entity specified just return the results
145
        if ($entity === null) {
×
146
            return array_map(
×
147
                static function (array $data): stdClass {
×
148
                    /** @var stdClass $object */
149
                    $object = (object) $data;
×
150

151
                    return $object;
×
152
                },
×
153
                $results
×
154
            );
×
155
        }
156

157
        return array_map(
×
158
            static fn (array $data): Entity => $entity::fromArray($data),
×
159
            $results
×
160
        );
×
161
    }
162

163
    /**
164
     * @inheritDoc
165
     */
166
    public function getOneOrNull(): Entity|stdClass|null
167
    {
168
        return $this->getResult()[0] ?? null;
×
169
    }
170

171
    /**
172
     * @inheritDoc
173
     */
174
    public function getOneOrFail(): object
175
    {
176
        $results = $this->getOneOrNull();
×
177

178
        if ($results === null) {
×
179
            throw new NotFoundException('Result Not Found');
×
180
        }
181

182
        return $results;
×
183
    }
184

185
    /**
186
     * @inheritDoc
187
     */
188
    public function getCount(): int
189
    {
190
        $results = $this->statement->fetchAll();
×
191

192
        return (int) ($results[0]['COUNT(*)'] ?? $results[0]['count'] ?? 0);
×
193
    }
194

195
    /**
196
     * @inheritDoc
197
     */
198
    public function getError(): string
199
    {
200
        return $this->statement->errorMessage() ?? 'An unknown error occurred.';
×
201
    }
202
}
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