Skip to content
This repository
Newer
Older
100644 157 lines (140 sloc) 3.291 kb
077ba19c »
2010-07-10 adding '\net\Message` for the basic request/response from `\net\Socket`.
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
4f1a9c0d »
2011-03-28 Updating copyright year.
5 * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
077ba19c »
2010-07-10 adding '\net\Message` for the basic request/response from `\net\Socket`.
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
8
9 namespace lithium\net;
10
621ef258 »
2011-04-17 Changing `\action\Request` to extend `\net\http\Request`, and overrid…
11 use ReflectionClass;
12 use ReflectionProperty;
13
077ba19c »
2010-07-10 adding '\net\Message` for the basic request/response from `\net\Socket`.
14 /**
15 * Base message class for any URI based request/response.
16 * @see http://tools.ietf.org/html/rfc3986#section-1.1.1
17 * @see http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax
18 *
19 */
20 class Message extends \lithium\core\Object {
21
22 /**
23 * The uri scheme
24 *
25 * @var string
26 */
27 public $scheme = 'tcp';
28
29 /**
30 * The hostname for this endpoint.
31 *
32 * @var string
33 */
34 public $host = 'localhost';
35
36 /**
37 * The port
38 *
39 * @var string
40 */
41 public $port = null;
42
43 /**
44 * Absolute path of the request.
45 *
46 * @var string
47 */
48 public $path = null;
49
50 /**
51 * The username
52 *
53 * @var string
54 */
55 public $username = null;
56
57 /**
58 * Absolute path of the request.
59 *
60 * @var string
61 */
62 public $password = null;
63
64 /**
65 * The body of the message.
66 *
67 * @var array
68 */
69 public $body = array();
70
71 /**
72 * Adds config values to the public properties when a new object is created.
73 *
f453b39b »
2011-07-20 BREAK: Fixes #387 on dev.lithify.me. Changes `net\http\Request::$para…
74 * @param array $config Configuration options : default value
75 * - `scheme`: tcp
76 * - `host`: localhost
77 * - `port`: null
78 * - `username`: null
79 * - `password`: null
80 * - `path`: null
81 * - `body`: null
077ba19c »
2010-07-10 adding '\net\Message` for the basic request/response from `\net\Socket`.
82 */
83 public function __construct(array $config = array()) {
84 $defaults = array(
85 'scheme' => 'tcp',
86 'host' => 'localhost',
87 'port' => null,
88 'username' => null,
89 'password' => null,
f453b39b »
2011-07-20 BREAK: Fixes #387 on dev.lithify.me. Changes `net\http\Request::$para…
90 'path' => null,
91 'body' => null
077ba19c »
2010-07-10 adding '\net\Message` for the basic request/response from `\net\Socket`.
92 );
93 $config += $defaults;
94
aad6df22 »
2010-07-10 fixing up some broken code related to changes in `\net\*`
95 foreach (array_filter($config) as $key => $value) {
077ba19c »
2010-07-10 adding '\net\Message` for the basic request/response from `\net\Socket`.
96 $this->{$key} = $value;
97 }
98 parent::__construct($config);
99 }
100
101 /**
102 * Add body parts.
103 *
104 * @param mixed $data
105 * @param array $options
106 * - `'buffer'`: split the body string
107 * @return array
108 */
109 public function body($data = null, $options = array()) {
110 $default = array('buffer' => null);
111 $options += $default;
112 $this->body = array_merge((array) $this->body, (array) $data);
27e8f652 »
2011-07-26 `\net\Message` should not trim the body.
113 $body = join("\r\n", $this->body);
077ba19c »
2010-07-10 adding '\net\Message` for the basic request/response from `\net\Socket`.
114 return ($options['buffer']) ? str_split($body, $options['buffer']) : $body;
115 }
116
117 /**
118 * Converts the data in the record set to a different format, i.e. an array. Available
119 * options: array, url, context, or string.
120 *
121 * @param string $format Format to convert to.
122 * @param array $options
123 * @return mixed
124 */
125 public function to($format, array $options = array()) {
126 switch ($format) {
127 case 'array':
128 $array = array();
621ef258 »
2011-04-17 Changing `\action\Request` to extend `\net\http\Request`, and overrid…
129 $class = new ReflectionClass(get_class($this));
130
131 foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
077ba19c »
2010-07-10 adding '\net\Message` for the basic request/response from `\net\Socket`.
132 $array[$prop->getName()] = $prop->getValue($this);
133 }
134 return $array;
135 case 'url':
136 $host = $this->host . ($this->port ? ":{$this->port}" : '');
137 return "{$this->scheme}://{$host}{$this->path}";
138 case 'context':
139 $defaults = array('content' => $this->body(), 'ignore_errors' => true);
140 return array($this->scheme => $options + $defaults);
141 case 'string':
142 default:
143 return (string) $this;
144 }
145 }
146
147 /**
148 * Magic method to convert object to string.
149 *
150 * @return string
151 */
152 public function __toString() {
153 return (string) $this->body();
154 }
155 }
156
157 ?>
Something went wrong with that request. Please try again.