Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 2463d22

Browse files
committed
update some tool class
1 parent f98f2f8 commit 2463d22

File tree

12 files changed

+225
-35
lines changed

12 files changed

+225
-35
lines changed

src/Collections/CollectionInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,30 @@ public function set($key, $value);
1111

1212
public function get(string $key, $default = null);
1313

14+
/**
15+
* @param array $items
16+
*/
1417
public function replace(array $items);
1518

19+
/**
20+
* @return array
21+
*/
1622
public function all();
1723

24+
/**
25+
* @param string $key
26+
* @return bool
27+
*/
1828
public function has(string $key);
1929

30+
/**
31+
* @param $key
32+
* @return mixed
33+
*/
2034
public function remove($key);
2135

36+
/**
37+
* clear all data
38+
*/
2239
public function clear();
2340
}

src/Collections/LiteCollection.php

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

src/Collections/SimpleCollection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public static function make($items = null)
3434
*/
3535
public function __construct(array $items = [])
3636
{
37-
foreach ($items as $key => $value) {
38-
$this->set($key, $value);
39-
}
37+
$this->replace($items);
4038
}
4139

4240
public function __destruct()

src/Helpers/ObjectHelper.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,22 @@
1818
class ObjectHelper
1919
{
2020
/**
21-
* 给对象设置属性值
22-
* @param $object
23-
* @param array $options
24-
*/
25-
public static function setAttrs($object, array $options)
26-
{
27-
self::configure($object, $options);
28-
}
29-
30-
/**
31-
* 给对象设置属性值
32-
* @param $object
21+
* @param mixed $object An object instance
3322
* @param array $options
23+
* @return mixed
3424
*/
35-
public static function configure($object, array $options)
25+
public static function init($object, array $options)
3626
{
37-
foreach ($options as $property => $value) {
38-
$object->$property = $value;
39-
}
27+
return self::smartConfigure($object, $options);
4028
}
4129

4230
/**
4331
* 给对象设置属性值
4432
* - 会先尝试用 setter 方法设置属性
4533
* - 再尝试直接设置属性
46-
* @param $object
34+
* @param mixed $object An object instance
4735
* @param array $options
36+
* @return mixed
4837
*/
4938
public static function smartConfigure($object, array $options)
5039
{
@@ -55,12 +44,37 @@ public static function smartConfigure($object, array $options)
5544

5645
$setter = 'set' . ucfirst($property);
5746

47+
// has setter
5848
if (method_exists($object, $setter)) {
5949
$object->$setter($value);
6050
} else {
6151
$object->$property = $value;
6252
}
6353
}
54+
55+
return $object;
56+
}
57+
58+
/**
59+
* 给对象设置属性值
60+
* @param $object
61+
* @param array $options
62+
*/
63+
public static function setAttrs($object, array $options)
64+
{
65+
self::configure($object, $options);
66+
}
67+
68+
/**
69+
* 给对象设置属性值
70+
* @param $object
71+
* @param array $options
72+
*/
73+
public static function configure($object, array $options)
74+
{
75+
foreach ($options as $property => $value) {
76+
$object->$property = $value;
77+
}
6478
}
6579

6680
/**

src/Traits/LiteEventStaticTrait.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
namespace Inhere\Library\Traits;
10+
use Inhere\Library\Helpers\Php;
1011

1112
/**
1213
* Trait LiteEventStaticTrait - 简洁版的事件处理trait,一个事件只允许一个回调
@@ -47,11 +48,7 @@ public static function fire($name, array $args = [])
4748
return null;
4849
}
4950

50-
if (is_object($cb) || (is_string($cb) && function_exists($cb))) {
51-
return $cb(...$args);
52-
}
53-
54-
return call_user_func_array($cb, $args);
51+
return Php::call($cb, ...$args);
5552
}
5653

5754
/**

src/Traits/LiteEventTrait.php

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

99
namespace Inhere\Library\Traits;
1010

11+
use Inhere\Library\Helpers\Php;
12+
1113
/**
1214
* Trait LiteEventTrait - 简洁版的事件处理trait,一个事件只允许一个回调
1315
* @package Inhere\Library\Traits
@@ -47,11 +49,7 @@ protected function fire($name, array $args = [])
4749
return null;
4850
}
4951

50-
if (is_object($cb) || (is_string($cb) && function_exists($cb))) {
51-
return $cb(...$args);
52-
}
53-
54-
return call_user_func_array($cb, $args);
52+
return Php::call($cb, ...$args);
5553
}
5654

5755
/**

0 commit comments

Comments
 (0)