prepare($query); $statement->execute($params); return $statement; } /** * @param string $class * @param string $query * @param array $params * * @return array */ public static function fetch(string $class, string $query, array $params = []): array { $rows = DB::query($query, $params)->fetchAll(\PDO::FETCH_ASSOC); $results = []; foreach ($rows as $row) { $results[] = DB::convertToModel($class, $row); } return $results; } /** * @param string $class * @param array $row * * @return object */ public static function convertToModel(string $class, array $row): object { $object = new $class(); foreach ($row as $columnKey => $columnValue) { $objectKey = explode('_', $columnKey); $objectKey = $objectKey[0] . implode('', array_map('ucwords', array_slice($objectKey, 1))); if (property_exists($object, $objectKey)) { $propertyType = (new \ReflectionProperty($object, $objectKey))->getType(); if (class_exists($propertyType->getName())) { $object->$objectKey = new ($propertyType->getName())($columnValue); } else { $object->$objectKey = $columnValue; } } } return $object; } }