1: <?php
2:
3: namespace Liberty;
4:
5:
6: /**
7: * Collection class.
8: *
9: * @category Cryptocurrency
10: * @package Liberty
11: * @license https://en.wikipedia.org/wiki/Public_domain Public Domain.
12: * @version 1.0.0
13: * @since 2018-02-17
14: * @author Alireza Rahmani Khalili @alirezarahmani, Liberty Group <cryptolibertygroup@gmail.com>
15: */
16:
17:
18:
19: class Collection {
20:
21: /**
22: * The List itself
23: * @var string
24: */
25: protected $items = array();
26:
27:
28:
29: public function __construct($list=array())
30: {
31: if(isset($list)) {
32: $this->items = $list;
33: }
34: }
35:
36:
37:
38: public function add($obj, $key = null) {
39: if ($key == null) {
40: $this->items[] = $obj;
41: }
42: else {
43: if (isset($this->items[$key])) {
44: throw new KeyHasUseException("Key $key already in use.");
45: }
46: else {
47: $this->items[$key] = $obj;
48: }
49: }
50: }
51:
52:
53:
54: public function remove($key) {
55: if (isset($this->items[$key])) {
56: unset($this->items[$key]);
57: }
58: else {
59: throw new KeyInvalidException("Invalid key $key.");
60: }
61: }
62:
63:
64:
65: public function item($key){
66: if (isset($this->items[$key])) {
67: return $this->items[$key];
68: }
69: else {
70: throw new KeyInvalidException("Invalid key $key.");
71: }
72: }
73:
74:
75: public function items() {
76: return $this->items;
77: }
78:
79:
80: public function keys() {
81: return array_keys($this->items);
82: }
83:
84:
85:
86: public function length() {
87: return count($this->items);
88: }
89:
90:
91: }
92:
93:
94: ?>
95: