Overview

Namespaces

  • Deimos
    • Request
      • Adapter
  • PHP

Classes

  • Deimos\Request\Request

Interfaces

  • Throwable

Traits

  • Deimos\Request\Adapter\Data
  • Deimos\Request\Adapter\Other
  • Deimos\Request\Adapter\Query
  • Deimos\Request\Adapter\Request
  • Deimos\Request\Adapter\Router
  • Deimos\Request\Adapter\Server
  • Deimos\Request\AdapterExtension
  • Deimos\Request\DefaultAdapter
  • Deimos\Request\RequestExtension

Exceptions

  • BadFunctionCallException
  • Exception
  • InvalidArgumentException
  • LogicException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Deimos\Request;
  4: 
  5: trait RequestExtension
  6: {
  7: 
  8:     use Adapter\Request;
  9:     use Adapter\Server;
 10:     use Adapter\Router;
 11:     use Adapter\Query;
 12:     use Adapter\Other;
 13:     use Adapter\Data;
 14: 
 15:     /**
 16:      * Gets request URL
 17:      *
 18:      * @param bool $withParams Whether to preserve URL parameters
 19:      *
 20:      * @return string URL of this request
 21:      */
 22:     public function url($withParams = false)
 23:     {
 24:         $url = $this->isHttps() ? 'https://' : 'http://';
 25:         $url .= $this->server('http_host');
 26: 
 27:         return $url . $this->urlPath($withParams);
 28:     }
 29: 
 30:     /**
 31:      * without host & schema
 32:      *
 33:      * @param bool $withParams
 34:      *
 35:      * @return mixed|string
 36:      */
 37:     public function urlPath($withParams = false)
 38:     {
 39:         $path = $this->server('request_uri');
 40: 
 41:         if (!$withParams)
 42:         {
 43:             $position = mb_strpos($path, '?');
 44:             if ($position !== false)
 45:             {
 46:                 $path = mb_substr($path, 0, $position);
 47:             }
 48:         }
 49: 
 50:         return $path;
 51:     }
 52: 
 53:     /**
 54:      * @return bool
 55:      */
 56:     public function isHttps()
 57:     {
 58:         $https = $this->server('https');
 59: 
 60:         return $this->filterVariable($https, FILTER_VALIDATE_BOOLEAN, false);
 61:     }
 62: 
 63:     /**
 64:      * @param string|null $url
 65:      *
 66:      * @return string
 67:      */
 68:     public function queryString($url = null)
 69:     {
 70:         $query = '';
 71: 
 72:         if (!$url)
 73:         {
 74:             $url = $this->server('request_uri');
 75:         }
 76: 
 77:         $position = mb_strpos($url, '?');
 78: 
 79:         if ($position !== false)
 80:         {
 81:             $query = mb_substr($url, $position + 1);
 82:         }
 83: 
 84:         return $query;
 85:     }
 86: 
 87:     /**
 88:      * @param array     $data
 89:      * @param int|array $options
 90:      */
 91:     public function json(array $data = array(), $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
 92:     {
 93: 
 94:         if (!headers_sent())
 95:         {
 96:             header('Cache-Control: no-cache, must-revalidate');
 97:             header('Expires: Tue, 19 May 1981 18:00:00 GMT');
 98:             header('Content-type: application/json; charset=utf-8');
 99:         }
100: 
101:         if (is_array($options))
102:         {
103:             $this->helper->json()->reset();
104: 
105:             foreach ($options as $option)
106:             {
107:                 $this->helper->json()->addOption($option);
108:             }
109:         }
110:         else
111:         {
112:             $this->helper->json()->setOption($options);
113:         }
114: 
115:         return $this->helper->json()->encode($data);
116: 
117:     }
118: 
119:     /**
120:      * Check if the request is ajax
121:      *
122:      * @return bool
123:      */
124:     public function isAjax()
125:     {
126:         $httpXRequestedWith = $this->server('http_x_requested_with');
127:         $httpXRequestedWith = $this->normalizeLow($httpXRequestedWith);
128: 
129:         return $httpXRequestedWith === 'xmlhttprequest';
130:     }
131: 
132:     /**
133:      * @return bool
134:      */
135:     public function isDelete()
136:     {
137:         return $this->method() === 'DELETE';
138:     }
139: 
140:     /**
141:      * @return string
142:      */
143:     public function method()
144:     {
145:         return $this->server('request_method');
146:     }
147: 
148:     /**
149:      * @return bool
150:      */
151:     public function isHead()
152:     {
153:         return $this->method() === 'HEAD';
154:     }
155: 
156:     /**
157:      * @return bool
158:      */
159:     public function isPut()
160:     {
161:         return $this->method() === 'PUT';
162:     }
163: 
164:     /**
165:      * @return bool
166:      */
167:     public function isPatch()
168:     {
169:         return $this->method() === 'PATCH';
170:     }
171: 
172:     /**
173:      * @return bool
174:      */
175:     public function isPost()
176:     {
177:         return $this->method() === 'POST';
178:     }
179: 
180:     /**
181:      * @return bool
182:      */
183:     public function isGet()
184:     {
185:         return $this->method() === 'GET';
186:     }
187: 
188: }
API documentation generated by ApiGen