@@ -13,5 +13,5 @@ |
||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | class InvalidOperationException extends \LogicException{ |
| 16 | - protected $code = 2; |
|
| 16 | + protected $code = 2; |
|
| 17 | 17 | } |
| 18 | 18 | \ No newline at end of file |
@@ -13,5 +13,5 @@ |
||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | class UnexpectedResponseException extends \UnexpectedValueException { |
| 16 | - protected $code = 3; |
|
| 16 | + protected $code = 3; |
|
| 17 | 17 | } |
| 18 | 18 | \ No newline at end of file |
@@ -13,5 +13,5 @@ |
||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | class InvalidArgumentException extends \LogicException{ |
| 16 | - protected $code = 1; |
|
| 16 | + protected $code = 1; |
|
| 17 | 17 | } |
| 18 | 18 | \ No newline at end of file |
@@ -15,81 +15,81 @@ |
||
| 15 | 15 | |
| 16 | 16 | class Response |
| 17 | 17 | { |
| 18 | - public |
|
| 19 | - $code, |
|
| 20 | - $errorCode, |
|
| 21 | - $error, |
|
| 22 | - $header, |
|
| 23 | - $body, |
|
| 24 | - /** |
|
| 25 | - * @var Request |
|
| 26 | - */ |
|
| 27 | - $request, |
|
| 28 | - $contentType, |
|
| 29 | - $charset, |
|
| 30 | - $duration, |
|
| 31 | - $info; |
|
| 32 | - protected function __construct() |
|
| 33 | - { |
|
| 34 | - } |
|
| 18 | + public |
|
| 19 | + $code, |
|
| 20 | + $errorCode, |
|
| 21 | + $error, |
|
| 22 | + $header, |
|
| 23 | + $body, |
|
| 24 | + /** |
|
| 25 | + * @var Request |
|
| 26 | + */ |
|
| 27 | + $request, |
|
| 28 | + $contentType, |
|
| 29 | + $charset, |
|
| 30 | + $duration, |
|
| 31 | + $info; |
|
| 32 | + protected function __construct() |
|
| 33 | + { |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public static function create(Request $request, $body, $info, $errorCode, $error){ |
|
| 37 | - $self = new self; |
|
| 38 | - $self->request = $request; |
|
| 39 | - $self->body = $body; |
|
| 40 | - $self->info = $info; |
|
| 41 | - $self->errorCode = $errorCode; |
|
| 42 | - $self->error = $error; |
|
| 43 | - $self->parse(); |
|
| 44 | - $self->check(); |
|
| 45 | - return $self; |
|
| 46 | - } |
|
| 47 | - public function check(){ |
|
| 36 | + public static function create(Request $request, $body, $info, $errorCode, $error){ |
|
| 37 | + $self = new self; |
|
| 38 | + $self->request = $request; |
|
| 39 | + $self->body = $body; |
|
| 40 | + $self->info = $info; |
|
| 41 | + $self->errorCode = $errorCode; |
|
| 42 | + $self->error = $error; |
|
| 43 | + $self->parse(); |
|
| 44 | + $self->check(); |
|
| 45 | + return $self; |
|
| 46 | + } |
|
| 47 | + public function check(){ |
|
| 48 | 48 | |
| 49 | - } |
|
| 50 | - public function parse(){ |
|
| 51 | - //has header |
|
| 52 | - if($this->request->getIni('header')){ |
|
| 53 | - $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); |
|
| 54 | - $this->body = substr($this->body, $this->info['header_size']); |
|
| 55 | - $headers = explode(PHP_EOL, $headers); |
|
| 56 | - array_shift($headers); // HTTP HEADER |
|
| 57 | - foreach($headers as $h) { |
|
| 58 | - if(false !== strpos($h, ':')) |
|
| 59 | - list($k, $v) = explode(':', $h, 2); |
|
| 60 | - else |
|
| 61 | - list($k, $v) = array($h,''); |
|
| 49 | + } |
|
| 50 | + public function parse(){ |
|
| 51 | + //has header |
|
| 52 | + if($this->request->getIni('header')){ |
|
| 53 | + $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); |
|
| 54 | + $this->body = substr($this->body, $this->info['header_size']); |
|
| 55 | + $headers = explode(PHP_EOL, $headers); |
|
| 56 | + array_shift($headers); // HTTP HEADER |
|
| 57 | + foreach($headers as $h) { |
|
| 58 | + if(false !== strpos($h, ':')) |
|
| 59 | + list($k, $v) = explode(':', $h, 2); |
|
| 60 | + else |
|
| 61 | + list($k, $v) = array($h,''); |
|
| 62 | 62 | |
| 63 | - $this->header[trim($k)] = trim($v); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - $this->code = $this->info['http_code']; |
|
| 67 | - $this->duration = $this->info['total_time']; |
|
| 68 | - $this->contentType = $this->info['content_type']; |
|
| 69 | - $content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; |
|
| 70 | - $content_type = explode(';', $content_type); |
|
| 71 | - $this->contentType = $content_type[0]; |
|
| 72 | - if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { |
|
| 73 | - list( , $this->charset) = explode('=', $content_type[1]); |
|
| 74 | - } |
|
| 75 | - } |
|
| 63 | + $this->header[trim($k)] = trim($v); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + $this->code = $this->info['http_code']; |
|
| 67 | + $this->duration = $this->info['total_time']; |
|
| 68 | + $this->contentType = $this->info['content_type']; |
|
| 69 | + $content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; |
|
| 70 | + $content_type = explode(';', $content_type); |
|
| 71 | + $this->contentType = $content_type[0]; |
|
| 72 | + if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { |
|
| 73 | + list( , $this->charset) = explode('=', $content_type[1]); |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Status Code Definitions |
|
| 79 | - * |
|
| 80 | - * Informational 1xx |
|
| 81 | - * Successful 2xx |
|
| 82 | - * Redirection 3xx |
|
| 83 | - * Client Error 4xx |
|
| 84 | - * Server Error 5xx |
|
| 85 | - * |
|
| 86 | - * http://pretty-rfc.herokuapp.com/RFC2616#status.codes |
|
| 87 | - * |
|
| 88 | - * @return bool Did we receive a 4xx or 5xx? |
|
| 89 | - */ |
|
| 90 | - public function hasErrors() |
|
| 91 | - { |
|
| 92 | - return $this->code == 0 || $this->code >= 400; |
|
| 93 | - } |
|
| 77 | + /** |
|
| 78 | + * Status Code Definitions |
|
| 79 | + * |
|
| 80 | + * Informational 1xx |
|
| 81 | + * Successful 2xx |
|
| 82 | + * Redirection 3xx |
|
| 83 | + * Client Error 4xx |
|
| 84 | + * Server Error 5xx |
|
| 85 | + * |
|
| 86 | + * http://pretty-rfc.herokuapp.com/RFC2616#status.codes |
|
| 87 | + * |
|
| 88 | + * @return bool Did we receive a 4xx or 5xx? |
|
| 89 | + */ |
|
| 90 | + public function hasErrors() |
|
| 91 | + { |
|
| 92 | + return $this->code == 0 || $this->code >= 400; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | } |
| 96 | 96 | \ No newline at end of file |
@@ -9,53 +9,53 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Mime |
| 11 | 11 | { |
| 12 | - const JSON = 'application/json'; |
|
| 13 | - const XML = 'application/xml'; |
|
| 14 | - const XHTML = 'application/html+xml'; |
|
| 15 | - const FORM = 'application/x-www-form-urlencoded'; |
|
| 16 | - const UPLOAD = 'multipart/form-data'; |
|
| 17 | - const PLAIN = 'text/plain'; |
|
| 18 | - const JS = 'text/javascript'; |
|
| 19 | - const HTML = 'text/html'; |
|
| 20 | - const YAML = 'application/x-yaml'; |
|
| 21 | - const CSV = 'text/csv'; |
|
| 12 | + const JSON = 'application/json'; |
|
| 13 | + const XML = 'application/xml'; |
|
| 14 | + const XHTML = 'application/html+xml'; |
|
| 15 | + const FORM = 'application/x-www-form-urlencoded'; |
|
| 16 | + const UPLOAD = 'multipart/form-data'; |
|
| 17 | + const PLAIN = 'text/plain'; |
|
| 18 | + const JS = 'text/javascript'; |
|
| 19 | + const HTML = 'text/html'; |
|
| 20 | + const YAML = 'application/x-yaml'; |
|
| 21 | + const CSV = 'text/csv'; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Map short name for a mime type |
|
| 25 | - * to a full proper mime type |
|
| 26 | - */ |
|
| 27 | - public static $mimes = array( |
|
| 28 | - 'json' => self::JSON, |
|
| 29 | - 'xml' => self::XML, |
|
| 30 | - 'form' => self::FORM, |
|
| 31 | - 'plain' => self::PLAIN, |
|
| 32 | - 'text' => self::PLAIN, |
|
| 33 | - 'upload' => self::UPLOAD, |
|
| 34 | - 'html' => self::HTML, |
|
| 35 | - 'xhtml' => self::XHTML, |
|
| 36 | - 'js' => self::JS, |
|
| 37 | - 'javascript'=> self::JS, |
|
| 38 | - 'yaml' => self::YAML, |
|
| 39 | - 'csv' => self::CSV, |
|
| 40 | - ); |
|
| 23 | + /** |
|
| 24 | + * Map short name for a mime type |
|
| 25 | + * to a full proper mime type |
|
| 26 | + */ |
|
| 27 | + public static $mimes = array( |
|
| 28 | + 'json' => self::JSON, |
|
| 29 | + 'xml' => self::XML, |
|
| 30 | + 'form' => self::FORM, |
|
| 31 | + 'plain' => self::PLAIN, |
|
| 32 | + 'text' => self::PLAIN, |
|
| 33 | + 'upload' => self::UPLOAD, |
|
| 34 | + 'html' => self::HTML, |
|
| 35 | + 'xhtml' => self::XHTML, |
|
| 36 | + 'js' => self::JS, |
|
| 37 | + 'javascript'=> self::JS, |
|
| 38 | + 'yaml' => self::YAML, |
|
| 39 | + 'csv' => self::CSV, |
|
| 40 | + ); |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Get the full Mime Type name from a "short name". |
|
| 44 | - * Returns the short if no mapping was found. |
|
| 45 | - * @param string $short_name common name for mime type (e.g. json) |
|
| 46 | - * @return string full mime type (e.g. application/json) |
|
| 47 | - */ |
|
| 48 | - public static function getFullMime($short_name) |
|
| 49 | - { |
|
| 50 | - return array_key_exists($short_name, self::$mimes) ? self::$mimes[$short_name] : $short_name; |
|
| 51 | - } |
|
| 42 | + /** |
|
| 43 | + * Get the full Mime Type name from a "short name". |
|
| 44 | + * Returns the short if no mapping was found. |
|
| 45 | + * @param string $short_name common name for mime type (e.g. json) |
|
| 46 | + * @return string full mime type (e.g. application/json) |
|
| 47 | + */ |
|
| 48 | + public static function getFullMime($short_name) |
|
| 49 | + { |
|
| 50 | + return array_key_exists($short_name, self::$mimes) ? self::$mimes[$short_name] : $short_name; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @param string $short_name |
|
| 55 | - * @return bool |
|
| 56 | - */ |
|
| 57 | - public static function supportsMimeType($short_name) |
|
| 58 | - { |
|
| 59 | - return array_key_exists($short_name, self::$mimes); |
|
| 60 | - } |
|
| 53 | + /** |
|
| 54 | + * @param string $short_name |
|
| 55 | + * @return bool |
|
| 56 | + */ |
|
| 57 | + public static function supportsMimeType($short_name) |
|
| 58 | + { |
|
| 59 | + return array_key_exists($short_name, self::$mimes); |
|
| 60 | + } |
|
| 61 | 61 | } |
@@ -12,26 +12,26 @@ |
||
| 12 | 12 | namespace MultiHttp; |
| 13 | 13 | |
| 14 | 14 | trait RequestTrait { |
| 15 | - protected $expectContentType = null; |
|
| 16 | - public function expectsJson(){ |
|
| 17 | - $this->expectContentType = Mime::JSON; |
|
| 18 | - } |
|
| 19 | - public function expectsXml(){ |
|
| 20 | - $this->expectContentType = Mime::XML; |
|
| 21 | - } |
|
| 15 | + protected $expectContentType = null; |
|
| 16 | + public function expectsJson(){ |
|
| 17 | + $this->expectContentType = Mime::JSON; |
|
| 18 | + } |
|
| 19 | + public function expectsXml(){ |
|
| 20 | + $this->expectContentType = Mime::XML; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - protected function json(){ |
|
| 23 | + protected function json(){ |
|
| 24 | 24 | |
| 25 | - } |
|
| 26 | - protected function unJson(){ |
|
| 25 | + } |
|
| 26 | + protected function unJson(){ |
|
| 27 | 27 | |
| 28 | - } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - protected function xml(){ |
|
| 30 | + protected function xml(){ |
|
| 31 | 31 | |
| 32 | - } |
|
| 33 | - protected function unXml(){ |
|
| 32 | + } |
|
| 33 | + protected function unXml(){ |
|
| 34 | 34 | |
| 35 | - } |
|
| 35 | + } |
|
| 36 | 36 | } |
| 37 | 37 | |
@@ -19,70 +19,70 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | trait JsonTrait |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @return Request |
|
| 24 | - */ |
|
| 25 | - public function expectsJson() |
|
| 26 | - { |
|
| 27 | - return $this->expectsMime('json'); |
|
| 28 | - } |
|
| 22 | + /** |
|
| 23 | + * @return Request |
|
| 24 | + */ |
|
| 25 | + public function expectsJson() |
|
| 26 | + { |
|
| 27 | + return $this->expectsMime('json'); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @return Request |
|
| 32 | - */ |
|
| 33 | - public function sendJson() |
|
| 34 | - { |
|
| 35 | - return $this->sendMime('json'); |
|
| 36 | - } |
|
| 30 | + /** |
|
| 31 | + * @return Request |
|
| 32 | + */ |
|
| 33 | + public function sendJson() |
|
| 34 | + { |
|
| 35 | + return $this->sendMime('json'); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param $body |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function json($body) |
|
| 43 | - { |
|
| 44 | - return json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
|
| 45 | - } |
|
| 38 | + /** |
|
| 39 | + * @param $body |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function json($body) |
|
| 43 | + { |
|
| 44 | + return json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @param $body |
|
| 49 | - * @return mixed |
|
| 50 | - */ |
|
| 51 | - public function unJson($body) |
|
| 52 | - { |
|
| 53 | - $parsed = json_decode($body, true); |
|
| 54 | - if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
| 55 | - return $parsed; |
|
| 56 | - } |
|
| 47 | + /** |
|
| 48 | + * @param $body |
|
| 49 | + * @return mixed |
|
| 50 | + */ |
|
| 51 | + public function unJson($body) |
|
| 52 | + { |
|
| 53 | + $parsed = json_decode($body, true); |
|
| 54 | + if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
| 55 | + return $parsed; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @return string |
|
| 60 | - */ |
|
| 61 | - private static function jsonLastErrorMsg(){ |
|
| 62 | - if(function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
| 63 | - switch (json_last_error()) { |
|
| 64 | - case JSON_ERROR_NONE: |
|
| 65 | - return ' - No errors'; |
|
| 66 | - break; |
|
| 67 | - case JSON_ERROR_DEPTH: |
|
| 68 | - return ' - Maximum stack depth exceeded'; |
|
| 69 | - break; |
|
| 70 | - case JSON_ERROR_STATE_MISMATCH: |
|
| 71 | - return ' - Underflow or the modes mismatch'; |
|
| 72 | - break; |
|
| 73 | - case JSON_ERROR_CTRL_CHAR: |
|
| 74 | - return ' - Unexpected control character found'; |
|
| 75 | - break; |
|
| 76 | - case JSON_ERROR_SYNTAX: |
|
| 77 | - return ' - Syntax error, malformed JSON'; |
|
| 78 | - break; |
|
| 79 | - case JSON_ERROR_UTF8: |
|
| 80 | - return ' - Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
| 81 | - break; |
|
| 82 | - default: |
|
| 83 | - return ' - Unknown error'; |
|
| 84 | - break; |
|
| 85 | - } |
|
| 86 | - } |
|
| 58 | + /** |
|
| 59 | + * @return string |
|
| 60 | + */ |
|
| 61 | + private static function jsonLastErrorMsg(){ |
|
| 62 | + if(function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
| 63 | + switch (json_last_error()) { |
|
| 64 | + case JSON_ERROR_NONE: |
|
| 65 | + return ' - No errors'; |
|
| 66 | + break; |
|
| 67 | + case JSON_ERROR_DEPTH: |
|
| 68 | + return ' - Maximum stack depth exceeded'; |
|
| 69 | + break; |
|
| 70 | + case JSON_ERROR_STATE_MISMATCH: |
|
| 71 | + return ' - Underflow or the modes mismatch'; |
|
| 72 | + break; |
|
| 73 | + case JSON_ERROR_CTRL_CHAR: |
|
| 74 | + return ' - Unexpected control character found'; |
|
| 75 | + break; |
|
| 76 | + case JSON_ERROR_SYNTAX: |
|
| 77 | + return ' - Syntax error, malformed JSON'; |
|
| 78 | + break; |
|
| 79 | + case JSON_ERROR_UTF8: |
|
| 80 | + return ' - Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
| 81 | + break; |
|
| 82 | + default: |
|
| 83 | + return ' - Unknown error'; |
|
| 84 | + break; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | 87 | } |
| 88 | 88 | |
@@ -14,12 +14,12 @@ |
||
| 14 | 14 | |
| 15 | 15 | class Helper |
| 16 | 16 | { |
| 17 | - public static function retry($maxTimes = 2, callable $task, $sleep = 0){ |
|
| 18 | - $tryTimes = 0; |
|
| 19 | - while(++$tryTimes <= $maxTimes){ |
|
| 20 | - if($task()) break; |
|
| 21 | - else usleep(abs($sleep) * 1000000); |
|
| 22 | - } |
|
| 23 | - return $tryTimes; |
|
| 24 | - } |
|
| 17 | + public static function retry($maxTimes = 2, callable $task, $sleep = 0){ |
|
| 18 | + $tryTimes = 0; |
|
| 19 | + while(++$tryTimes <= $maxTimes){ |
|
| 20 | + if($task()) break; |
|
| 21 | + else usleep(abs($sleep) * 1000000); |
|
| 22 | + } |
|
| 23 | + return $tryTimes; |
|
| 24 | + } |
|
| 25 | 25 | } |
| 26 | 26 | \ No newline at end of file |
@@ -47,11 +47,11 @@ |
||
| 47 | 47 | |
| 48 | 48 | abstract function trace($uri, array $options = array()); |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param $method |
|
| 52 | - * @return bool |
|
| 53 | - */ |
|
| 54 | - public static function hasBody($method){ |
|
| 55 | - return in_array($method, array(self::POST, self::PUT, self::PATCH, self::OPTIONS)); |
|
| 56 | - } |
|
| 50 | + /** |
|
| 51 | + * @param $method |
|
| 52 | + * @return bool |
|
| 53 | + */ |
|
| 54 | + public static function hasBody($method){ |
|
| 55 | + return in_array($method, array(self::POST, self::PUT, self::PATCH, self::OPTIONS)); |
|
| 56 | + } |
|
| 57 | 57 | } |
| 58 | 58 | \ No newline at end of file |