ICanBoogie/HTTP v2.5.0
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Exception
    • HTTP
      • Headers
      • Request
      • RequestDispatcher

Classes

  • CallableDispatcher
  • File
  • FileInfo
  • FileList
  • FileResponse
  • Headers
  • RedirectResponse
  • Request
  • RequestDispatcher
  • RequestRange
  • Response
  • Status
  • WeightedDispatcher

Interfaces

  • Dispatcher
  • Exception
  • SecurityError

Exceptions

  • AuthenticationRequired
  • ClientError
  • DispatcherNotDefined
  • ForceRedirect
  • MethodNotSupported
  • NotFound
  • PermissionRequired
  • ServerError
  • ServiceUnavailable
  • StatusCodeNotValid

Functions

  • dispatch
  • get_dispatcher
  • get_initial_request
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\HTTP;

use ICanBoogie\Accessor\AccessorTrait;

/**
 * Class Status
 *
 * @property int $code
 * @property string $message
 *
 * @property-read bool $is_cacheable Whether the status is cacheable.
 * @property-read bool $is_client_error Whether the status is a client error.
 * @property-read bool $is_empty Whether the status is empty.
 * @property-read bool $is_forbidden Whether the status is forbidden.
 * @property-read bool $is_informational Whether the status is informational.
 * @property-read bool $is_not_found Whether the status is not found.
 * @property-read bool $is_ok Whether the status is ok.
 * @property-read bool $is_redirect Whether the status is a redirection.
 * @property-read bool $is_server_error Whether the status is a server error.
 * @property-read bool $is_successful Whether the status is successful.
 * @property-read bool $is_valid Whether the status is valid.
 */
class Status
{
    use AccessorTrait;

    const CONTINUE_ = 100;
    const SWITCHING_PROTOCOLS = 101;
    const OK = 200;
    const CREATED = 201;
    const ACCEPTED = 202;
    const NON_AUTHORITATIVE_INFORMATION = 203;
    const NO_CONTENT = 204;
    const RESET_CONTENT = 205;
    const PARTIAL_CONTENT = 206;
    const MULTIPLE_CHOICES = 300;
    const MOVED_PERMANENTLY = 301;
    const FOUND = 302;
    const SEE_OTHER = 303;
    const NOT_MODIFIED = 304;
    const USE_PROXY = 305;
    const TEMPORARY_REDIRECT = 307;
    const BAD_REQUEST = 400;
    const UNAUTHORIZED = 401;
    const PAYMENT_REQUIRED = 402;
    const FORBIDDEN = 403;
    const NOT_FOUND = 404;
    const METHOD_NOT_ALLOWED = 405;
    const NOT_ACCEPTABLE = 406;
    const PROXY_AUTHENTICATION_REQUIRED = 407;
    const REQUEST_TIMEOUT = 408;
    const CONFLICT = 409;
    const GONE = 410;
    const LENGTH_REQUIRED = 411;
    const PRECONDITION_FAILED = 412;
    const REQUEST_ENTITY_TOO_LARGE = 413;
    const REQUEST_URI_TOO_LONG = 414;
    const UNSUPPORTED_MEDIA_TYPE = 415;
    const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
    const EXPECTATION_FAILED = 417;
    const I_M_A_TEAPOT = 418;
    const INTERNAL_SERVER_ERROR = 500;
    const NOT_IMPLEMENTED = 501;
    const BAD_GATEWAY = 502;
    const SERVICE_UNAVAILABLE = 503;
    const GATEWAY_TIMEOUT = 504;
    const HTTP_VERSION_NOT_SUPPORTED = 505;

    /**
     * HTTP status codes and messages.
     *
     * @var array
     */
    static public $codes_and_messages = [

        100 => "Continue",
        101 => "Switching Protocols",

        200 => "OK",
        201 => "Created",
        202 => "Accepted",
        203 => "Non-Authoritative Information",
        204 => "No Content",
        205 => "Reset Content",
        206 => "Partial Content",

        300 => "Multiple Choices",
        301 => "Moved Permanently",
        302 => "Found",
        303 => "See Other",
        304 => "Not Modified",
        305 => "Use Proxy",
        307 => "Temporary Redirect",

        400 => "Bad Request",
        401 => "Unauthorized",
        402 => "Payment Required",
        403 => "Forbidden",
        404 => "Not Found",
        405 => "Method Not Allowed",
        406 => "Not Acceptable",
        407 => "Proxy Authentication Required",
        408 => "Request Timeout",
        409 => "Conflict",
        410 => "Gone",
        411 => "Length Required",
        412 => "Precondition Failed",
        413 => "Request Entity Too Large",
        414 => "Request-URI Too Long",
        415 => "Unsupported Media Type",
        416 => "Requested Range Not Satisfiable",
        417 => "Expectation Failed",
        418 => "I'm a teapot",

        500 => "Internal Server Error",
        501 => "Not Implemented",
        502 => "Bad Gateway",
        503 => "Service Unavailable",
        504 => "Gateway Timeout",
        505 => "HTTP Version Not Supported"

    ];

