FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Char.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Char extends \Rsi\Fred\Controller\Widget{
6 
7  //format options
8  const SIZE = 'size'; //!< Size of field.
9  const PLACEHOLDER = 'placeholder';
10  const SUGGEST = 'suggest'; //!< Suggestion optiones. This can be an assoc.array, a callback function (called with all extra
11  // parameters as an array), an SQL query, a reference to a database field in 'table.field' format, or a translatable string
12  // (options split by line feed, key and value seperated by '=').
13  const NOTATION = 'notation'; //!< Regular expression describing the prefered notation style (e.g. '/^[A-z]/' = start with a
14  // capital).
15  const TRANSFORM = 'transform'; //!< Transform the value (see \\Rsi\\Str::TRANSFORM_* constants).
16  const TRIM = 'trim'; //!< Strip these characters from the beginning and end of the value (true for regular whitespace).
17 
18  //checks
19  const REGEX = 'regex';
20 
21  public $regex = null;
22  public $size = null;
23  public $placeholder = null;
24  public $suggest = null;
25  public $suggestLimit = 10;
26  public $notation = null;
27  public $transform = null;
28  public $trim = null;
29 
30  public function clientConfig(){
31  $config = array_merge(parent::clientConfig(),$this->get([self::NOTATION,self::TRANSFORM,self::REGEX,self::TRIM]));
32  if($this->suggest) $config[self::SUGGEST] = true;
33  return $config;
34  }
35 
36  public function formatTag($value){
37  return $this->local->formatInt($value);
38  }
39 
40  protected function purgeBase($value){
41  return $value && is_string($value) ? $value : null;
42  }
43 
44  protected function purgeTransform($value){
45  return $this->transform && !$this->nothing($value) ? \Rsi\Str::transform($value,$this->transform) : $value;
46  }
47 
48  protected function purgeTrim($value){
49  return $this->trim ? trim($value,$this->trim === true ? " \t\n\r" : $this->trim) : $value;
50  }
51 
52  protected function checkMin($value,$index = null){
53  return !$this->min || (mb_strlen($value) >= $this->min);
54  }
55 
56  protected function checkMax($value,$index = null){
57  return !$this->max || (mb_strlen($value) <= $this->max);
58  }
59 
60  protected function checkRegex($value,$index = null){
61  return !$this->regex || preg_match($this->regex,$value);
62  }
63 
64  protected function actionSuggest(){
65  if(is_callable($suggest = $this->suggest))
66  $suggest = call_user_func($suggest,$this->params);
67  elseif(is_string($suggest)){
68  if($this->db->isSelection($suggest))
69  $suggest = $this->db->record($suggest,$this->params);
70  elseif(!preg_match('/^(\\w+)\\.(\\w+)$/',$suggest,$match))
71  $suggest = \Rsi\Record::explode($this->trans->str($suggest),"\n",'=');
72  elseif(($count = count($suggest = $this->db->record(
73  $this->db->limit($sql = "
74  select distinct(`$match[2]`) from `$match[1]`
75  where `$match[2]` like :this
76  order by `$match[2]`",
77  $this->suggestLimit
78  ),
79  ['this' => $value = $this->params['this'] . '%']
80  ))) < $this->suggestLimit) $suggest = array_merge($suggest,$this->db->record(
81  $this->db->limit($sql,$this->suggestLimit - $count),
82  ['this' => '_%' . $value]
83  ));
84  }
85  $this->request->result = ['suggest' => $suggest];
86  }
87 
88  protected function getParams(){
89  return array_merge(parent::getParams(),['this' => $this->request->this]);
90  }
91 
92 }
const TRIM
Strip these characters from the beginning and end of the value (true for regular whitespace).
Definition: Char.php:16
checkMax($value, $index=null)
Definition: Char.php:56
nothing($value)
Check if a value is empty.
Definition: Widget.php:240
checkRegex($value, $index=null)
Definition: Char.php:60
const SUGGEST
Suggestion optiones. This can be an assoc.array, a callback function (called with all extra...
Definition: Char.php:10
const TRANSFORM
Transform the value (see \Rsi\Str::TRANSFORM_* constants).
Definition: Char.php:15
const SIZE
Size of field.
Definition: Char.php:8
checkMin($value, $index=null)
Definition: Char.php:52
const NOTATION
Regular expression describing the prefered notation style (e.g. &#39;/^[A-z]/&#39; = start with a...
Definition: Char.php:13