1: <?php
  2: 
  3: namespace Deimos\Request;
  4: 
  5: use Deimos\Helper\Exceptions\ExceptionEmpty;
  6: 
  7: trait DefaultAdapter
  8: {
  9: 
 10:      11:  12: 
 13:     protected $helper;
 14:     private   $input = 'php://input';
 15: 
 16:      17:  18:  19:  20: 
 21:     protected function inputArray($type)
 22:     {
 23:         return filter_input_array($type, FILTER_UNSAFE_RAW) ?: [];
 24:     }
 25: 
 26:      27:  28: 
 29:     protected function getInput()
 30:     {
 31:         $contents = $this->getContents($this->input);
 32: 
 33:         return $this->parseStr($contents);
 34:     }
 35: 
 36:      37:  38:  39:  40: 
 41:     private function getContents($data)
 42:     {
 43:         return file_get_contents($data);
 44:     }
 45: 
 46:      47:  48:  49:  50: 
 51:     private function parseStr($data)
 52:     {
 53:         parse_str($data, $output);
 54: 
 55:         return $output;
 56:     }
 57: 
 58:      59:  60:  61:  62: 
 63:     protected function normalizeUpp($string)
 64:     {
 65:         if(null === $string) {
 66: 
 67:             return null;
 68:         }
 69: 
 70:         return strtoupper($string);
 71:     }
 72: 
 73:      74:  75:  76:  77: 
 78:     protected function normalizeLow($string)
 79:     {
 80:         if(null === $string) {
 81: 
 82:             return null;
 83:         }
 84: 
 85:         return strtolower($string);
 86:     }
 87: 
 88:      89:  90:  91:  92:  93:  94:  95:  96: 
 97:     protected function arrRequired(array $storage, $path, $xss)
 98:     {
 99:         $data = $this->helper->arr()->getRequired($storage, $path);
100: 
101:         if ($xss)
102:         {
103:             $data = $this->xss($data);
104:         }
105: 
106:         return $data;
107:     }
108: 
109:     110: 111: 112: 113: 
114:     protected function xss($data)
115:     {
116:         if (is_array($data))
117:         {
118:             return filter_var_array($data, FILTER_SANITIZE_STRING);
119:         }
120: 
121:         return filter_var($data, FILTER_SANITIZE_STRING);
122:     }
123: 
124:     125: 126: 127: 128: 129: 130: 131: 
132:     protected function arrGetXss(array $storage, $path, $default, $xss)
133:     {
134:         $data = $this->arrGet($storage, $path, $default);
135: 
136:         if ($xss && !empty($data))
137:         {
138:             $data = $this->xss($data);
139:         }
140: 
141:         return $data;
142:     }
143: 
144:     145: 146: 147: 148: 149: 150: 
151:     protected function arrGet(array $storage, $path, $default)
152:     {
153:         if ($path === null)
154:         {
155:             return $storage;
156:         }
157: 
158:         return $this->helper->arr()->get($storage, $path, $default);
159:     }
160: 
161:     162: 163: 164: 165: 166: 167: 
168:     protected function filterVariable($data, $type, $default)
169:     {
170:         return filter_var($data, $type, array(
171:             'options' => array('default' => $default)
172:         ));
173:     }
174: 
175: }