-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
121 lines (106 loc) · 2.49 KB
/
index.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
<?php
/**
* Factory for robot
*/
class FactoryRobot
{
public $type;
public function addType($type) {
$this->type = $type;
}
public function createRobot1($count) {
$result = array();
for ($i = 0; $i < $count; $i++) {
$result[$i] = new Robot1();
}
return $result;
}
public function createRobot2($count) {
$result = array();
for ($i = 0; $i < $count; $i++) {
$result[$i] = new Robot2();
}
return $result;
}
public function createMergeRobot(int $count) {
$result = array();
for ($i = 0; $i < $count; $i++) {
$result[$i] = $this->type;
}
return $result;
}
}
/**
* Simple robot
*/
class Robot1
{
public $weight = 160;
public $speed = 180;
public $height = 200;
}
/**
* Simple robot
*/
class Robot2
{
public $weight = 100;
public $speed = 120;
public $height = 140;
}
/**
* Merged robots
*/
class MergeRobot
{
public $robots;
public function addRobot($addedRobots) {
$robots = !is_array($addedRobots) ? array($addedRobots) : $addedRobots;
foreach ($robots as $robot) {
$this->robots[] = $robot;
}
}
public function getSpeed() {
return max(array_map(function($item) {
return $item->speed;
},
(array) $this->robots));
}
public function getWeight() {
return array_sum(array_map(function($item) {
return $item->weight;
},
(array) $this->robots));
}
public function getHeight() {
return array_sum(array_map(function($item) {
return $item->height;
},
(array) $this->robots));
}
}
$factory = new FactoryRobot();
$factory->addType(new Robot1());
$factory->addType(new Robot2());
echo '<pre>';
var_dump($factory->createRobot1(5));
echo '</pre>';
echo '<pre>';
var_dump($factory->createRobot2(2));
echo '</pre>';
$mergeRobot = new MergeRobot();
$mergeRobot->addRobot(new Robot2());
$mergeRobot->addRobot($factory->createRobot2(2));
$factory->addType($mergeRobot);
$res = $factory->createMergeRobot(1);
$result = reset($res);
echo '<pre>';
var_dump($result->getSpeed());
echo '</pre>';
echo '<pre>';
var_dump($result->getWeight());
echo '</pre>';
echo '<pre>';
var_dump($result->getHeight());
echo '</pre>';
die;