FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Provider.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Controller;
4 
5 class Provider extends \Rsi\Thing{
6 
7  const SEARCH_FROM = 'from'; //!< Search for values larger than the reference value.
8  const SEARCH_TO = 'to'; //!< Search for values smaller than the reference value.
9  const SEARCH_EXACT = 'exact'; //!< Search for values identical to the reference value (this may also be an array with
10  // multiple values).
11  const SEARCH_DATE = 'date'; //!< Search for a single date (24 hours).
12  const SEARCH_LIKE = 'like'; //!< Search for values that match reference value with wildcard (? = 1 char, * = multiple chars).
13  const SEARCH_RANGE = [self::SEARCH_FROM,self::SEARCH_TO];
14  const SEARCH_MULTI = 'multi'; //!< Pseudo type to allow for multiple values.
15 
16  public $order = []; //!< Basic/default order (same format as search function).
17  public $format = null; //!< Format function to apply to each record (called with a single record by reference, and true as a
18  // second parameter when it concerns totals.
19 
20  protected $_controller = null;
21  protected $_id = null;
22 
23  public function __construct($controller,$config){
24  $this->_controller = $controller;
25  $this->configure($config);
26  }
27  /**
28  * Translates a value using the dataset.
29  * @param string $column Column to get the translated value from.
30  * @param string $key Column to use to find the record.
31  * @param mixed $value Value to use to find the record.
32  * @param array $params Extra parameters.
33  * @return mixed Found value for the column (false = not found).
34  */
35  public function trans($column,$key,$value,$params = null){
36  return false;
37  }
38  /**
39  * Check if a record exists in the dataset.
40  * @param string $key Column to use to find the record.
41  * @param mixed $value Value to use to find the record.
42  * @param array $params Extra parameters.
43  * @return bool True if the record exists.
44  */
45  public function exists($key,$value,$params = null){
46  return false;
47  }
48 
49  protected function _search($search,$order,$params,$offset,$limit){
50  return [];
51  }
52  /**
53  * Search for records in the dataset.
54  * @param array $search Search criteria (key = column, value = array with key = search type - see SEARCH_* constants,
55  * value = search reference value).
56  * @param array $order Sort order for the result (key = column name, value = sort order - use PHP's SORT_*
57  * constants; SORT_ASC and SORT_DESC are always supported, combinations with SORT_NUMERIC, SORT_NATURAL and the like are
58  * depending on the type of provider and should also be shifted 4 bits up).
59  * @param array $params Extra parameters.
60  * @param int $offset Number of records to skip.
61  * @param int $limit Maximum number of records to return (empty = all).
62  * @return array Array with found records (key = column name).
63  */
64  public function search($search,$order = null,$params = null,$offset = 0,$limit = null){
65  $records = $this->_search($search,$order,$params,$offset,$limit);
66  if($this->format) array_walk($records,$this->format);
67  return $records;
68  }
69 
70  protected function _totals($search,$totals,$params){
71  return [];
72  }
73  /**
74  * Return the totals for selected columns.
75  * @param array $search Search criteria (key = search type - see SEARCH_* constants, value = search compare value).
76  * @param array $totals Columns to return the totals for, assoc.array (key = column name, value = total type - see
77  * \\Rsi\\Fred\\controller::TOTAL_* constants, one or multiple in an array).
78  * @param array $params Extra parameters.
79  * @return array Array with totals (key = column name, value = total).
80  */
81  public function totals($search,$totals,$params = null){
82  $totals = $this->_totals($search,$totals,$params);
83  if($this->format) call_user_func_array($this->format,[&$totals,true]);
84  return $totals;
85  }
86 
87  protected function _group($search,$group,$column,$total,$limit,$params){
88  return null;
89  }
90  /**
91  * Return the grouped totals for a column.
92  * @param array $search Search criteria (key = search type - see SEARCH_* constants, value = search compare value).
93  * @param string $group Column to group by.
94  * @param string $column Column to group the value for.
95  * @param string $total Total type (see \\Rsi\\Fred\\controller::TOTAL_* constants).
96  * @param int $limit Positive for top, negative for bottom, zero for all (sorted by $column).
97  * @param array $params Extra parameters.
98  * @return array Key = group, value = total.
99  */
100  public function group($search,$group,$column,$total,$limit = null,$params = null){
101  $totals = $this->_group($search,$group,$column,$total,$limit,$params);
102  if($this->format) foreach($totals as $key => $value){
103  $value = [$column => [$total => $value]];
104  call_user_func_array($this->format,[&$value,true]);
105  $totals[$key] = $value[$column][$total];
106  }
107  return $totals;
108  }
109 
110  protected function setId($value){
111  $this->_id = $value;
112  }
113 
114  protected function getId(){
115  return $this->_id;
116  }
117 
118 }
const SEARCH_EXACT
Search for values identical to the reference value (this may also be an array with.
Definition: Provider.php:9
totals($search, $totals, $params=null)
Return the totals for selected columns.
Definition: Provider.php:81
trans($column, $key, $value, $params=null)
Translates a value using the dataset.
Definition: Provider.php:35
exists($key, $value, $params=null)
Check if a record exists in the dataset.
Definition: Provider.php:45
Basic object.
Definition: Thing.php:13
configure($config)
Configure the object.
Definition: Thing.php:45
_group($search, $group, $column, $total, $limit, $params)
Definition: Provider.php:87
_totals($search, $totals, $params)
Definition: Provider.php:70
const SEARCH_LIKE
Search for values that match reference value with wildcard (? = 1 char, * = multiple chars)...
Definition: Provider.php:12
_search($search, $order, $params, $offset, $limit)
Definition: Provider.php:49
const SEARCH_TO
Search for values smaller than the reference value.
Definition: Provider.php:8
const SEARCH_DATE
Search for a single date (24 hours).
Definition: Provider.php:11
$order
Basic/default order (same format as search function).
Definition: Provider.php:16
const SEARCH_FROM
Search for values larger than the reference value.
Definition: Provider.php:7
search($search, $order=null, $params=null, $offset=0, $limit=null)
Search for records in the dataset.
Definition: Provider.php:64
group($search, $group, $column, $total, $limit=null, $params=null)
Return the grouped totals for a column.
Definition: Provider.php:100
__construct($controller, $config)
Definition: Provider.php:23
const SEARCH_MULTI
Pseudo type to allow for multiple values.
Definition: Provider.php:14
$format
Format function to apply to each record (called with a single record by reference, and true as a.
Definition: Provider.php:17