1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\Traits; |
3
|
|
|
|
4
|
|
|
trait DatabaseTrait |
5
|
|
|
{ |
6
|
|
|
public function insert($tableName, $data = []) |
7
|
|
|
{ |
8
|
|
|
return $this->add(self::QUERY_TYPE_INSERT, $tableName, $data); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
public function replace($tableName, $data = []) |
12
|
|
|
{ |
13
|
|
|
return $this->add(self::QUERY_TYPE_REPLACE, $tableName, $data); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
protected function add($queryType, $tableName, $data = []) |
17
|
|
|
{ |
18
|
|
|
if (empty($tableName) || empty($data)) { |
19
|
|
|
throw new \ErrorException('No data specified'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$query = $this->generateAddQuery($queryType, $tableName, $data); |
23
|
|
|
|
24
|
|
|
return $this->executeQuery($query, $data); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function getKeysValues($data = []) |
28
|
|
|
{ |
29
|
|
|
$multiDimensional = is_array($data[key($data)]); |
30
|
|
|
if ($multiDimensional) { |
31
|
|
|
$keys = array_keys(call_user_func_array('array_merge', $data)); |
32
|
|
|
// fill any missing keys with empty data |
33
|
|
|
$key_pair = array_combine($keys, array_fill(0, count($keys), null)); |
34
|
|
|
$data = array_map(function ($e) use ($key_pair) { |
35
|
|
|
return array_merge($key_pair, $e); |
36
|
|
|
}, $data); |
37
|
|
|
} else { |
38
|
|
|
$keys = array_keys($data); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return [$keys, $data]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function generateAddQuery($queryType, $tableName, $data) |
45
|
|
|
{ |
46
|
|
|
$multiDimensional = is_array($data[key($data)]); |
47
|
|
|
|
48
|
|
|
list($keys, $data) = $this->getKeysValues($data); |
49
|
|
|
|
50
|
|
|
switch ($queryType) { |
51
|
|
|
case self::QUERY_TYPE_REPLACE: |
52
|
|
|
$query = self::QUERY_TYPE_REPLACE . ' INTO'; |
53
|
|
|
break; |
54
|
|
|
case self::QUERY_TYPE_INSERT_IGNORE: |
55
|
|
|
$query = self::QUERY_TYPE_INSERT_IGNORE . ' INTO'; |
56
|
|
|
break; |
57
|
|
|
case self::QUERY_TYPE_INSERT: |
58
|
|
|
default: |
59
|
|
|
$query = self::QUERY_TYPE_INSERT . ' INTO'; |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
$query .= ' '.$this->escapeIdentifier($tableName).' (' . |
|
|
|
|
63
|
|
|
implode(', ', array_map([$this, 'escapeIdentifier'], $keys)) . |
64
|
|
|
') VALUES'; |
65
|
|
|
if ($multiDimensional) { |
66
|
|
|
$valuesStrings = []; |
67
|
|
|
foreach ($data as $item) { |
68
|
|
|
$valuesStrings[] = $this->generateValuesString($item); |
69
|
|
|
} |
70
|
|
|
$query .= implode(', ', $valuesStrings); |
71
|
|
|
} else { |
72
|
|
|
$query .= $this->generateValuesString($data); |
73
|
|
|
} |
74
|
|
|
return $query; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function generateValuesString($data) |
78
|
|
|
{ |
79
|
|
|
return ' (' . implode(', ', array_map(function ($v) { |
|
|
|
|
80
|
|
|
return '?'; |
81
|
|
|
}, $data)) . ')'; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.