1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of UnderQuery package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
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 Phuria\UnderQuery; |
13
|
|
|
|
14
|
|
|
use Interop\Container\ContainerInterface; |
15
|
|
|
use Phuria\UnderQuery\Connection\ConnectionInterface; |
16
|
|
|
use Phuria\UnderQuery\DependencyInjection\ContainerFactory; |
17
|
|
|
use Phuria\UnderQuery\QueryBuilder\DeleteBuilder; |
18
|
|
|
use Phuria\UnderQuery\QueryBuilder\InsertBuilder; |
19
|
|
|
use Phuria\UnderQuery\QueryBuilder\InsertSelectBuilder; |
20
|
|
|
use Phuria\UnderQuery\QueryBuilder\QueryBuilderFacade; |
21
|
|
|
use Phuria\UnderQuery\QueryBuilder\SelectBuilder; |
22
|
|
|
use Phuria\UnderQuery\QueryBuilder\UpdateBuilder; |
23
|
|
|
use Phuria\UnderQuery\QueryCompiler\QueryCompilerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class UnderQuery |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ContainerInterface $container |
32
|
|
|
*/ |
33
|
|
|
private $container; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ConnectionInterface|null $connection |
37
|
|
|
* @param ContainerInterface|null $container |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct(ConnectionInterface $connection = null, ContainerInterface $container = null) |
40
|
|
|
{ |
41
|
2 |
|
$this->container = $container ?: (new ContainerFactory())->create(); |
42
|
2 |
|
$this->connection = $connection; |
|
|
|
|
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return ContainerInterface |
47
|
|
|
*/ |
48
|
2 |
|
public function getContainer() |
49
|
|
|
{ |
50
|
2 |
|
return $this->container; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $class |
55
|
|
|
* |
56
|
|
|
* @return QueryCompilerInterface |
57
|
|
|
*/ |
58
|
1 |
|
private function createQueryBuilder($class) |
59
|
|
|
{ |
60
|
1 |
|
return new $class(new QueryBuilderFacade( |
61
|
1 |
|
$this->getContainer()->get('phuria.under_query.table_factory'), |
62
|
1 |
|
$this->getContainer()->get('phuria.under_query.query_compiler'), |
63
|
1 |
|
$this->connection |
64
|
1 |
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return SelectBuilder |
|
|
|
|
69
|
|
|
*/ |
70
|
1 |
|
public function createSelect() |
71
|
|
|
{ |
72
|
1 |
|
return $this->createQueryBuilder(SelectBuilder::class); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return UpdateBuilder |
|
|
|
|
77
|
|
|
*/ |
78
|
1 |
|
public function createUpdate() |
79
|
|
|
{ |
80
|
1 |
|
return $this->createQueryBuilder(UpdateBuilder::class); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return DeleteBuilder |
|
|
|
|
85
|
|
|
*/ |
86
|
1 |
|
public function createDelete() |
87
|
|
|
{ |
88
|
1 |
|
return $this->createQueryBuilder(DeleteBuilder::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return InsertBuilder |
|
|
|
|
93
|
|
|
*/ |
94
|
1 |
|
public function createInsert() |
95
|
|
|
{ |
96
|
1 |
|
return $this->createQueryBuilder(InsertBuilder::class); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return InsertSelectBuilder |
|
|
|
|
101
|
|
|
*/ |
102
|
1 |
|
public function createInsertSelect() |
103
|
|
|
{ |
104
|
1 |
|
return $this->createQueryBuilder(InsertSelectBuilder::class); |
105
|
|
|
} |
106
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: