These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * PotatoORM manages the persistence of database CRUD operations. |
||
4 | * |
||
5 | * @package Ibonly\PotatoORM\Model |
||
6 | * @author Ibraheem ADENIYI <[email protected]> |
||
7 | * @license MIT <https://opensource.org/licenses/MIT> |
||
8 | */ |
||
9 | |||
10 | namespace Ibonly\PotatoORM; |
||
11 | |||
12 | use PDO; |
||
13 | use Exception; |
||
14 | use PDOException; |
||
15 | use Ibonly\PotatoORM\GetData; |
||
16 | use Ibonly\PotatoORM\DatabaseQuery; |
||
17 | use Ibonly\PotatoORM\ModelInterface; |
||
18 | use Ibonly\PotatoORM\DataNotFoundException; |
||
19 | use Ibonly\PotatoORM\EmptyDatabaseException; |
||
20 | use Ibonly\PotatoORM\ColumnNotExistExeption; |
||
21 | use Ibonly\PotatoORM\DataAlreadyExistException; |
||
22 | use Ibonly\PotatoORM\InvalidConnectionException; |
||
23 | |||
24 | class Model extends DatabaseQuery implements ModelInterface |
||
25 | { |
||
26 | //Inject the inflector trait |
||
27 | use Inflector, Upload; |
||
28 | |||
29 | protected $ouput; |
||
30 | |||
31 | /** |
||
32 | * stripclassName() |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | public static function stripclassName() |
||
37 | { |
||
38 | $className = strtolower(get_called_class()); |
||
39 | $nameOfClass = explode("\\", $className); |
||
40 | |||
41 | return end($nameOfClass); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Get the table name if defined in the model |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function tableName() |
||
50 | { |
||
51 | if(isset($this->table)) { |
||
52 | $this->output = $this->table; |
||
53 | } else { |
||
54 | $this->output = null; |
||
55 | } |
||
56 | |||
57 | return $this->output; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get the fields to be fillables defined in the model |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function fields() |
||
66 | { |
||
67 | if (isset($this->fillables)) { |
||
68 | if (sizeof($this->fillables) > 0) { |
||
69 | $this->output = implode(", ", $this->fillables); |
||
70 | } else { |
||
71 | $this->output = '*'; |
||
72 | } |
||
73 | } else { |
||
74 | $this->output = '*'; |
||
75 | } |
||
76 | |||
77 | return $this->output; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * getClassName() |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getClassName() |
||
86 | { |
||
87 | if ($this->tableName() === null) { |
||
88 | $this->output = self::pluralize(self::stripclassName()); |
||
89 | } else { |
||
90 | $this->output = $this->tableName(); |
||
91 | } |
||
92 | |||
93 | return $this->output; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * getTableName() |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getTableName($connection) |
||
102 | { |
||
103 | return DatabaseQuery::checkTableName($this->getClassName(), $connection); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * getALL() |
||
108 | * Get all record from the database |
||
109 | * |
||
110 | * @return object |
||
111 | */ |
||
112 | public function getAll($dbConnection = NULL) |
||
113 | { |
||
114 | $connection = DatabaseQuery::checkConnection($dbConnection); |
||
115 | |||
116 | $sqlQuery = DatabaseQuery::selectAllQuery(self::getTableName($connection), self::fields()); |
||
117 | $query = $connection->prepare($sqlQuery); |
||
0 ignored issues
–
show
|
|||
118 | $query->execute(); |
||
119 | if ( $query->rowCount() ) |
||
120 | { |
||
121 | return new GetData($query->fetchAll($connection::FETCH_ASSOC)); |
||
122 | } |
||
123 | throw new EmptyDatabaseException(); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * where($data, $condition) |
||
128 | * Get data from database where $data = $condition |
||
129 | * |
||
130 | * @return object |
||
131 | */ |
||
132 | public function where($data, $condition = NULL, $dbConnection = NULL) |
||
133 | { |
||
134 | $databaseQuery = new DatabaseQuery(); |
||
135 | $connection = $databaseQuery->checkConnection($dbConnection); |
||
136 | |||
137 | $sqlQuery = $databaseQuery->selectQuery(self::getTableName($connection), self::fields(), $data, $condition, $connection); |
||
138 | $query = $connection->prepare($sqlQuery); |
||
0 ignored issues
–
show
|
|||
139 | $query->execute(); |
||
140 | if ( $query->rowCount() ) |
||
141 | { |
||
142 | return new GetData($query->fetchAll($connection::FETCH_ASSOC)); |
||
143 | } |
||
144 | throw new DataNotFoundException(); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * find($value) |
||
149 | * Find data from database where id = $value |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | public static function find($value, $dbConnection = NULL) |
||
154 | { |
||
155 | $connection = DatabaseQuery::checkConnection($dbConnection); |
||
156 | |||
157 | $sqlQuery = DatabaseQuery::selectQuery(self::getTableName($connection), self::fields(), ['id' => $value], NULL, $connection); |
||
158 | $query = $connection->prepare($sqlQuery); |
||
0 ignored issues
–
show
|
|||
159 | $query->execute(); |
||
160 | if ( $query->rowCount() ) |
||
161 | { |
||
162 | $found = new static; |
||
163 | $found->id = $value; |
||
164 | $found->data = $query->fetchAll($connection::FETCH_ASSOC); |
||
165 | return $found; |
||
166 | } |
||
167 | throw new DataNotFoundException(); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * save() |
||
172 | * Insert data into database |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | View Code Duplication | public function save($dbConnection = NULL) |
|
177 | { |
||
178 | $connection = DatabaseQuery::checkConnection($dbConnection); |
||
179 | |||
180 | $query = $this->insertQuery(self::getTableName($connection)); |
||
181 | $statement = $connection->prepare($query); |
||
182 | if( $statement->execute() ) |
||
183 | { |
||
184 | return true; |
||
185 | } |
||
186 | throw new DataAlreadyExistException(); |
||
187 | |||
188 | } |
||
189 | |||
190 | /** |
||
191 | * update() |
||
192 | * Update details in database after ::find(2) |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | View Code Duplication | public function update($dbConnection = NULL) |
|
197 | { |
||
198 | $connection = DatabaseQuery::checkConnection($dbConnection); |
||
199 | |||
200 | $updateQuery = $this->updateQuery(self::getTableName($connection)); |
||
201 | $statement = $connection->prepare($updateQuery); |
||
202 | if( $statement->execute() ) |
||
203 | { |
||
204 | return true; |
||
205 | } |
||
206 | throw new DataAlreadyExistException(); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * destroy($value) |
||
211 | * Delete data from database |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function destroy($value, $dbConnection = NULL) |
||
216 | { |
||
217 | $connection = DatabaseQuery::checkConnection($dbConnection); |
||
218 | |||
219 | $query = $connection->prepare('DELETE FROM ' . self::getTableName($connection) . ' WHERE id = '.$value); |
||
220 | $query->execute(); |
||
221 | $check = $query->rowCount(); |
||
222 | if ($check) |
||
223 | { |
||
224 | return true; |
||
225 | } |
||
226 | throw new DataNotFoundException; |
||
227 | } |
||
228 | } |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.