-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseCollection.php
executable file
·131 lines (102 loc) · 2.69 KB
/
useCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env php
<?php
/**
* Summary of the experiment of this example:
*
* Starting with the following collection:
*
* ['foo', 'bar', 'baz, 'quux']
*
* Perform additions, updates and deletes (including testing locking and overwriting) and end up with:
*
* ['quux', 'xuuq', 'baz, 'zab', 'foo']
*
* This will be done in a purposefully convoluted way to stress test the component.
* Unit testing can be found in the test/unit folder
*
*/
declare(strict_types = 1);
chdir(__DIR__);
require '../vendor/autoload.php';
use GalvaoEti\Collection\Collection;
try {
$c = new Collection('string');
} catch (Throwable $t) {
echo $t->getMessage();
exit(1);
}
$c->allowOverwriting = true;
try {
$c->add('foo');
$c->add('bar');
$c->add('baz');
$c->add('quux');
} catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Initial collection state: ' . PHP_EOL;
var_dump($c->get(null));
echo 'Adding incorrect elements on purpose: ' . PHP_EOL;
try {
$c->add('qqq');
echo 'Added qqq' . PHP_EOL;
$c->add('xxx');
echo 'Added xxx' . PHP_EOL;
$c->add('zzz');
echo 'Added zzz' . PHP_EOL;
} catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Making the collection non-overwritable and trying to update the wrong elements...' . PHP_EOL;
$c->allowOverwriting = false;
try {
$c->update(4, 'xuuq');
} catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Making the collection overwritable and trying again.' . PHP_EOL;
$c->allowOverwriting = true;
try {
$prev = $c->get(4);
$c->update(4, 'xuuq');
echo 'Updated ' . $prev . ' to xuuq' . PHP_EOL;
} catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
echo "Delete the item in index 5 ('xxx') and rearranging the keys" . PHP_EOL;
try {
$c->delete(5, true);
echo 'Deleted xxx' . PHP_EOL;
} catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
echo "Locking the collection and trying to delete item which is now on index 5 ('zzz')" . PHP_EOL;
$c->locked = true;
try {
$c->delete(5);
} catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Unlocking and trying again' . PHP_EOL;
$c->locked = false;
try {
$c->delete(5);
echo 'Deleted zzz' . PHP_EOL;
} catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
// Adding the final items
try {
$c->add('zab');
echo 'Added zab' . PHP_EOL;
$c->add('rab');
echo 'Added rab' . PHP_EOL;
$c->add('oof', false);
echo 'Added oof' . PHP_EOL;
} catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
// Using the collection's generator to display all it's items
foreach ($c->generateData() as $index => $item) {
echo $index . ' => ' . $item . PHP_EOL;
}