1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpWinTools\WmiScripting\Query; |
4
|
|
|
|
5
|
|
|
use PhpWinTools\WmiScripting\Models\Win32Model; |
6
|
|
|
use PhpWinTools\WmiScripting\Connections\Connection; |
7
|
|
|
use PhpWinTools\WmiScripting\Collections\ModelCollection; |
8
|
|
|
use PhpWinTools\WmiScripting\Support\ApiObjects\Contracts\ObjectSet; |
9
|
|
|
|
10
|
|
|
class Builder |
11
|
|
|
{ |
12
|
|
|
protected $model; |
13
|
|
|
|
14
|
|
|
protected $services; |
15
|
|
|
|
16
|
|
|
protected $connection; |
17
|
|
|
|
18
|
|
|
protected $from; |
19
|
|
|
|
20
|
|
|
protected $selects = ['*']; |
21
|
|
|
|
22
|
|
|
protected $wheres = []; |
23
|
|
|
|
24
|
|
|
protected $relationships = []; |
25
|
|
|
|
26
|
|
|
public function __construct(Win32Model $model, Connection $connection) |
27
|
|
|
{ |
28
|
|
|
$this->model = $model; |
29
|
|
|
$this->connection = $connection; |
30
|
|
|
$this->from = $model->getWmiClassNameAttribute(); |
31
|
|
|
$this->services = $connection->connect(); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function all() |
35
|
|
|
{ |
36
|
|
|
return $this->hydrate($this->query("select * from {$this->from}")); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function hydrate(array $items = []) |
40
|
|
|
{ |
41
|
|
|
return new ModelCollection(array_map(function ($item) { |
42
|
|
|
return $this->model::newInstance($item); |
43
|
|
|
}, $items)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return ModelCollection|Win32Model[] |
48
|
|
|
*/ |
49
|
|
|
public function get() |
50
|
|
|
{ |
51
|
|
|
return $this->hydrate($this->query($this->queryString())); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $query |
56
|
|
|
* |
57
|
|
|
* @return mixed|ObjectSet |
58
|
|
|
*/ |
59
|
|
|
public function query($query) |
60
|
|
|
{ |
61
|
|
|
return $this->connection->query($query, $this->model, $this->relationships); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function queryString(): string |
68
|
|
|
{ |
69
|
|
|
return $this->compileQuery(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function select(array $columns = ['*']) |
73
|
|
|
{ |
74
|
|
|
$this->selects = $columns; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function where($column, $operator, $value) |
80
|
|
|
{ |
81
|
|
|
$this->wheres[] = [ |
82
|
|
|
'column' => $column, |
83
|
|
|
'operator' => $operator, |
84
|
|
|
'value' => $value, |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function with($relationship) |
91
|
|
|
{ |
92
|
|
|
if (is_array($relationship)) { |
93
|
|
|
$this->relationships = array_merge($this->relationships, $relationship); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->relationships[] = $relationship; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function compileQuery() |
104
|
|
|
{ |
105
|
|
|
$select = implode(', ', $this->selects); |
106
|
|
|
$query = "select {$select} from {$this->from}"; |
107
|
|
|
$wheres = []; |
108
|
|
|
|
109
|
|
|
foreach ($this->wheres as $where) { |
110
|
|
|
$wheres[] = "{$where['column']} {$where['operator']} " |
111
|
|
|
. (is_string($where['value']) ? "'{$where['value']}'" : $where['value']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return empty($wheres) ? $query : $query . ' where ' . implode(' and ', $wheres); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|