Source of file CommonManager.php
Size: 7,101 Bytes - Last Modified: 2020-10-24T02:46:31+00:00
/home/travis/build/NextDom/nextdom-core/src/Managers/Parents/CommonManager.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 | <?php /* This file is part of Jeedom. * * Jeedom is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Jeedom is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jeedom. If not, see <http://www.gnu.org/licenses/>. */ /* This file is part of NextDom Software. * * NextDom is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * NextDom is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NextDom. If not, see <http://www.gnu.org/licenses/>. */ namespace NextDom\Managers\Parents; use NextDom\Exceptions\CoreException; use NextDom\Helpers\DBHelper; /** * Base manager with commonts functions * * @package NextDom\Managers */ trait CommonManager { /** * Get one object by his id * * @param $requestedId * * @return mixed * * @throws CoreException */ public static function byId($requestedId) { if (empty($requestedId) === true) { return null; } $params = ['id' => $requestedId]; $sql = static::getBaseSQL() . "WHERE `id` = :id"; return DBHelper::getOneObject($sql, $params, static::CLASS_NAME); } /** * Get all objects * * @return mixed|null * * @throws CoreException */ protected static function getAll() { return DBHelper::getAllObjects(static::getBaseSQL(), [], static::CLASS_NAME); } /** * Get count(*). * * @param bool $onlyVisible Filter only visible objects * * @return int * * @throws \Exception */ public static function getCount() { $sql = "SELECT count(*) FROM " . static::DB_CLASS_NAME . ";"; return DBHelper::Prepare($sql)['count(*)']; } /** * Get all object sorted on column * * @param string $orderColumn Sort column * @param bool $reverseOrder True for DESC order * @param int $limit Max number of results * * @return mixed|null * * @throws CoreException */ protected static function getAllOrdered(string $orderColumn, bool $reverseOrder = false, int $limit = 0) { $sql = static::getBaseSQL() . "ORDER BY `$orderColumn`"; if ($reverseOrder) { $sql .= ' DESC'; } if ($limit > 0) { $sql .= " LIMIT $limit"; } return DBHelper::getAllObjects($sql, [], static::CLASS_NAME); } /** * Get one object filtered with clauses * * @param array $clauses * @param string|array $orderColumn * @param bool $reverseOrder * * @return mixed|null * @throws CoreException * @throws \ReflectionException */ protected static function getOneByClauses(array $clauses, $orderColumn = '', bool $reverseOrder = false) { return static::query($clauses, '=', true, $orderColumn, $reverseOrder); } /** * Get one multiple filtered with clauses * * @param array $clauses * @param string|array $orderColumn * @param bool $reverseOrder * * @return mixed|null * @throws CoreException * @throws \ReflectionException */ protected static function getMultipleByClauses(array $clauses, $orderColumn = '', bool $reverseOrder = false) { return static::query($clauses, '=', false, $orderColumn, $reverseOrder); } /** * Search one object filtered with clauses * * @param array $clauses * @param string|array $orderColumn * @param bool $reverseOrder * * @return mixed|null * @throws CoreException * @throws \ReflectionException */ protected static function searchOneByClauses(array $clauses, $orderColumn = '', bool $reverseOrder = false) { return static::query($clauses, 'LIKE', true, $orderColumn, $reverseOrder); } /** * Search one multiple filtered with clauses * * @param array $clauses * @param string|array $orderColumn * @param bool $reverseOrder * * @return mixed|null * @throws CoreException * @throws \ReflectionException */ protected static function searchMultipleByClauses(array $clauses, $orderColumn = '', bool $reverseOrder = false) { return static::query($clauses, 'LIKE', false, $orderColumn, $reverseOrder); } /** * Execute a query * * @param array $clauses * @param string $compOperator * @param bool $onlyOneResult * @param string|array $orderColumn * @param bool $reverseOrder * * @return mixed|null * * @throws CoreException * @throws \ReflectionException */ private static function query(array $clauses, $compOperator = '', bool $onlyOneResult = false, $orderColumn = '', bool $reverseOrder = false) { $sql = static::createSQL($clauses, $compOperator, $orderColumn, $reverseOrder); if ($onlyOneResult === true) { return DBHelper::getOneObject($sql, $clauses, static::CLASS_NAME); } else { return DBHelper::getAllObjects($sql, $clauses, static::CLASS_NAME); } } /** * Create SQL query * * @param array $clauses * @param string $compOperator * @param string|array $orderColumn * @param bool $reverseOrder * * @return string * */ private static function createSQL(array $clauses, string $compOperator, $orderColumn = '', bool $reverseOrder = false) { $sql = static::getBaseSQL(); $sqlClauses = ''; $sqlOrder = ''; foreach ($clauses as $clauseName => $clauseValue) { if ($sqlClauses === '') { $sqlClauses = "WHERE "; } else { $sqlClauses .= "AND "; } $sqlClauses .= "`$clauseName` $compOperator :$clauseName "; } if ($orderColumn !== '') { if (is_array($orderColumn)) { $sqlOrder = "ORDER BY `" . implode("`, `", $orderColumn) . "`"; } else { $sqlOrder = "ORDER BY `$orderColumn`"; } if ($reverseOrder) { $sqlOrder .= ' DESC'; } } return $sql . $sqlClauses . $sqlOrder; } } |