Skip to content

Commit 58da918

Browse files
committed
split some class. remove inhere\library package
1 parent 6514ba7 commit 58da918

21 files changed

+1785
-967
lines changed

src/Body.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
*
1616
* @link https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php
1717
*/
18-
class Body extends Stream
18+
class Body extends TempStream
1919
{
20+
/**
21+
* TempStream constructor.
22+
* @param string $mode
23+
*/
24+
public function __construct($mode = 'rb+')
25+
{
26+
parent::__construct($mode);
27+
}
2028
}

src/Collection.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/10/21
6+
* Time: 下午2:04
7+
*/
8+
9+
namespace Inhere\Http;
10+
11+
/**
12+
* Class Collection
13+
* @package Inhere\Http
14+
*/
15+
class Collection extends \ArrayObject
16+
{
17+
public function sets(array $values)
18+
{
19+
$this->replace($values);
20+
}
21+
22+
/**
23+
* @param array $items
24+
*/
25+
public function replace(array $items)
26+
{
27+
foreach ($items as $key => $value) {
28+
$this->set($key, $value);
29+
}
30+
}
31+
32+
/**
33+
* @param string $name
34+
* @param null $default
35+
* @return mixed|null
36+
*/
37+
public function get(string $name, $default = null)
38+
{
39+
return $this[$name] ?? $default;
40+
}
41+
42+
/**
43+
* @param string $name
44+
* @param mixed $value
45+
* @return mixed|null
46+
*/
47+
public function add($name, $value)
48+
{
49+
if (isset($this[$name])) {
50+
return null;
51+
}
52+
53+
$this[$name] = $value;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* @param string $name
60+
* @param mixed $value
61+
* @return mixed|null
62+
*/
63+
public function set($name, $value)
64+
{
65+
return $this[$name] = $value;
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function all()
72+
{
73+
return $this->getArrayCopy();
74+
}
75+
76+
/**
77+
* {@inheritDoc}
78+
*/
79+
public function has(string $key)
80+
{
81+
return array_key_exists($key, $this->data);
82+
}
83+
84+
/**
85+
* {@inheritDoc}
86+
*/
87+
public function remove($key)
88+
{
89+
if (isset($this[$key])) {
90+
$val = $this[$key];
91+
unset($this[$key]);
92+
93+
return $val;
94+
}
95+
96+
return null;
97+
}
98+
99+
/**
100+
* clear all data
101+
*/
102+
public function clear()
103+
{
104+
foreach ($this as $key) {
105+
unset($this[$key]);
106+
}
107+
}
108+
}

src/Cookies.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111
namespace Inhere\Http;
1212

13-
use Inhere\Library\Collections\SimpleCollection;
14-
1513
/**
1614
* Class Cookies
1715
* @package Inhere\Http
1816
*/
19-
class Cookies extends SimpleCollection
17+
class Cookies extends Collection
2018
{
2119
/**
2220
* Cookies
@@ -74,7 +72,7 @@ public function set($name, $value)
7472
public function toHeaders()
7573
{
7674
$headers = [];
77-
foreach ($this->data as $name => $properties) {
75+
foreach ($this as $name => $properties) {
7876
$headers[] = $this->toHeader($name, $properties);
7977
}
8078

@@ -133,7 +131,7 @@ public function toRequestHeader()
133131
{
134132
$cookieValue = '';
135133

136-
foreach ($this->data as $name => $value) {
134+
foreach ($this as $name => $value) {
137135
$cookieValue .= urlencode($name) . '=' . urlencode($value['value']) . '; ';
138136
}
139137

@@ -154,7 +152,7 @@ public static function parseFromRawHeader($cookieText)
154152
if (is_array($cookieText)) {
155153
$cookieText = array_shift($cookieText) ?: '';
156154
}
157-
155+
158156
if (!is_string($cookieText)) {
159157
throw new \InvalidArgumentException('Cannot parse Cookie data. Header value must be a string.');
160158
}

src/CookiesTrait.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/10/21
6+
* Time: 下午1:18
7+
*/
8+
9+
namespace Inhere\Http;
10+
11+
/**
12+
* Trait CookiesTrait
13+
* @package Inhere\Http
14+
*/
15+
trait CookiesTrait
16+
{
17+
/**
18+
* @var Cookies
19+
*/
20+
private $cookies;
21+
22+
/*******************************************************************************
23+
* Cookies
24+
******************************************************************************/
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function getCookieParams()
30+
{
31+
return $this->cookies->all();
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function getCookieParam($key, $default = null)
38+
{
39+
return $this->cookies->get($key, $default);
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function withCookieParams(array $cookies)
46+
{
47+
$clone = clone $this;
48+
$clone->cookies = new Cookies($cookies);
49+
50+
return $clone;
51+
}
52+
53+
/**
54+
* @param string $name
55+
* @param string|array $value
56+
* @return $this
57+
*/
58+
public function setCookie(string $name, $value)
59+
{
60+
$this->cookies->set($name, $value);
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* @return Cookies
67+
*/
68+
public function getCookies(): Cookies
69+
{
70+
return $this->cookies;
71+
}
72+
73+
/**
74+
* @param Cookies|array $cookies
75+
* @return $this
76+
*/
77+
public function setCookies($cookies)
78+
{
79+
if (is_array($cookies)) {
80+
return $this->setCookiesFromArray($cookies);
81+
}
82+
83+
$this->cookies = $cookies;
84+
85+
return $this;
86+
}
87+
88+
/**
89+
* @param array $cookies
90+
* @return $this
91+
*/
92+
public function setCookiesFromArray(array $cookies)
93+
{
94+
if (!$this->cookies) {
95+
$this->cookies = new Cookies($cookies);
96+
} else {
97+
$this->cookies->sets($cookies);
98+
}
99+
100+
return $this;
101+
}
102+
}

src/Extra/ExtendedRequestTrait.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
use Inhere\Http\UploadedFile;
1212
use Inhere\Http\Uri;
13-
use Inhere\Library\Helpers\PhpHelper;
1413
use Inhere\Validate\FilterList;
15-
use Inhere\Library\Types;
1614

1715
/**
1816
* trait ExtendedRequestTrait
@@ -53,11 +51,19 @@
5351
*/
5452
trait ExtendedRequestTrait
5553
{
56-
/**
57-
* return raw data
58-
*/
54+
/** @var string */
5955
private static $rawFilter = 'raw';
6056

57+
/** @var array */
58+
private static $phpTypes = [
59+
'int', 'integer',
60+
'float', 'double',
61+
'bool', 'boolean',
62+
'string',
63+
64+
'array', 'object', 'resource'
65+
];
66+
6167
/**
6268
* @var array
6369
*/
@@ -236,7 +242,7 @@ public function filtering($value, $filter = null)
236242

237243
// is custom callable filter
238244
if (is_callable($filter)) {
239-
$result = PhpHelper::call($filter, $value);
245+
$result = $filter($value);
240246
}
241247

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

248-
if (!in_array($filter, Types::all(), true)) {
249-
$result = PhpHelper::call($filter, $value);
254+
if (!in_array($filter, self::$phpTypes, true)) {
255+
$result = $filter($value);
250256
} else {
251257
switch (lcfirst(trim($filter))) {
252-
case Types::T_BOOL :
253-
case Types::T_BOOLEAN :
258+
case 'bool':
259+
case 'boolean':
254260
$result = (bool)$value;
255261
break;
256-
case Types::T_DOUBLE :
257-
case Types::T_FLOAT :
262+
case 'double':
263+
case 'float':
258264
$result = (float)$value;
259265
break;
260-
case Types::T_INT :
261-
case Types::T_INTEGER :
266+
case 'int' :
267+
case 'integer':
262268
$result = (int)$value;
263269
break;
264-
case Types::T_STRING :
270+
case 'string':
265271
$result = (string)$value;
266272
break;
267-
case Types::T_ARRAY :
273+
case 'array':
268274
$result = (array)$value;
269275
break;
270-
case Types::T_OBJECT :
276+
case 'object':
271277
$result = (object)$value;
272278
break;
273279
default:

src/Extra/ExtendedResponseTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function withRedirect($url, $status = null)
6767
*/
6868
public function withJson($data, $status = null, $encodingOptions = 0)
6969
{
70-
$response = $this->withBody(new Body(fopen('php://temp', 'rb+')));
70+
$response = $this->withBody(new Body());
7171
$response->body->write($json = json_encode($data, $encodingOptions));
7272

7373
// Ensure that the json encoding passed successfully

src/Headers.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
namespace Inhere\Http;
1010

11-
use Inhere\Library\Collections\SimpleCollection;
12-
1311
/**
1412
* Class Headers
1513
* @package Inhere\Http
1614
*/
17-
class Headers extends SimpleCollection
15+
class Headers extends Collection
1816
{
1917
/**
2018
* the connection header line data end char
@@ -195,7 +193,7 @@ public function toHeaderLines($toString = false)
195193
{
196194
$output = [];
197195

198-
foreach ($this->data as $name => $info) {
196+
foreach ($this as $name => $info) {
199197
$name = ucwords($name, '-');
200198
$value = implode(',', $info['value']);
201199
$output[] = "$name: $value\r\n";
@@ -211,7 +209,7 @@ public function getLines()
211209
{
212210
$output = [];
213211

214-
foreach ($this->data as $name => $info) {
212+
foreach ($this as $name => $info) {
215213
$name = ucwords($name, '-');
216214
$output[$name] = implode(',', $info['value']);
217215
}

0 commit comments

Comments
 (0)