-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueryObjectContextAwareManager.php
More file actions
53 lines (39 loc) · 1.21 KB
/
QueryObjectContextAwareManager.php
File metadata and controls
53 lines (39 loc) · 1.21 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
<?php declare(strict_types = 1);
namespace Contributte\Nextras\Orm\QueryObject;
use Contributte\Nextras\Orm\QueryObject\Exception\InvalidObjectCreationException;
use Nette\DI\Container;
use Nextras\Dbal\Connection;
use Nextras\Dbal\Result\Result;
class QueryObjectContextAwareManager implements QueryObjectManager
{
protected Container $context;
public function __construct(Container $context)
{
$this->context = $context;
}
public function fetch(QueryObject $queryObject): Result
{
/** @var Connection $connection */
$connection = $this->context->getByType(Connection::class);
$qb = $queryObject->fetch($connection->createQueryBuilder());
$result = $connection->queryArgs(
$qb->getQuerySql(),
$qb->getQueryParameters()
);
if ($queryObject instanceof ExecutableQueryObject) {
$result = $queryObject->postResult($result);
}
return $result;
}
/**
* @param class-string $class
*/
public function create(string $class): QueryObject
{
$obj = $this->context->getByType($class);
if (!($obj instanceof QueryObject)) {
throw new InvalidObjectCreationException(sprintf('Created object must be typed of %s, type of %s given.', QueryObject::class, $obj::class));
}
return $obj;
}
}