-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
74 lines (66 loc) · 1.98 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
<?php
class Travel
{
protected $allTravel;
public function __construct()
{
$this->allTravel = json_decode(file_get_contents('https://5f27781bf5d27e001612e057.mockapi.io/webprovise/travels'),true);
}
public function sumPrice(){
return array_reduce($this->allTravel, function($carry, $item){
if(!isset($carry[$item['companyId']])){
$carry[$item['companyId']] = $item['price'];
} else {
$carry[$item['companyId']] += $item['price'];
}
return $carry;
});
}
}
class Company
{
protected $allCompany;
public function __construct()
{
$this->allCompany = json_decode(file_get_contents('https://5f27781bf5d27e001612e057.mockapi.io/webprovise/companies'),true);
}
public function addTotalPrice($arrPrice)
{
foreach ($this->allCompany as $key => $company){
$this->allCompany[$key]['price'] = $arrPrice[$company['id']] ?? 0;
}
}
public function buildTree($parentId = 0) {
$branch = [];
foreach ($this->allCompany as $element) {
if ($element['parentId'] == $parentId) {
$children = $this->buildTree($element['id']);
if ($children) {
$element['price'] += array_sum(array_column($children, 'price', 'id'));
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
public function getAll()
{
return $this->allCompany;
}
}
class TestScript
{
public function execute()
{
$start = microtime(true);
$travel = new Travel();
$arraySumPrice = $travel->sumPrice();
$company = new Company();
$company->addTotalPrice($arraySumPrice);
echo "<pre>";
print_r($company->buildTree());
echo 'Total time: ' . (microtime(true) - $start);
}
}
(new TestScript())->execute();