1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nucleus - XMPP Library for PHP |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some rights reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Xmpp\Utils; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Observable collection class. |
21
|
|
|
* |
22
|
|
|
* > means output |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* $collection = new ObservableCollection(); |
26
|
|
|
* $collection->on('set', function($value, $key) { echo "New item[$key]: $value".PHP_EOL; }); |
27
|
|
|
* $collection->on('remove', function($value, $key) { echo "Item removed[$key]: $value".PHP_EOL; }); |
28
|
|
|
* $collection->on('empty', function() { echo "list empty".PHP_EOL }); |
29
|
|
|
* |
30
|
|
|
* $collection[] = "foo"; |
31
|
|
|
* > New item[0]: foo |
32
|
|
|
* $collection[] = "bar" |
33
|
|
|
* > New item[1]: bar |
34
|
|
|
* unset($collection[1]); |
35
|
|
|
* > Item removed[1]: bar |
36
|
|
|
* $collection["dead"] = "beef" |
37
|
|
|
* > New item[dead]: deadbeef |
38
|
|
|
* |
39
|
|
|
* echo count($collection).PHP_EOL; |
40
|
|
|
* > 2 |
41
|
|
|
* |
42
|
|
|
* unset($collection[0]); |
43
|
|
|
* > Item removed[0]: foo |
44
|
|
|
* unset($collection['dead']); |
45
|
|
|
* > Item removed[dead]: beed |
46
|
|
|
* > List empty |
47
|
|
|
* ``` |
48
|
|
|
* |
49
|
|
|
* @event empty() Collection was emptied |
50
|
|
|
* @event set($value, $key) Item was added to collection |
51
|
|
|
* @event remove($value, $key) Item was removed from collection |
52
|
|
|
* |
53
|
|
|
* @package Kadet\Xmpp\Utils |
54
|
|
|
*/ |
55
|
|
|
class ObservableCollection extends \ArrayObject |
56
|
|
|
{ |
57
|
|
|
use BetterEmitter; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sets the value at the specified index to newval |
61
|
|
|
* @link http://php.net/manual/en/arrayobject.offsetset.php |
62
|
|
|
* @param mixed $index <p> |
63
|
|
|
* The index being set. |
64
|
|
|
* </p> |
65
|
|
|
* @param mixed $value <p> |
66
|
|
|
* The new value for the <i>index</i>. |
67
|
|
|
* </p> |
68
|
|
|
* @return void |
69
|
|
|
* @since 5.0.0 |
70
|
|
|
*/ |
71
|
|
|
public function offsetSet($index, $value) |
72
|
|
|
{ |
73
|
|
|
parent::offsetSet($index, $value); |
74
|
|
|
$this->emit('set', [ $value, $index ]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Unsets the value at the specified index |
79
|
|
|
* @link http://php.net/manual/en/arrayobject.offsetunset.php |
80
|
|
|
* @param mixed $index <p> |
81
|
|
|
* The index being unset. |
82
|
|
|
* </p> |
83
|
|
|
* @return void |
84
|
|
|
* @since 5.0.0 |
85
|
|
|
*/ |
86
|
|
|
public function offsetUnset($index) |
87
|
|
|
{ |
88
|
|
|
$this->emit('remove', [ $this[$index], $index ]); |
89
|
|
|
|
90
|
|
|
parent::offsetUnset($index); |
91
|
|
|
|
92
|
|
|
if(!count($this)) { |
93
|
|
|
$this->emit('empty'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function __destruct() |
98
|
|
|
{ |
99
|
|
|
$this->emit('empty'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|