1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace PhpBoot\DB\rules\update; |
3
|
|
|
use PhpBoot\DB\Context; |
4
|
|
|
use PhpBoot\DB\rules\basic\BasicRule; |
5
|
|
|
use PhpBoot\DB\rules\basic\WhereRule; |
6
|
|
|
use PhpBoot\DB\impls\UpdateSetImpl; |
7
|
|
|
use PhpBoot\DB\impls\UpdateImpl; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
require_once dirname(__DIR__).'/impls.php'; |
11
|
|
|
require_once __DIR__.'/basic.php'; |
12
|
|
|
|
13
|
|
|
class UpdateRule extends BasicRule |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* update('table')->set('a', 1) => "UPDATE table SET a=1" |
17
|
|
|
* @param string $table |
18
|
|
|
* @return \PhpBoot\DB\rules\update\UpdateSetRule |
19
|
|
|
*/ |
20
|
11 |
|
public function update($table) { |
21
|
11 |
|
UpdateImpl::update($this->context, $table); |
22
|
11 |
|
return new UpdateSetRule($this->context); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
class UpdateSetRule extends BasicRule |
|
|
|
|
27
|
|
|
{ |
28
|
11 |
|
public function __construct($context){ |
29
|
11 |
|
parent::__construct($context); |
30
|
11 |
|
$this->impl = new UpdateSetImpl(); |
31
|
11 |
|
} |
32
|
|
|
/** |
33
|
|
|
* update('table')->set(['a'=>1]) => "UPDATE table SET a=1" |
34
|
|
|
|
35
|
|
|
* update('table')->set('a=?',1) => "UPDATE table SET a=1" |
36
|
|
|
* @param array|string $expr |
37
|
|
|
* @param mixed $_ |
38
|
|
|
* @return UpdateSetWhereRule |
39
|
|
|
*/ |
40
|
11 |
|
public function set($expr, $_=null) { |
|
|
|
|
41
|
11 |
|
$this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
42
|
11 |
|
return new UpdateSetWhereRule($this->context, $this->impl); |
43
|
|
|
} |
44
|
|
|
private $impl; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
class UpdateSetWhereRule extends WhereRule |
|
|
|
|
48
|
|
|
{ |
49
|
11 |
|
public function __construct(Context $context, UpdateSetImpl $impl){ |
50
|
11 |
|
parent::__construct($context); |
51
|
11 |
|
$this->impl = $impl; |
52
|
11 |
|
} |
53
|
|
|
/** |
54
|
|
|
* update('table')->set(['a'=>1]) => "UPDATE table SET a=1" |
55
|
|
|
|
56
|
|
|
* update('table')->set('a=?',1) => "UPDATE table SET a=1" |
57
|
|
|
* @param array|string $expr |
58
|
|
|
* @param mixed $_ |
59
|
|
|
* @return WhereRule |
60
|
|
|
*/ |
61
|
|
|
public function set($expr, $_=null) { |
|
|
|
|
62
|
|
|
$this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
63
|
|
|
return new UpdateSetWhereRule($this->context, $this->impl); |
64
|
|
|
} |
65
|
|
|
private $impl; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.