1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* m'Manager | Invoices Management System |
4
|
|
|
* |
5
|
|
|
* This content is released under the Proprietary License (Proprietary) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2017, Eric Claver AKAFFOU - All Rights Reserved |
8
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited |
9
|
|
|
* Proprietary and confidential |
10
|
|
|
* |
11
|
|
|
* @package m'Manager |
12
|
|
|
* @author Eric Claver AKAFFOU |
13
|
|
|
* @copyright Copyright (c) 2017, on'Eric Computing, Inc. (https://www.onericcomputing.com/) |
14
|
|
|
* @license https://www.mmanager.fr Proprietary License |
15
|
|
|
* @link https://codecanyon.net/item/mmanager-invoices-management-system/19866435?s_rank=1 |
16
|
|
|
* @since Version 1.0.0 |
17
|
|
|
* @filesource |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Mmanager\Persistence; |
21
|
|
|
|
22
|
|
|
use Mmanager\Domain\Repository\Contract\QueryInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Abstract Repository Class |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractQuery implements QueryInterface |
28
|
|
|
{ |
29
|
|
|
protected $table; |
30
|
|
|
protected $primaryKey = 'id'; |
31
|
|
|
protected $returnType = 'array'; |
32
|
|
|
protected $useSoftDeletes = false; |
33
|
|
|
protected $useTimestamps = false; |
34
|
|
|
protected $dateFormat = 'datetime'; |
35
|
|
|
protected $createdField = 'created_at'; |
36
|
|
|
protected $updatedField = 'updated_at'; |
37
|
|
|
protected $tempUseSoftDeletes; |
38
|
|
|
protected $deletedField = 'deleted'; |
39
|
|
|
protected $tempReturnType; |
40
|
|
|
protected $protectFields = true; |
41
|
|
|
protected $validationRules = []; |
42
|
|
|
protected $validationMessages = []; |
43
|
|
|
protected $skipValidation = false; |
44
|
|
|
protected $beforeInsert = []; |
45
|
|
|
protected $afterInsert = []; |
46
|
|
|
protected $beforeUpdate = []; |
47
|
|
|
protected $afterUpdate = []; |
48
|
|
|
protected $afterFind = []; |
49
|
|
|
protected $afterDelete = []; |
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @var object |
52
|
|
|
*/ |
53
|
|
|
protected $builder; |
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* Query Builder Interface |
56
|
|
|
* @param object $builder |
57
|
|
|
* @return mixed |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public function __construct($builder) { |
60
|
|
|
$this->builder = $builder; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function findAll() { |
64
|
|
|
$table = $this->findTableBy($this->table); |
65
|
|
|
return $this->builder->db->query("SELECT * FROM {$table}")->result_object(); |
66
|
|
|
} |
67
|
|
|
public function setDate($userDate = null) { |
68
|
|
|
switch ($this->dateFormat) { |
69
|
|
|
case 'int': |
70
|
|
|
return $this->_parseDate($userDate); |
71
|
|
|
case 'datetime': |
72
|
|
|
return date('Y-m-d H:i:s', $this->_parseDate($userDate)); |
73
|
|
|
case 'date': |
74
|
|
|
return date('Y-m-d', $this->_parseDate($userDate)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getTable() { |
79
|
|
|
return $this->findTableBy($this->table); |
80
|
|
|
} |
81
|
|
|
public function setTable($table) { |
82
|
|
|
$this->table = $table; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
/** |
|
|
|
|
86
|
|
|
* Find Table by key |
87
|
|
|
* @param string $key |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function findTableBy($key) { |
|
|
|
|
91
|
|
|
$return = ''; |
92
|
|
|
foreach ($this->_tableArray() as $k => $table) { |
93
|
|
|
if ($k === $this->_parseTableKey($key) || |
94
|
|
|
$k.'s' === $this->_parseTableKey($key)) { |
95
|
|
|
$return = $table; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
return $return; |
99
|
|
|
} |
100
|
|
|
/** |
101
|
|
|
* Helper function to parse table key |
102
|
|
|
* @param string $key |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function _parseTableKey($key) { |
106
|
|
|
return strtolower($key); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Helper function to get all table |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
protected function _tableArray() { |
114
|
|
|
return include dirname(__DIR__).'/Config/tables.config.php'; |
115
|
|
|
} |
116
|
|
|
protected function isValidQueryBuilder($builder) { |
117
|
|
|
if ( ! $builder instanceof QueryInterface) { |
118
|
|
|
throw new \InvalidArgumentException("Constructor parameter is not valid", 1); |
119
|
|
|
} |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
public function query($sql, $binds = FALSE, $return_object = NULL) { |
123
|
|
|
return $this->builder->db->query($sql, $binds = FALSE, $return_object = NULL); |
124
|
|
|
} |
125
|
|
|
private function _parseDate($date) { |
126
|
|
|
return is_numeric($date) ? (int) $date : time(); |
127
|
|
|
} |
128
|
|
|
} |