Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 157 lines (140 sloc) 3.332 kb
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
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\net;

use ReflectionClass;
use ReflectionProperty;

/**
* Base message class for any URI based request/response.
*
* @see http://tools.ietf.org/html/rfc3986#section-1.1.1
* @see http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax
*/
class Message extends \lithium\core\Object {

/**
* The URI scheme.
*
* @var string
*/
public $scheme = 'tcp';

/**
* The hostname for this endpoint.
*
* @var string
*/
public $host = 'localhost';

/**
* The port for this endpoint.
*
* @var string
*/
public $port = null;

/**
* Absolute path of the request.
*
* @var string
*/
public $path = null;

/**
* The username for this endpoint.
*
* @var string
*/
public $username = null;

/**
* The password for this endpoint.
*
* @var string
*/
public $password = null;

/**
* The body of the message.
*
* @var array
*/
public $body = array();

/**
* Adds config values to the public properties when a new object is created.
*
* @param array $config Configuration options : default value
* - `scheme`: tcp
* - `host`: localhost
* - `port`: null
* - `username`: null
* - `password`: null
* - `path`: null
* - `body`: null
*/
public function __construct(array $config = array()) {
$defaults = array(
'scheme' => 'tcp',
'host' => 'localhost',
'port' => null,
'username' => null,
'password' => null,
'path' => null,
'body' => null
);
$config += $defaults;

foreach (array_filter($config) as $key => $value) {
$this->{$key} = $value;
}
parent::__construct($config);
}

/**
* Add body parts.
*
* @param mixed $data
* @param array $options
* - `'buffer'`: split the body string
* @return array
*/
public function body($data = null, $options = array()) {
$default = array('buffer' => null);
$options += $default;
$this->body = array_merge((array) $this->body, (array) $data);
$body = join("\r\n", $this->body);
return ($options['buffer']) ? str_split($body, $options['buffer']) : $body;
}

/**
* Converts the data in the record set to a different format, i.e. an array. Available
* options: array, url, context, or string.
*
* @param string $format Format to convert to.
* @param array $options
* @return mixed
*/
public function to($format, array $options = array()) {
switch ($format) {
case 'array':
$array = array();
$class = new ReflectionClass(get_class($this));

foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
$array[$prop->getName()] = $prop->getValue($this);
}
return $array;
case 'url':
$host = $this->host . ($this->port ? ":{$this->port}" : '');
return "{$this->scheme}://{$host}{$this->path}";
case 'context':
$defaults = array('content' => $this->body(), 'ignore_errors' => true);
return array($this->scheme => $options + $defaults);
case 'string':
default:
return (string) $this;
}
}

/**
* Magic method to convert object to string.
*
* @return string
*/
public function __toString() {
return (string) $this->body();
}
}

?>
Something went wrong with that request. Please try again.