-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilterTrait.php
executable file
·49 lines (41 loc) · 1.19 KB
/
FilterTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace AE\Stdlib;
trait FilterTrait
{
public function __construct()
{
}
public function toValues()
{
$filterArray = array_filter(get_object_vars($this));
return array_values($filterArray);
}
public function getFilterNames()
{
$filterArray = get_object_vars($this);
$filterNames = array_keys(array_filter($filterArray));
$sqlFragments = [];
foreach($filterNames as $filter) {
$sqlFragments[] = '' . $filter . '=? ';
}
$sql = implode(" AND ", $sqlFragments);
return $sql;
}
public function getFilterNamesAsDbColumns()
{
$filterArray = get_object_vars($this);
$filterNames = array_keys(array_filter($filterArray));
$sqlFragments = [];
foreach($filterNames as $filter) {
$filter = strtolower(implode("_", preg_split('/(?=[A-Z])/', $filter)));
$sqlFragments[] = '' . $filter . '=? ';
}
$sql = implode(" AND ", $sqlFragments);
return $sql;
}
// To Array, remove null values from filter
public function toArray()
{
return array_filter(get_object_vars($this));
}
}