Advertisement
Guest User

Request simple wrapper

a guest
Nov 27th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. <?php
  2. interface EscapeStrategy {
  3.     public function escape($param);
  4. }
  5.  
  6. class HTMLEscapeStrategy implements EscapeStrategy {
  7.     public function escape($param) {
  8.         return htmlentities($param);
  9.     }
  10. }
  11.  
  12. class Post {
  13.  
  14.     private $params = [];
  15.  
  16.     /**
  17.      * @var EscapeStrategy
  18.      */
  19.     private $escapeStrategy;
  20.  
  21.     public function __construct(array $params = array(), EscapeStrategy $escapeStrategy) {
  22.         $this->params = $params;
  23.         $this->escapeStrategy = $escapeStrategy;
  24.     }
  25.  
  26.     public function isEmpty() {
  27.         return count($this->params) <= 0;
  28.     }
  29.  
  30.     public function __get($name) {
  31.         return $this->escapeStrategy->escape($this->params[$name]);
  32.     }
  33. }
  34.  
  35. $post = new Post($_POST, new HTMLEscapeStrategy());
  36.  
  37. ?>
  38.  
  39. <form action="" method="POST">
  40.     <input type="text" name="firstName" />
  41.     <input type="submit" />
  42. </form>
  43.  
  44. <?php if(!$post->isEmpty()): ?>
  45.     <b><?= $post->firstName; ?></b>
  46. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement