Skip to content

Commit

Permalink
Причесывание
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed May 24, 2021
1 parent bbc4e28 commit fa2b2b6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 82 deletions.
95 changes: 47 additions & 48 deletions src/Services/PSR/PSR7/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
*/
class Message implements MessageInterface
{
const DEFAULT_HTTP_VERSION = '1.1';
protected const DEFAULT_HTTP_VERSION = '1.1';

/**
* @var HttpRequest
* @var HttpRequest $request
*/
protected $request;

/**
* @var string
* @var string $httpVersion
*/
protected $httpVersion;

Expand All @@ -33,15 +33,15 @@ class Message implements MessageInterface
*/

protected $body;

/**
* @var UriInterface
* @var UriInterface $uri
*/

protected $uri;

/**
* @var array
* @var array $attributes
*/

protected $attributes;

/**
Expand All @@ -57,7 +57,7 @@ public function __construct(
string $httpVersion = null,
$body = null,
array $attributes = []
){
) {
$this->request = $request;
$this->httpVersion = $httpVersion;
$this->body = $body;
Expand All @@ -73,27 +73,7 @@ public function __construct(
}

/**
* @param HttpRequest $request
*
* @return boolean
*/
private function needCheckBody(HttpRequest $request)
{
$method = strtolower($request->getRequestMethod());
return in_array($method, ['post', 'put']);
}

private function getCurrentLink()
{
$server = $this->request->getServer();
return ($server->get('HTTPS') === 'on' ? "https" : "http").
"://".
$server->get('HTTP_HOST').
$server->get('REQUEST_URI');
}

/**
* @return string
* @inheritDoc
*/
public function getProtocolVersion()
{
Expand All @@ -105,18 +85,16 @@ public function getProtocolVersion()
return $this->httpVersion = str_replace(['HTTP', '/'], '', $version);
}


/**
* @param string $version
* @return $this|Message
* @inheritDoc
*/
public function withProtocolVersion($version)
{
return new static($this->request, $version, $this->body, $this->attributes);
}

/**
* @return array|string[][]
* @inheritDoc
*/
public function getHeaders()
{
Expand All @@ -130,22 +108,23 @@ public function getHeaders()
}

/**
* @param string $name
* @return bool
* @inheritDoc
*/
public function hasHeader($name)
{
return !empty($this->getHeader($name));
}

/**
* @inheritDoc
*/
public function getHeader($name)
{
return (array)($this->request->getHeader($name) ?? []);
}

/**
* @param string $name
* @return string
* @inheritDoc
*/
public function getHeaderLine($name)
{
Expand All @@ -158,9 +137,7 @@ public function getHeaderLine($name)
}

/**
* @param string $name
* @param string|string[] $value
* @return $this|Message
* @inheritDoc
*/
public function withHeader($name, $value)
{
Expand All @@ -170,9 +147,7 @@ public function withHeader($name, $value)
}

/**
* @param string $name
* @param string|string[] $value
* @return $this|Message
* @inheritDoc
*/
public function withAddedHeader($name, $value)
{
Expand All @@ -187,8 +162,7 @@ public function withAddedHeader($name, $value)
}

/**
* @param string $name
* @return $this|Message
* @inheritDoc
*/
public function withoutHeader($name)
{
Expand All @@ -203,7 +177,7 @@ public function withoutHeader($name)
}

/**
* @return StreamInterface
* @inheritDoc
*/
public function getBody()
{
Expand All @@ -215,8 +189,7 @@ public function getBody()
}

/**
* @param StreamInterface $body
* @return $this|Message
* @inheritDoc
*/
public function withBody(StreamInterface $body)
{
Expand All @@ -226,4 +199,30 @@ public function withBody(StreamInterface $body)

return new static($this->request, $this->httpVersion, $body, $this->attributes);
}

/**
* @param HttpRequest $request Битриксовый Request.
*
* @return boolean
*/
private function needCheckBody(HttpRequest $request)
{
$method = strtolower($request->getRequestMethod());

return in_array($method, ['post', 'put']);
}

/**
* Текущий URL.
*
* @return string
*/
private function getCurrentLink() : string
{
$server = $this->request->getServer();
return ($server->get('HTTPS') === 'on' ? 'https' : 'http').
'://'.
$server->get('HTTP_HOST').
$server->get('REQUEST_URI');
}
}
18 changes: 6 additions & 12 deletions src/Services/PSR/PSR7/PsrRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
class PsrRequest extends Message implements RequestInterface
{
/**
* @return string
* @inheritDoc
*/
public function getRequestTarget()
{
return (string)$this->request->getRequestUri();
}

/**
* @param mixed $requestTarget
*
* @return $this|PsrRequest
* @inheritDoc
*/
public function withRequestTarget($requestTarget)
{
Expand All @@ -34,17 +32,15 @@ public function withRequestTarget($requestTarget)
}

/**
* @return string|null
* @inheritDoc
*/
public function getMethod()
{
return $this->request->getRequestMethod();
}

/**
* @param string $method
*
* @return $this|PsrRequest
* @inheritDoc
*/
public function withMethod($method)
{
Expand All @@ -55,17 +51,15 @@ public function withMethod($method)
}

/**
* @return UriInterface
* @inheritDoc
*/
public function getUri()
{
return $this->uri;
}

/**
* @param UriInterface $uri
* @param false $preserveHost
* @return $this|PsrRequest
* @inheritDoc
*/
public function withUri(UriInterface $uri, $preserveHost = false)
{
Expand Down
43 changes: 21 additions & 22 deletions src/Services/PSR/PSR7/ServerPsrRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use GuzzleHttp\Psr7\UploadedFile;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;

/**
* Class ServerPsrRequest
Expand Down Expand Up @@ -88,27 +87,6 @@ public function getUploadedFiles()
}, $this->request->getFileList()->toArray());
}

/**
* @return array
*/
private function getFileList(): array
{
$fileList = [];
foreach ($this->request->getFileList() as $key => $file) {
foreach ($file as $k => $value) {
if (is_array($value)) {
foreach ($value as $i => $v) {
$fileList[$key][$i][$k] = $v;
}
} else {
$fileList[$key][$k] = $v;
}
}
}

return $fileList;
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -184,4 +162,25 @@ public function withoutAttribute($attribute): ServerRequestInterface

return $new;
}

/**
* @return array
*/
private function getFileList(): array
{
$fileList = [];
foreach ($this->request->getFileList() as $key => $file) {
foreach ($file as $k => $value) {
if (is_array($value)) {
foreach ($value as $i => $v) {
$fileList[$key][$i][$k] = $v;
}
} else {
$fileList[$key][$k] = $v;
}
}
}

return $fileList;
}
}

0 comments on commit fa2b2b6

Please sign in to comment.