-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayAccess.php
141 lines (117 loc) · 2.67 KB
/
ArrayAccess.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
132
133
134
135
136
137
138
139
140
141
<?php
/**
*=============================================================
* <ArrayAccess.php>
* Object Oriented Programming: ArrayAccess built-in interface in PHP
* Problem: Objects implementing ArrayAccess do not support the increment/decrement
* operators ++ and --, unlike array() and ArrayObject()
*
* @Author Muhammad Anwar Hussain<anwar_hussain.01@yahoo.com>
* Created on: 10th July 2019
* ============================================================
*/
trait FlightInfo
{
public function callFlight($flightName)
{
return new $flightName;
}
}
trait PassengerInfo
{
public function passengerInfo($terminalName)
{
echo "[$terminalName]: Passenger information for ".strtoupper(__CLASS__ ). "<br>";
}
}
class Terminal_1
{
use FlightInfo;
}
class Terminal_2
{
use FlightInfo;
}
class Terminal_3
{
use FlightInfo;
}
class Flight_A
{
use PassengerInfo;
}
class Flight_B
{
use PassengerInfo;
}
class Flight_C
{
use PassengerInfo;
}
class Flight_D
{
use PassengerInfo;
}
class Flight_E
{
use PassengerInfo;
}
class Container implements \ArrayAccess
{
protected $data = array();
public function offsetExists ($offset)
{
return isset($this->data[$offset]);
}
public function offsetSet ($offset, $value)
{
if (is_null($offset))
{
$this->data[] = $value;
}
else
{
$this->data[$offset] = $value;
}
}
public function &offsetGet ($offset)
{
if (!isset($this->data[$offset]))
{
throw new Exception(sprintf('Key "%s" is not defined.', $offset));
}
$this->data[$offset] = is_callable($this->data[$offset])? $this->data[$offset]($this): $this->data[$offset];
return $this->data[$offset];
}
public function offsetUnset ($offset)
{
unset($this->data[$offset]);
}
}
$c = new Container();
$c['terminals'] = ['Terminal_1', 'Terminal_2', 'Terminal_3'];
$c['flights'] = ['Flight_A', 'Flight_D', 'Flight_B', 'Flight_C', 'Flight_E'];
if(isset($c['flights'][1]))
{
unset($c['flights'][1]);
}
if(isset($c['flights'][2]))
{
unset($c['flights'][2]);
}
shuffle($c['terminals']);
shuffle($c['flights']);
for ($c['indx'] = 0; $c['indx'] < count($c['terminals']); $c['indx'] = $c['indx'] + 1)
{
$c['terminal'] = function($c){return new $c['terminals'][$c['indx']];};
$c['flight'] = $c['terminal']->callFlight($c['flights'][$c['indx']]);
$c['flight']->passengerInfo($c['terminals'][$c['indx']]);
}
echo '<br>';
for ($indx = 0; $indx < count($c['terminals']); $indx++)
{
$c['terminal'] = function($c) use ($indx){return new $c['terminals'][$indx];};
$c['flight'] = $c['terminal']->callFlight($c['flights'][$indx]);
$c['flight']->passengerInfo($c['terminals'][$indx]);
}
?>