Skip to content

Commit

Permalink
split some class. remove inhere\library package
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 21, 2017
1 parent 6514ba7 commit 58da918
Show file tree
Hide file tree
Showing 21 changed files with 1,785 additions and 967 deletions.
10 changes: 9 additions & 1 deletion src/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
*
* @link https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php
*/
class Body extends Stream
class Body extends TempStream
{
/**
* TempStream constructor.
* @param string $mode
*/
public function __construct($mode = 'rb+')
{
parent::__construct($mode);
}
}
108 changes: 108 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017/10/21
* Time: 下午2:04
*/

namespace Inhere\Http;

/**
* Class Collection
* @package Inhere\Http
*/
class Collection extends \ArrayObject
{
public function sets(array $values)
{
$this->replace($values);
}

/**
* @param array $items
*/
public function replace(array $items)
{
foreach ($items as $key => $value) {
$this->set($key, $value);
}
}

/**
* @param string $name
* @param null $default
* @return mixed|null
*/
public function get(string $name, $default = null)
{
return $this[$name] ?? $default;
}

/**
* @param string $name
* @param mixed $value
* @return mixed|null
*/
public function add($name, $value)
{
if (isset($this[$name])) {
return null;
}

$this[$name] = $value;

return $this;
}

/**
* @param string $name
* @param mixed $value
* @return mixed|null
*/
public function set($name, $value)
{
return $this[$name] = $value;
}

/**
* {@inheritDoc}
*/
public function all()
{
return $this->getArrayCopy();
}

/**
* {@inheritDoc}
*/
public function has(string $key)
{
return array_key_exists($key, $this->data);
}

/**
* {@inheritDoc}
*/
public function remove($key)
{
if (isset($this[$key])) {
$val = $this[$key];
unset($this[$key]);

return $val;
}

return null;
}

/**
* clear all data
*/
public function clear()
{
foreach ($this as $key) {
unset($this[$key]);
}
}
}
10 changes: 4 additions & 6 deletions src/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

namespace Inhere\Http;

use Inhere\Library\Collections\SimpleCollection;

