|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Scrawler package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Pranjal Pandey <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Scrawler\Arca\Manager; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Connection; |
|
14
|
|
|
use Ramsey\Uuid\Uuid; |
|
15
|
|
|
use Scrawler\Arca\Collection; |
|
16
|
|
|
use Scrawler\Arca\Config; |
|
17
|
|
|
use Scrawler\Arca\Model; |
|
18
|
|
|
use Scrawler\Arca\QueryBuilder; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class responsible for manging single records. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class RecordManager |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Create RecordManager. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct( |
|
29
|
|
|
private readonly Connection $connection, |
|
30
|
|
|
private readonly ModelManager $modelManager, |
|
31
|
|
|
private readonly Config $config, |
|
32
|
|
|
) { |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a new record. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function insert(Model $model): mixed |
|
39
|
|
|
{ |
|
40
|
|
|
if ($this->config->isUsingUUID()) { |
|
41
|
|
|
$model->set('id', Uuid::uuid4()->toString()); |
|
42
|
|
|
} |
|
43
|
|
|
$this->connection->insert($model->getName(), $model->getSelfProperties()); |
|
44
|
|
|
if ($this->config->isUsingUUID()) { |
|
45
|
|
|
return $model->get('id'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return (int) $this->connection->lastInsertId(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Update a record. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function update(Model $model): mixed |
|
55
|
|
|
{ |
|
56
|
|
|
$this->connection->update($model->getName(), $model->getSelfProperties(), ['id' => $model->getId()]); |
|
57
|
|
|
|
|
58
|
|
|
return $model->getId(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Delete a record. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function delete(Model $model): mixed |
|
65
|
|
|
{ |
|
66
|
|
|
$this->connection->delete($model->getName(), ['id' => $model->getId()]); |
|
67
|
|
|
|
|
68
|
|
|
return $model->getId(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get single record by id. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getById(string $table, mixed $id): ?Model |
|
75
|
|
|
{ |
|
76
|
|
|
$query = (new QueryBuilder($this->connection, $this->modelManager)) |
|
77
|
|
|
->select('*') |
|
78
|
|
|
->from($table, 't') |
|
79
|
|
|
->where('t.id = ?') |
|
80
|
|
|
->setParameter(0, $id); |
|
81
|
|
|
|
|
82
|
|
|
return $query->first(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get all records. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getAll(string $tableName): Collection |
|
89
|
|
|
{ |
|
90
|
|
|
return (new QueryBuilder($this->connection, $this->modelManager)) |
|
91
|
|
|
->select('*') |
|
92
|
|
|
->from($tableName, 't') |
|
93
|
|
|
->get(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* get query builder from db. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function find(string $name): QueryBuilder |
|
100
|
|
|
{ |
|
101
|
|
|
return (new QueryBuilder($this->connection, $this->modelManager)) |
|
102
|
|
|
->select('*') |
|
103
|
|
|
->from($name, 't'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* get query builder from db. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function select(string $expression): QueryBuilder |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
return (new QueryBuilder($this->connection, $this->modelManager)); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.