nyroFwk  0.2
vars.class.php
Go to the documentation of this file.
1 <?php
10 final class http_vars {
11 
17  private static $instance;
18 
22  private function __construct() {}
23 
29  public static function getInstance() {
30  if (is_null(self::$instance))
31  self::$instance = new http_vars();
32  return self::$instance;
33  }
34 
48  public function getVar($prm) {
49  if (is_string($prm))
50  $p = array('name'=>$prm);
51  else
52  $p = &$prm;
53  $ret = null;
54  if (config::initTab($p, array(
55  'name'=>null,
56  'nameIn'=>'',
57  'method'=>array('post', 'get'),
58  'trim'=>true
59  ))) {
60  $matches = explode('|', str_replace(
61  array('[]', '][', '[', ']'),
62  array('', '|', '|', ''),
63  $p['name']
64  ));
65  $name = empty($matches)? array($p['name']): $matches;
66  if (is_array($p['method'])) {
67  for($i = 0; $i < count($p['method']) && $ret === null; $i++) {
68  $act = '_'.strtoupper($p['method'][$i]);
69  $ret = utils::getValInArray($GLOBALS[$act], $name);
70  }
71  } else {
72  $act = '_'.strtoupper($p['method']);
73  $ret = utils::getValInArray($GLOBALS[$act], $name);
74  }
75  if ($act == '_GET' && !is_null($ret))
76  $ret = is_array($ret) ? array_map('urldecode', $ret) : urldecode($ret);
77  if ($p['trim'] && !is_null($ret) && !is_array($ret))
78  $ret = trim($ret);
79  }
80  $prm = array_merge(array('default'=>null), $p);
81  if (is_array($prm['default'])) {
82  if (is_null($ret) || !in_array($ret, $prm['default'])) {
83  $ret = $prm['default'][0];
84  }
85  } else if (is_null($ret))
86  $ret = $prm['default'];
87 
88  $ret = utils::htmlIn($ret);
89  if (is_array($ret) && !empty($p['nameIn']))
90  return array_key_exists($p['nameIn'], $ret) ? $ret[$p['nameIn']] : null;
91  else
92  return $ret;
93  }
94 
103  public function post($name, $default=null) {
104  return $this->getVar(array(
105  'name'=>$name,
106  'method'=>'post',
107  'default'=>$default));
108  }
109 
116  public function getVars($method='post') {
117  $act = '_'.strtoupper($method);
118  return utils::htmlIn($GLOBALS[$act]);
119  }
120 
127  public function posts() {
128  return $this->getvars('post');
129  }
130 
137  public function gets() {
138  return $this->getvars('get');
139  }
140 
149  public function get($name, $default=null) {
150  return $this->getVar(array(
151  'name'=>$name,
152  'method'=>'get',
153  'default'=>$default));
154  }
155 
164  public function session($name, $default=null) {
165  return $this->getVar(array(
166  'name'=>$name,
167  'method'=>'session',
168  'default'=>$default));
169  }
170 
179  public function cookie($name, $default=null) {
180  return $this->getVar(array(
181  'name'=>$name,
182  'method'=>'cookie',
183  'default'=>$default));
184  }
185 
186 }
session($name, $default=null)
Definition: vars.class.php:164
static htmlIn($val)
static $instance
Definition: vars.class.php:17
getVars($method='post')
Definition: vars.class.php:116
static initTab(array &$vars, array $init)
cookie($name, $default=null)
Definition: vars.class.php:179
post($name, $default=null)
Definition: vars.class.php:103
static getValInArray(array $source, array $keys)
getVar($prm)
Definition: vars.class.php:48
static getInstance()
Definition: vars.class.php:29
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13