Message.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\net;
  9. use ReflectionClass;
  10. use ReflectionProperty;
  11. /**
  12. * Base message class for any URI based request/response.
  13. *
  14. * @see http://tools.ietf.org/html/rfc3986#section-1.1.1
  15. * @see http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax
  16. */
  17. class Message extends \lithium\core\Object {
  18. /**
  19. * The URI scheme.
  20. *
  21. * @var string
  22. */
  23. public $scheme = 'tcp';
  24. /**
  25. * The hostname for this endpoint.
  26. *
  27. * @var string
  28. */
  29. public $host = 'localhost';
  30. /**
  31. * The port for this endpoint.
  32. *
  33. * @var string
  34. */
  35. public $port = null;
  36. /**
  37. * Absolute path of the request.
  38. *
  39. * @var string
  40. */
  41. public $path = null;
  42. /**
  43. * The username for this endpoint.
  44. *
  45. * @var string
  46. */
  47. public $username = null;
  48. /**
  49. * The password for this endpoint.
  50. *
  51. * @var string
  52. */
  53. public $password = null;
  54. /**
  55. * The body of the message.
  56. *
  57. * @var array
  58. */
  59. public $body = array();
  60. /**
  61. * Adds config values to the public properties when a new object is created.
  62. *
  63. * @param array $config Configuration options : default value
  64. * - `scheme`: tcp
  65. * - `host`: localhost
  66. * - `port`: null
  67. * - `username`: null
  68. * - `password`: null
  69. * - `path`: null
  70. * - `body`: null
  71. */
  72. public function __construct(array $config = array()) {
  73. $defaults = array(
  74. 'scheme' => 'tcp',
  75. 'host' => 'localhost',
  76. 'port' => null,
  77. 'username' => null,
  78. 'password' => null,
  79. 'path' => null,
  80. 'body' => null
  81. );
  82. $config += $defaults;
  83. foreach (array_intersect_key(array_filter($config), $defaults) as $key => $value) {
  84. $this->{$key} = $value;
  85. }
  86. parent::__construct($config);
  87. }
  88. /**
  89. * Add body parts.
  90. *
  91. * @param mixed $data
  92. * @param array $options
  93. * - `'buffer'`: split the body string
  94. * @return array
  95. */
  96. public function body($data = null, $options = array()) {
  97. $default = array('buffer' => null);
  98. $options += $default;
  99. $this->body = array_merge((array) $this->body, (array) $data);
  100. $body = join("\r\n", $this->body);
  101. return ($options['buffer']) ? str_split($body, $options['buffer']) : $body;
  102. }
  103. /**
  104. * Converts the data in the record set to a different format, i.e. an array. Available
  105. * options: array, url, context, or string.
  106. *
  107. * @param string $format Format to convert to.
  108. * @param array $options
  109. * @return mixed
  110. */
  111. public function to($format, array $options = array()) {
  112. switch ($format) {
  113. case 'array':
  114. $array = array();
  115. $class = new ReflectionClass(get_class($this));
  116. foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
  117. $array[$prop->getName()] = $prop->getValue($this);
  118. }
  119. return $array;
  120. case 'url':
  121. $host = $this->host . ($this->port ? ":{$this->port}" : '');
  122. return "{$this->scheme}://{$host}{$this->path}";
  123. case 'context':
  124. $defaults = array('content' => $this->body(), 'ignore_errors' => true);
  125. return array($this->scheme => $options + $defaults);
  126. case 'string':
  127. default:
  128. return (string) $this;
  129. }
  130. }
  131. /**
  132. * Magic method to convert object to string.
  133. *
  134. * @return string
  135. */
  136. public function __toString() {
  137. return (string) $this->body();
  138. }
  139. }
  140. ?>