Passed
Push — sudav3 ( 2da71b...bd404a )
by 世昌
02:46
created

ReadStatement::where()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
namespace suda\orm\struct;
3
4
use suda\orm\TableAccess;
5
use suda\orm\TableStruct;
6
7
class ReadStatement extends \suda\orm\statement\ReadStatement
8
{
9
    /**
10
     * 访问操作
11
     *
12
     * @var TableAccess
13
     */
14
    protected $access;
15
16
    public function __construct(TableAccess $access)
17
    {
18
        $this->access = $access;
19
        parent::__construct(
20
            $access->getSource()->write()->rawTableName($access->getStruct()->getName()),
21
            $access->getStruct()
22
        );
23
    }
24
25
26
    /**
27
     * 取1
28
     *
29
     * @param string|null $class
30
     * @param array $args
31
     * @return mixed
32
     */
33
    public function one(?string $class = null, array $args = [])
34
    {
35
        return $this->access->run($this->wantOne($class, $args));
36
    }
37
  
38
    /**
39
     * 取全部
40
     *
41
     * @param string|null $class
42
     * @param array $args
43
     * @return array
44
     */
45
    public function all(?string $class = null, array $args = []):array
46
    {
47
        return $this->access->run($this->wantAll($class, $args));
48
    }
49
  
50
    /**
51
     * 取1
52
     *
53
     * @param string|null $class
54
     * @param array $args
55
     * @return mixed
56
     */
57
    public function fetch(?string $class = null, array $args = [])
58
    {
59
        return $this->one($class, $args);
60
    }
61
  
62
    /**
63
     * 取全部
64
     *
65
     * @param string|null $class
66
     * @param array $args
67
     * @return array
68
     */
69
    public function fetchAll(?string $class = null, array $args = []):array
70
    {
71
        return $this->all($class, $args);
72
    }
73
74
    /**
75
     * 条件
76
     *
77
     * @param string|array $where
78
     * @param array $whereBinder
79
     * @return self
80
     */
81
    public function where($where, ...$args)
82
    {
83
        if (\is_array($where)) {
84
            $where = $this->aliasKeyField($where);
85
            $this->whereArray($where, $args[0] ?? []);
86
        } elseif (is_array($args[0])) {
87
            $this->whereStringArray($where, $args[0]);
88
        } else {
89
            list($string, $array) = $this->prepareQueryMark($where, $args);
90
            $this->whereStringArray($string, $array);
91
        }
92
        return $this;
93
    }
94
95
    /**
96
     * 处理输入的键
97
     *
98
     * @param array $fields
99
     * @return array
100
     */
101
    protected function aliasKeyField(array $fields)
102
    {
103
        $values = [];
104
        foreach ($fields as $name => $value) {
105
            $index = $this->access->getMiddleware()->inputName($name);
106
            $values[$index] = $value;
107
        }
108
        return $values;
109
    }
110
111
    /**
112
     * Get 访问操作
113
     *
114
     * @return  TableAccess
115
     */
116
    public function getAccess():TableAccess
117
    {
118
        return $this->access;
119
    }
120
}
121