lithium\net\http\Message::__construct PHP Method

__construct() public method

Constructor. Adds config values to the public properties when a new object is created.
See also: lithium\net\Message::__construct()
public __construct ( array $config = [] ) : void
$config array The available configuration options are the following. Further options are inherited from the parent class. - `'protocol'` _string_: Defaults to `null`. - `'version'` _string_: Defaults to `'1.1'`. - `'scheme'` _string_: Overridden and defaulting to `'http'`. - `'headers'` _array_: Defaults to `array()`.
return void
    public function __construct(array $config = array())
    {
        $defaults = array('protocol' => null, 'version' => '1.1', 'scheme' => 'http', 'headers' => array());
        $config += $defaults;
        foreach (array_intersect_key(array_filter($config), $defaults) as $key => $value) {
            $this->{$key} = $value;
        }
        parent::__construct($config);
        if (strpos($this->host, '/') !== false) {
            list($this->host, $this->path) = explode('/', $this->host, 2);
        }
        $this->path = str_replace('//', '/', "/{$this->path}");
        $this->protocol = $this->protocol ?: "HTTP/{$this->version}";
    }

Usage Example

Beispiel #1
0
 /**
  * Adds config values to the public properties when a new object is created.
  *
  * @param array $config Configuration options : default value
  *        - `'protocol'` _string_: null
  *        - `'version'` _string_: '1.1'
  *        - `'headers'` _array_: array()
  *        - `'body'` _mixed_: null
  *        - `'message'` _string_: null
  *        - `'status'` _mixed_: null
  *        - `'type'` _string_: null
  */
 public function __construct(array $config = array())
 {
     $defaults = array('message' => null, 'status' => null, 'type' => null, 'cookies' => null);
     parent::__construct($config + $defaults);
     if ($this->_config['message']) {
         $this->body = $this->_parseMessage($this->_config['message']);
     }
     if ($this->headers('Transfer-Encoding')) {
         $this->body = $this->_httpChunkedDecode($this->body);
     }
     if ($status = $this->_config['status']) {
         $this->status($status);
     }
     if ($cookies = $this->headers('Set-Cookie')) {
         $this->_parseCookies($cookies);
     }
     if ($cookies = $this->_config['cookies']) {
         $this->cookies($cookies);
     }
     if ($type = $this->_config['type']) {
         $this->type($type);
     }
     if (!($header = $this->headers('Content-Type'))) {
         return;
     }
     $header = is_array($header) ? end($header) : $header;
     preg_match('/([-\\w\\/\\.+]+)(;\\s*?charset=(.+))?/i', $header, $match);
     if (isset($match[1])) {
         $this->type(trim($match[1]));
     }
     if (isset($match[3])) {
         $this->encoding = strtoupper(trim($match[3]));
     }
 }
All Usage Examples Of lithium\net\http\Message::__construct