Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • HTTPDownload
  • Request
  • Socket
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\Net;
 14: 
 15: /**
 16:  * Classe requête permettant la récupération des paramètres et l'ajout de filtre
 17:  * @category Pry
 18:  * @package Net
 19:  * @author Olivier ROGER <oroger.fr>
 20:  *
 21:  */
 22: class Request
 23: {
 24: 
 25:     protected $headers;
 26:     protected $get;
 27:     protected $post;
 28:     protected $cookie;
 29:     protected $request;
 30:     protected $file;
 31:     protected $filters;
 32:     protected $defaultMethod = 'request';
 33: 
 34:     public function __construct()
 35:     {
 36:         $this->get     = $_GET;
 37:         $this->post    = $_POST;
 38:         $this->cookie  = $_COOKIE;
 39:         $this->file    = $_FILES;
 40:         $this->request = $_REQUEST;
 41:     }
 42: 
 43:     /**
 44:      * Réinitialise l'objet request
 45:      */
 46:     public function reset()
 47:     {
 48:         $this->get     = $_GET;
 49:         $this->post    = $_POST;
 50:         $this->cookie  = $_COOKIE;
 51:         $this->file    = $_FILES;
 52:         $this->request = $_REQUEST;
 53:         $this->headers = null;
 54:     }
 55: 
 56:     /**
 57:      * Retourne l'ensemble des entêtes de la requête
 58:      * @return array
 59:      */
 60:     public function getHeaders()
 61:     {
 62:         if (empty($this->headers))
 63:             $this->headers = getallheaders();
 64: 
 65:         return $this->headers;
 66:     }
 67: 
 68:     /**
 69:      * Retourne un header en particulier
 70:      * @param string $name Nom du header
 71:      * @return string Valeur du header ou null si l'entête n'existe pas.
 72:      */
 73:     public function getHeader($name)
 74:     {
 75:         $this->getHeaders();
 76:         return (isset($this->headers[$name])) ? $this->headers[$name] : null;
 77:     }
 78: 
 79:     /**
 80:      * Récupère une variable $_SERVER
 81:      * @param string $name Nom de la variable à récupérer. Si null la totalité des variables sera retournée
 82:      * @return mixed Retourne une string en cas de valeur unique un array sinon
 83:      */
 84:     public function getServer($name = null)
 85:     {
 86:         if (!empty($name))
 87:             return (isset($_SERVER[$name])) ? $_SERVER[$name] : null;
 88: 
 89:         return $_SERVER;
 90:     }
 91: 
 92:     /**
 93:      * Retourne un paramètre de la requête. Le paramètres pourra être filtré si des filtres ont été défini
 94:      * @param string $name Nom du paramètre
 95:      * @param string $type Type de requête. Peut être get|post|request|cookie
 96:      * @return mixed
 97:      */
 98:     public function getParam($name, $type = null)
 99:     {
100:         $type = empty($type) ? $this->defaultMethod : strtolower($type);
101: 
102:         if (!$this->isValidMethod($type))
103:             throw new \InvalidArgumentException('Type de paramètre invalide');
104: 
105:         if (!empty($this->filters[$type]) && $this->filters[$type]['isFiltered'] == false)
106:             $this->applyFilters($type);
107: 
108:         return (isset($this->{$type}[$name])) ? $this->{$type}[$name] : null;
109:     }
110: 
111:     /**
112:      * Retourne l'ensemble des pramètres de type $type
113:      * @param string $type Peut être get|post|request|cookie
114:      * @return array
115:      * @throws InvalidArgumentException
116:      */
117:     public function getParams($type = null)
118:     {
119:         $type = empty($type) ? $this->defaultMethod : strtolower($type);
120: 
121:         if (!$this->isValidMethod($type))
122:             throw new \InvalidArgumentException('Type de paramètre invalide');
123: 
124:         if (!empty($this->filters[$type]) && $this->filters[$type]['isFiltered'] == false)
125:             $this->applyFilters($type);
126: 
127:         return $this->$type;
128:     }
129: 
130:     /**
131:      * Récupère une valeur POST
132:      * @param string $name Nom de la valeur POST
133:      * @param string $dataType Type de données pour appliquer un filtres.
134:      * @param mixed $flag Flag optionnel à utiliser pour le filtre
135:      * Types autorisés int,float,string,email,url,ip
136:      * @return mixed
137:      */
138:     public function getPost($name, $dataType = null,$flag = null)
139:     {
140:         return $this->getWithFilter($name, 'post', $dataType, $flag);
141:     }
142: 
143:     /**
144:      * Récupère une valeur GET
145:      * @param string $name Nom de la valeur GET
146:      * @param string $dataType Type de données pour appliquer un filtres.
147:      * Types autorisés int,float,string,email,url,ip
148:      * @return mixed
149:      */
150:     public function get($name, $dataType = null,$flag = null)
151:     {
152:         return $this->getWithFilter($name, 'get', $dataType, $flag);
153:     }
154: 
155:     /**
156:      * Récupère une variable d'environnement
157:      * @param string $name
158:      */
159:     public function getEnv($name)
160:     {
161:         return getenv($name);
162:     }
163: 
164:     /**
165:      * Retourne la variable $_FILES demandé
166:      * @param string $name nom du file
167:      * @return array
168:      */
169:     public function getFile($name)
170:     {
171:         return isset($this->file[$name]) ? $this->file[$name] : null;
172:     }
173: 
174:     /**
175:      * Retourne $_FILES
176:      * @return array
177:      */
178:     public function getFiles()
179:     {
180:         return $this->file;
181:     }
182: 
183:     /**
184:      * Ajoute un filtre à appliquer lors de la récupération de paramètre.
185:      * <code>
186:      * setFilter(
187:      *      array(
188:      *         'id' => FILTER_SANITIZE_NUMBER_INT,
189:      *         'nom'=> FILTER_SANITIZE_STRING
190:      *      ),'post'
191:      * );
192:      * </code>
193:      * @param array $filtre Description du filtre. Doit être compatible avec filter_var_array
194:      * @param string $type Le type de requête
195:      * @see http://github.com/bdelespierre/php-axiom/tree/master/libraries/axiom/axRequest.class.php
196:      * @see http://php.net/manual/fr/function.filter-var-array.php
197:      * @return \Controller_Request
198:      * @throws InvalidArgumentException En cas de type invalide
199:      */
200:     public function setFilter(array $filtre, $type = null)
201:     {
202:         $type = empty($type) ? $this->defaultMethod : strtolower($type);
203: 
204:         if (!$this->isValidMethod($type))
205:             throw new \InvalidArgumentException('Type de paramètre invalide');
206: 
207:         $this->filters[$type] = array(
208:             'filter' => $filtre,
209:             'isFiltered' => false
210:         );
211: 
212:         return $this;
213:     }
214: 
215:     /**
216:      * Défini la méthode par défaut à utiliser. request est utilisé de base.
217:      * Cela agit directement sur les méthode getParam() , getParams() , setFilter() , quand le 
218:      * paramètre de méthode n'est pas fournit
219:      * @param string $name Nom de la méthode parmis get|post|cookie|request
220:      */
221:     public function setDefaultMethod($name)
222:     {
223:         $this->defaultMethod = strtolower($name);
224:     }
225: 
226:     public function __get($name)
227:     {
228:         return $this->getParam($name);
229:     }
230:     
231:     public function __isset($name)
232:     {
233:         $tmp = $this->getParam($name);
234:         return isset($tmp);
235:     }
236: 
237:     public function add($params, $type = null)
238:     {
239:         $type = empty($type) ? $this->defaultMethod : strtolower($type);
240: 
241:         $this->{$type} = array_merge($this->{$type}, $params);
242: 
243:         if (isset($this->filters[$type]))
244:             $this->filters[$type]['isFiltered'] = false;
245:     }
246:     
247:     /**
248:      * Vérifie si la requête est de type post
249:      * @return boolean
250:      */
251:     public function isPost()
252:     {
253:         if($this->getServer('REQUEST_METHOD') == 'POST') {
254:             return true;
255:         }
256:         
257:         return false;
258:     }
259:     
260:     /**
261:      * Vérifie si la requête est de type get
262:      * @return boolean
263:      */
264:     public function isGet()
265:     {
266:         if($this->getServer('REQUEST_METHOD') == 'GET') {
267:             return true;
268:         }
269:         
270:         return false;
271:     }
272:     
273:     /**
274:      * Vérifie la validité de la méthode demandé
275:      * @param string $method
276:      * @return boolean
277:      */
278:     protected function isValidMethod($method)
279:     {
280:         return in_array($method, array('get', 'post', 'request', 'cookie'));
281:     }
282: 
283:     /**
284:      * Applique les filtres défini
285:      * @param string $type le type de requête
286:      * @return boolean
287:      * @throws RuntimeException Si les filtres échoue
288:      */
289:     protected function applyFilters($type)
290:     {
291:         if (empty($this->filters[$type]) || empty($this->$type))
292:             return false;
293: 
294:         $this->$type = filter_var_array($this->$type, $this->filters[$type]['filter']);
295: 
296:         if (!$this->$type)
297:             throw new \RuntimeException('Filtres invalide');
298: 
299:         $this->filters['isFiltered'] = true;
300:     }
301: 
302:     /**
303:      * Récupère un paramètre en appliquant un filtres particulier
304:      * @param string $name Nom du paramètre
305:      * @param string $type Type de paramètre
306:      * @param string $dataType Type de données attendu
307:      * @param mixed $flag Flag optionnel à utiliser
308:      */
309:     protected function getWithFilter($name, $type, $dataType,$flag = null)
310:     {
311:         $type = empty($type) ? $this->defaultMethod : strtolower($type);
312:         if (isset($this->{$type}[$name]))
313:         {
314:             switch ($dataType)
315:             {
316:                 case 'int' :
317:                     return intval($this->{$type}[$name]);
318:                     break;
319:                 case 'float' :
320:                     return floatval($this->{$type}[$name]);
321:                     break;
322:                 case 'string' :
323:                     $str = filter_var($this->{$type}[$name], FILTER_SANITIZE_STRING,$flag);
324:                     return ($str != false) ? $str : null;
325:                     break;
326:                 case 'email' :
327:                     $str =  filter_var($this->{$type}[$name], FILTER_VALIDATE_EMAIL,$flag);
328:                     return ($str != false) ? $str : null;
329:                     break;
330:                 case 'url' :
331:                     $str =  filter_var($this->{$type}[$name], FILTER_VALIDATE_URL,$flag);
332:                     return ($str != false) ? $str : null;
333:                     break;
334:                 case 'ip' :
335:                     $str =  filter_var($this->{$type}[$name], FILTER_VALIDATE_IP,$flag);
336:                     return ($str != false) ? $str : null;
337:                     break;
338:                 default :
339:                     return $this->{$type}[$name];
340:             }
341:         }
342:         else
343:         {
344:             return null;
345:         }
346:     }
347: 
348: }
349: 
350: ?>
351: 
Pry API documentation generated by ApiGen 2.8.0