1<?php declare(strict_types=1); 2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */ 3 4/** 5 * Class ilTermsOfServiceTableDatabaseDataProvider 6 * @author Michael Jansen <mjansen@databay.de> 7 */ 8abstract class ilTermsOfServiceTableDatabaseDataProvider implements ilTermsOfServiceTableDataProvider 9{ 10 /** @var ilDBInterface */ 11 protected $db; 12 13 /** 14 * ilTermsOfServiceTableDatabaseDataProvider constructor. 15 * @param ilDBInterface $db 16 */ 17 public function __construct(ilDBInterface $db) 18 { 19 $this->db = $db; 20 } 21 22 /** 23 * @param array $params 24 * @param array $filter 25 * @return string 26 */ 27 abstract protected function getSelectPart(array $params, array $filter) : string; 28 29 /** 30 * @param array $params 31 * @param array $filter 32 * @return string 33 */ 34 abstract protected function getFromPart(array $params, array $filter) : string; 35 36 /** 37 * @param array $params 38 * @param array $filter 39 * @return string 40 */ 41 abstract protected function getWherePart(array $params, array $filter) : string; 42 43 /** 44 * @param array $params 45 * @param array $filter 46 * @return string 47 */ 48 abstract protected function getGroupByPart(array $params, array $filter) : string; 49 50 /** 51 * @param array $params 52 * @param array $filter 53 * @return string 54 * @abstract 55 */ 56 abstract protected function getHavingPart(array $params, array $filter) : string; 57 58 /** 59 * @param array $params 60 * @param array $filter 61 * @return string 62 */ 63 abstract protected function getOrderByPart(array $params, array $filter) : string; 64 65 /** 66 * @param array $params 67 * @param array $filter 68 * @return array 69 * @throws InvalidArgumentException 70 */ 71 public function getList(array $params, array $filter) : array 72 { 73 $data = [ 74 'items' => [], 75 'cnt' => 0 76 ]; 77 78 $select = $this->getSelectPart($params, $filter); 79 $where = $this->getWherePart($params, $filter); 80 $from = $this->getFromPart($params, $filter); 81 $order = $this->getOrderByPart($params, $filter); 82 $group = $this->getGroupByPart($params, $filter); 83 $having = $this->getHavingPart($params, $filter); 84 85 if (isset($params['limit'])) { 86 if (!is_numeric($params['limit'])) { 87 throw new InvalidArgumentException('Please provide a valid numerical limit.'); 88 } 89 90 if (!isset($params['offset'])) { 91 $params['offset'] = 0; 92 } else { 93 if (!is_numeric($params['offset'])) { 94 throw new InvalidArgumentException('Please provide a valid numerical offset.'); 95 } 96 } 97 98 $this->db->setLimit($params['limit'], $params['offset']); 99 } 100 101 $where = strlen($where) ? 'WHERE ' . $where : ''; 102 $query = "SELECT {$select} FROM {$from} {$where}"; 103 104 if (strlen($group)) { 105 $query .= " GROUP BY {$group}"; 106 } 107 108 if (strlen($having)) { 109 $query .= " HAVING {$having}"; 110 } 111 112 if (strlen($order)) { 113 $query .= " ORDER BY {$order}"; 114 } 115 116 $res = $this->db->query($query); 117 while ($row = $this->db->fetchAssoc($res)) { 118 $data['items'][] = $row; 119 } 120 121 if (isset($params['limit'])) { 122 $cnt_sql = "SELECT COUNT(*) cnt FROM ({$query}) subquery"; 123 $row_cnt = $this->db->fetchAssoc($this->db->query($cnt_sql)); 124 $data['cnt'] = $row_cnt['cnt']; 125 } 126 127 return $data; 128 } 129} 130