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 AUTOCOMPLETE = 'autocomplete'; //!< Browser auto-complete (see
11  // https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete).
12  const SUGGEST = 'suggest'; //!< Suggestion optiones. This can be an assoc.array, a callback function (called with all extra
13  // parameters as an array), an SQL query, a reference to a database field in 'table.field' format, or a translatable string
14  // (options split by line feed, key and value seperated by '=').
15  const NOTATION = 'notation'; //!< Regular expression describing the prefered notation style (e.g. '/^[A-Z]/' = start with a
16  // capital).
17  const TRANSFORM = 'transform'; //!< Transform the value (see \\Rsi\\Str::TRANSFORM_* constants).
18  const TRIM = 'trim'; //!< Strip these characters from the beginning and end of the value (true for regular whitespace).
19  const REPLACE = 'replace'; //!< Array with key = regex to search for and value = value to replace it with.
20 
21  //checks
22  const REGEX = 'regex';
23  const SPOOF = 'spoof';
24 
25  public $regex = null;
26  public $spoof = null; //!< Spoof check options (true = all; see \\Spoofchecker).
27  public $size = null;
28  public $placeholder = null;
29  public $autocomplete = null;
30  public $suggest = null;
31  public $suggestLimit = 10;
32  public $notation = null;
33  public $transform = null;
34  public $trim = null;
35  public $replace = [];
36 
37  public function clientConfig(){
38  $config = array_merge(parent::clientConfig(),$this->get([self::NOTATION,self::TRANSFORM,self::REGEX,self::TRIM]));
39  if($this->suggest) $config[self::SUGGEST] = true;
40  if($this->replace) $config[self::REPLACE] = $this->replace;
41  return $config;
42  }
43 
44  public function formatTag($value){
45  return $this->local->formatInt($value);
46  }
47 
48  protected function purgeBase($value){
49  return $value && is_string($value) ? $value : null;
50  }
51 
52  protected function purgeTransform($value){
53  return $this->transform && !$this->nothing($value) ? \Rsi\Str::transform($value,$this->transform) : $value;
54  }
55 
56  protected function purgeTrim($value){
57  return $this->trim && is_scalar($value) ? trim($value,$this->trim === true ? " \t\n\r" : $this->trim) : $value;
58  }
59 
60  protected function purgeReplace($value){
61  if($this->replace) foreach($this->replace as $mask => $replace) $value = preg_replace($mask,$replace,$value);
62  return $value;
63  }
64 
65  protected function checkMin($value){
66  return !$this->min || (mb_strlen($value) >= $this->min);
67  }
68 
69  protected function checkMax($value){
70  return !$this->max || (mb_strlen($value) <= $this->max);
71  }
72 
73  protected function checkRegex($value){
74  return !$this->regex || preg_match($this->regex,$value);
75  }
76 
77  protected function checkSpoof($value){
78  return !$this->spoof || !\Rsi\Str::spoof($value,$this->spoof === true ? null : $this->spoof);
79  }
80 
81  protected function actionSuggest(){
82  if(is_callable($suggest = $this->suggest))
83  $suggest = call_user_func($suggest,$this->params);
84  elseif(is_string($suggest)){
85  if($this->db->isSelection($suggest))
86  $suggest = $this->db->record($suggest,$this->params);
87  elseif(!preg_match('/^(\\w+)\\.(\\w+)$/',$suggest,$match))
88  $suggest = \Rsi\Record::explode($this->trans->str($suggest),"\n",'=');
89  elseif(($count = count($suggest = $this->db->record(
90  $this->db->limit($sql = "
91  select distinct(`$match[2]`) from `$match[1]`
92  where `$match[2]` like :this
93  order by `$match[2]`",
94  $this->suggestLimit
95  ),
96  ['this' => $value = $this->request->this . '%']
97  ))) < $this->suggestLimit) $suggest = array_merge($suggest,$this->db->record(
98  $this->db->limit($sql,$this->suggestLimit - $count),
99  ['this' => '_%' . $value]
100  ));
101  }
102  $this->request->result = ['suggest' => $suggest];
103  }
104 
105  protected function getParams(){
106  return array_merge(parent::getParams(),['this' => $this->request->this]);
107  }
108 
109 }
Rsi\Fred\Controller\Widget\Char\REGEX
const REGEX
Definition: Char.php:22
Rsi\Fred\Controller\Widget\Char\SUGGEST
const SUGGEST
Suggestion optiones. This can be an assoc.array, a callback function (called with all extra.
Definition: Char.php:12
Rsi\Fred\Controller\Widget\Char\REPLACE
const REPLACE
Array with key = regex to search for and value = value to replace it with.
Definition: Char.php:19
Rsi\Fred\Controller\Widget\$min
$min
Definition: Widget.php:47
Rsi\Fred\Controller\Widget\Char\AUTOCOMPLETE
const AUTOCOMPLETE
Browser auto-complete (see.
Definition: Char.php:10
Rsi\Fred\Controller\Widget\Char\getParams
getParams()
Definition: Char.php:105
Rsi\Fred\Controller\Widget\Char\purgeReplace
purgeReplace($value)
Definition: Char.php:60
Rsi\Fred\Controller\Widget\Char\checkMin
checkMin($value)
Definition: Char.php:65
Rsi\Fred\Controller\Widget\Char\purgeBase
purgeBase($value)
Purge function that will be called first.
Definition: Char.php:48
Rsi\Fred\Controller\Widget\Char\TRIM
const TRIM
Strip these characters from the beginning and end of the value (true for regular whitespace).
Definition: Char.php:18
Rsi\Fred\Controller\Widget\nothing
nothing($value)
Check if a value is empty.
Definition: Widget.php:274
Rsi\Fred\Controller\Widget\Char\actionSuggest
actionSuggest()
Definition: Char.php:81
Rsi\Fred\Controller\Widget\Char\$spoof
$spoof
Spoof check options (true = all; see \Spoofchecker).
Definition: Char.php:26
Rsi\Fred\Controller\Widget\Char\$placeholder
$placeholder
Definition: Char.php:28
Rsi\Fred\Controller\Widget\Char
Definition: Char.php:5
Rsi\Fred\Controller\Widget\Char\clientConfig
clientConfig()
Public configuration.
Definition: Char.php:37
Rsi\Fred\Controller\Widget\Char\PLACEHOLDER
const PLACEHOLDER
Definition: Char.php:9
Rsi\Fred\Controller\Widget\Char\checkMax
checkMax($value)
Definition: Char.php:69
Rsi\Fred\Controller\Widget\Char\$regex
$regex
Definition: Char.php:25
Rsi\Fred\Controller\Widget\Char\$notation
$notation
Definition: Char.php:32
Rsi\Fred\Controller\Widget\Char\formatTag
formatTag($value)
Format a tag value.
Definition: Char.php:44
Rsi\Fred\Controller\Widget\$max
$max
Definition: Widget.php:48
Rsi\Fred\Controller\Widget\Char\$suggest
$suggest
Definition: Char.php:30
Rsi\Fred\Controller\Widget\Char\NOTATION
const NOTATION
Regular expression describing the prefered notation style (e.g. '/^[A-Z]/' = start with a.
Definition: Char.php:15
Rsi\Fred\Controller\Widget\Char\checkSpoof
checkSpoof($value)
Definition: Char.php:77
Rsi\Fred\Controller\Widget\Char\purgeTransform
purgeTransform($value)
Definition: Char.php:52
Rsi\Fred\Controller\Widget\Char\$replace
$replace
Definition: Char.php:35
Rsi\Fred\Controller\Widget
Definition: Char.php:3
Rsi\Fred\Controller\Widget\Char\$size
$size
Definition: Char.php:27
Rsi\Fred\Controller\Widget\Char\purgeTrim
purgeTrim($value)
Definition: Char.php:56
Rsi\Fred\Controller\Widget\Char\SPOOF
const SPOOF
Definition: Char.php:23
Rsi\Fred\Controller\Widget\Char\TRANSFORM
const TRANSFORM
Transform the value (see \Rsi\Str::TRANSFORM_* constants).
Definition: Char.php:17
Rsi\Fred\Controller\Widget\Char\checkRegex
checkRegex($value)
Definition: Char.php:73
Rsi\Fred\Controller\Widget\Char\SIZE
const SIZE
Size of field.
Definition: Char.php:8
Rsi\Fred\Controller\Widget\Char\$suggestLimit
$suggestLimit
Definition: Char.php:31
Rsi\Fred\Controller\Widget\Char\$trim
$trim
Definition: Char.php:34
Rsi\Fred\Controller\Widget\Char\$transform
$transform
Definition: Char.php:33
Rsi\Fred\Controller\Widget\Char\$autocomplete
$autocomplete
Definition: Char.php:29