|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PEIP\Base; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the PEIP package. |
|
7
|
|
|
* (c) 2009-2016 Timo Michna <timomichna/yahoo.de> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Sealer |
|
15
|
|
|
* Class to act as a implementation of the Selaer/Usealer pattern. |
|
16
|
|
|
* Used to give a reference to an arbitrary value without referencing the value itself. |
|
17
|
|
|
* By sealing an value the sealer will return a 'box'-object, which can later be used |
|
18
|
|
|
* to receive the sealed value. By passing an arbitrary object as second argument to the |
|
19
|
|
|
* 'seal' method, any object can act as the 'box' for the sealed value. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
|
22
|
|
|
* @implements \PEIP\INF\Base\Sealer, \PEIP\INF\Base\Unsealer |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
// PHP5.3 |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class Sealer implements \PEIP\INF\Base\Sealer, \PEIP\INF\Base\Unsealer |
|
29
|
|
|
{ |
|
30
|
|
|
protected $store; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param ObjectStorage $store an instane of ObjectStorage to act as the internal object-store |
|
36
|
|
|
* |
|
37
|
|
|
* @return |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(\SplObjectStorage $store = null) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->store = (bool) $store ? $store : new ObjectStorage(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Seals a given value and returns a 'box' object as reference. |
|
46
|
|
|
* If method is called with an object instance as second argument, |
|
47
|
|
|
* the given object is used as the box. Otherwise a simple stdClass object |
|
48
|
|
|
* will be created as the 'box'. |
|
49
|
|
|
* |
|
50
|
|
|
* @param mixed $value the value to seal. |
|
51
|
|
|
* @param object $box an object to act as the 'box' |
|
52
|
|
|
* |
|
53
|
|
|
* @return object the 'box' for the sealed value |
|
54
|
|
|
*/ |
|
55
|
|
|
public function seal($value, $box = false) |
|
56
|
|
|
{ |
|
57
|
|
|
$box = (bool) $box ? $box : new \stdClass(); |
|
58
|
|
|
$this->store[$box] = $value; |
|
59
|
|
|
|
|
60
|
|
|
return $box; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Unseals and returns a value refernced by the given box-object. |
|
65
|
|
|
* |
|
66
|
|
|
* @param object $box the box-object to return the value for. |
|
67
|
|
|
* |
|
68
|
|
|
* @return |
|
69
|
|
|
*/ |
|
70
|
|
|
public function unseal($box) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->store[$box]; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|