    /**
     * Creates a new instance from the provided status.
     *
     * @param $status
     *
     * @return Status
     *
     * @throws \InvalidArgumentException When the HTTP status code is not valid.
     */
    static public function from($status)
    {
        if ($status instanceof self)
        {
            return $status;
        }

        $message = null;

        if (is_array($status))
        {
            list($code, $message) = $status;
        }
        elseif (is_numeric($status))
        {
            $code = (int) $status;
        }
        else
        {
            if (!preg_match('/^(\d{3})\s+(.+)$/', $status, $matches))
            {
                throw new \InvalidArgumentException("Invalid status: $status.");
            }

            list(, $code, $message) = $matches;
        }

        return new static($code, $message);
    }

    /**
     * Asserts that a status code is valid.
     *
     * @param $code
     *
     * @throws StatusCodeNotValid if the status code is not valid.
     */
    static private function assert_code_is_valid($code)
    {
        if ($code >= 100 && $code < 600)
        {
            return;
        }

        throw new StatusCodeNotValid($code);
    }

    /**
     * Status code.
     *
     * @var int
     */
    private $code;

    protected function set_code($code)
    {
        self::assert_code_is_valid($code);

        $this->code = $code;
    }

    protected function get_code()
    {
        return $this->code;
    }

    /**
     * Whether the status is valid.
     *
     * A status is considered valid when its code is between 100 and 600, 100 included.
     *
     * @return bool
     */
    protected function get_is_valid()
    {
        return $this->code >= 100 && $this->code < 600;
    }

    /**
     * Whether the status is informational.
     *
     * A status is considered informational when its code is between 100 and 200, 100 included.
     *
     * @return bool
     */
    protected function get_is_informational()
    {
        return $this->code >= 100 && $this->code < 200;
    }

    /**
     * Whether the status is successful.
     *
     * A status is considered successful when its code is between 200 and 300, 200 included.
     *
     * @return bool
     */
    protected function get_is_successful()
    {
        return $this->code >= 200 && $this->code < 300;
    }

    /**
     * Whether the status is a redirection.
     *
     * A status is considered to be a redirection when its code is between 300 and 400, 300
     * included.
     *
     * @return bool
     */
    protected function get_is_redirect()
    {
        return $this->code >= 300 && $this->code < 400;
    }

    /**
     * Whether the status is a client error.
     *
     * A status is considered a client error when its code is between 400 and 500, 400
     * included.
     *
     * @return bool
     */
    protected function get_is_client_error()
    {
        return $this->code >= 400 && $this->code < 500;
    }

    /**
     * Whether the status is a server error.
     *
     * A status is considered a server error when its code is between 500 and 600, 500
     * included.
     *
     * @return bool
     */
    protected function get_is_server_error()
    {
        return $this->code >= 500 && $this->code < 600;
    }

    /**
     * Whether the status is ok.
     *
     * A status is considered ok when its code is {@link OK}.
     *
     * @return bool
     */
    protected function get_is_ok()
    {
        return $this->code == self::OK;
    }

    /**
     * Whether the status is forbidden.
     *
     * A status is considered forbidden ok when its code is {@link FORBIDDEN}.
     *
     * @return bool
     */
    protected function get_is_forbidden()
    {
        return $this->code == self::FORBIDDEN;
    }

    /**
     * Whether the status is not found.
     *
     * A status is considered not found when its code is {@link NOT_FOUND}.
     *
     * @return bool
     */
    protected function get_is_not_found()
    {
        return $this->code == self::NOT_FOUND;
    }

    /**
     * Whether the status is empty.
     *
     * A status is considered empty when its code is {@link CREATED}, {@link NO_CONTENT} or
     * {@link NOT_MODIFIED}.
     *
     * @return bool
     */
    protected function get_is_empty()
    {
        static $range = [

            self::CREATED,
            self::NO_CONTENT,
            self::NOT_MODIFIED

        ];

        return in_array($this->code, $range);
    }

    /**
     * Whether the status is cacheable.
     *
     * @return bool
     */
    protected function get_is_cacheable()
    {
        static $range = [

            self::OK,
            self::NON_AUTHORITATIVE_INFORMATION,
            self::MULTIPLE_CHOICES,
            self::MOVED_PERMANENTLY,
            self::FOUND,
            self::NOT_FOUND,
            self::GONE

        ];

        return in_array($this->code, $range);
    }

    /**
     * Message describing the status code.
     *
     * @var string
     */
    private $message;

    protected function set_message($message)
    {
        $this->message = $message;
    }

    protected function get_message()
    {
        $message = $this->message;
        $code = $this->code;

        if (!$message && $code)
        {
            $message = self::$codes_and_messages[$code];
        }

        return $message;
    }

    /**
     * @param int $code
     * @param string|null $message
     */
    public function __construct($code = self::OK, $message = null)
    {
        self::assert_code_is_valid($code);

        $this->code = $code;
        $this->message = $message ?: self::$codes_and_messages[$code];
    }

    public function __toString()
    {
        return "$this->code " . $this->get_message();
    }
}
ICanBoogie/HTTP v2.5.0 API documentation generated by ApiGen