Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 9b4e4a6

Browse files
committed
update: add a object stroage tool
1 parent 1004cb9 commit 9b4e4a6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/ObjectStorage.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018-01-30
6+
* Time: 14:56
7+
*/
8+
9+
namespace MyLib\ObjUtil;
10+
11+
/**
12+
* Class ObjectStorage
13+
* - 允许使用非对象作为key,会自动使用 \stdClass 转成对象
14+
* @package MyLib\ObjUtil
15+
*/
16+
class ObjectStorage extends \SplObjectStorage
17+
{
18+
/**
19+
* @param mixed $key
20+
* @param null $data
21+
*/
22+
public function attach($key, $data = null)
23+
{
24+
if (!\is_object($key)) {
25+
$raw = $key;
26+
$key = new \stdClass();
27+
$key->value = $raw;
28+
}
29+
30+
parent::attach($key, $data);
31+
}
32+
33+
/**
34+
* @param mixed $key
35+
*/
36+
public function detach($key)
37+
{
38+
if (!\is_object($key)) {
39+
$raw = $key;
40+
$key = new \stdClass();
41+
$key->value = $raw;
42+
}
43+
44+
parent::detach($key);
45+
}
46+
47+
/**
48+
* @param mixed $key
49+
* @return bool
50+
*/
51+
public function contains($key)
52+
{
53+
if (!\is_object($key)) {
54+
$raw = $key;
55+
$key = new \stdClass();
56+
$key->value = $raw;
57+
}
58+
59+
return parent::contains($key);
60+
}
61+
62+
/**
63+
* @param mixed $key
64+
* @return bool
65+
*/
66+
public function has($key): bool
67+
{
68+
return $this->contains($key);
69+
}
70+
}

0 commit comments

Comments
 (0)