/**
* Class Cookies
* @package Inhere\Http
*/
class Cookies extends SimpleCollection
class Cookies extends Collection
{
/**
* Cookies
Expand Down Expand Up @@ -74,7 +72,7 @@ public function set($name, $value)
public function toHeaders()
{
$headers = [];
foreach ($this->data as $name => $properties) {
foreach ($this as $name => $properties) {
$headers[] = $this->toHeader($name, $properties);
}

Expand Down Expand Up @@ -133,7 +131,7 @@ public function toRequestHeader()
{
$cookieValue = '';

foreach ($this->data as $name => $value) {
foreach ($this as $name => $value) {
$cookieValue .= urlencode($name) . '=' . urlencode($value['value']) . '; ';
}

Expand All @@ -154,7 +152,7 @@ public static function parseFromRawHeader($cookieText)
if (is_array($cookieText)) {
$cookieText = array_shift($cookieText) ?: '';
}

if (!is_string($cookieText)) {
throw new \InvalidArgumentException('Cannot parse Cookie data. Header value must be a string.');
}
Expand Down
102 changes: 102 additions & 0 deletions src/CookiesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017/10/21
* Time: 下午1:18
*/

namespace Inhere\Http;

/**
* Trait CookiesTrait
* @package Inhere\Http
*/
trait CookiesTrait
{
/**
* @var Cookies
*/
private $cookies;

/*******************************************************************************
* Cookies
******************************************************************************/

/**
* @inheritdoc
*/
public function getCookieParams()
{
return $this->cookies->all();
}

/**
* @inheritdoc
*/
public function getCookieParam($key, $default = null)
{
return $this->cookies->get($key, $default);
}

/**
* @inheritdoc
*/
public function withCookieParams(array $cookies)
{
$clone = clone $this;
$clone->cookies = new Cookies($cookies);

return $clone;
}

/**
* @param string $name
* @param string|array $value
* @return $this
*/
public function setCookie(string $name, $value)
{
$this->cookies->set($name, $value);

return $this;
}

/**
* @return Cookies
*/
public function getCookies(): Cookies
{
return $this->cookies;
}

/**
* @param Cookies|array $cookies
* @return $this
*/
public function setCookies($cookies)
{
if (is_array($cookies)) {
return $this->setCookiesFromArray($cookies);
}

$this->cookies = $cookies;

return $this;
}

/**
* @param array $cookies
* @return $this
*/
public function setCookiesFromArray(array $cookies)
{
if (!$this->cookies) {
$this->cookies = new Cookies($cookies);
} else {
$this->cookies->sets($cookies);
}

return $this;
}
}
40 changes: 23 additions & 17 deletions src/Extra/ExtendedRequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

use Inhere\Http\UploadedFile;
use Inhere\Http\Uri;
use Inhere\Library\Helpers\PhpHelper;
use Inhere\Validate\FilterList;
use Inhere\Library\Types;

/**
* trait ExtendedRequestTrait
Expand Down Expand Up @@ -53,11 +51,19 @@
*/
trait ExtendedRequestTrait
{
/**
* return raw data
*/
/** @var string */
private static $rawFilter = 'raw';

/** @var array */
private static $phpTypes = [
'int', 'integer',
'float', 'double',
'bool', 'boolean',
'string',

'array', 'object', 'resource'
];

/**
* @var array
*/
Expand Down Expand Up @@ -236,7 +242,7 @@ public function filtering($value, $filter = null)

// is custom callable filter
if (is_callable($filter)) {
$result = PhpHelper::call($filter, $value);
$result = $filter($value);
}

return $result;
Expand All @@ -245,29 +251,29 @@ public function filtering($value, $filter = null)
// is a defined filter
$filter = self::$filterList[$filter];

if (!in_array($filter, Types::all(), true)) {
$result = PhpHelper::call($filter, $value);
if (!in_array($filter, self::$phpTypes, true)) {
$result = $filter($value);
} else {
switch (lcfirst(trim($filter))) {
case Types::T_BOOL :
case Types::T_BOOLEAN :
case 'bool':
case 'boolean':
$result = (bool)$value;
break;
case Types::T_DOUBLE :
case Types::T_FLOAT :
case 'double':
case 'float':
$result = (float)$value;
break;
case Types::T_INT :
case Types::T_INTEGER :
case 'int' :
case 'integer':
$result = (int)$value;
break;
case Types::T_STRING :
case 'string':
$result = (string)$value;
break;
case Types::T_ARRAY :
case 'array':
$result = (array)$value;
break;
case Types::T_OBJECT :
case 'object':
$result = (object)$value;
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion src/Extra/ExtendedResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function withRedirect($url, $status = null)
*/
public function withJson($data, $status = null, $encodingOptions = 0)
{
$response = $this->withBody(new Body(fopen('php://temp', 'rb+')));
$response = $this->withBody(new Body());
$response->body->write($json = json_encode($data, $encodingOptions));

// Ensure that the json encoding passed successfully
Expand Down
8 changes: 3 additions & 5 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

namespace Inhere\Http;

use Inhere\Library\Collections\SimpleCollection;

/**
* Class Headers
* @package Inhere\Http
*/
class Headers extends SimpleCollection
class Headers extends Collection
{
/**
* the connection header line data end char
Expand Down Expand Up @@ -195,7 +193,7 @@ public function toHeaderLines($toString = false)
{
$output = [];

foreach ($this->data as $name => $info) {
foreach ($this as $name => $info) {
$name = ucwords($name, '-');
$value = implode(',', $info['value']);
$output[] = "$name: $value\r\n";
Expand All @@ -211,7 +209,7 @@ public function getLines()
{
$output = [];

foreach ($this->data as $name => $info) {
foreach ($this as $name => $info) {
$name = ucwords($name, '-');
$output[$name] = implode(',', $info['value']);
}
Expand Down
Loading

0 comments on commit 58da918

Please sign in to comment.