This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.php
151 lines (105 loc) · 3.96 KB
/
generate.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
142
143
144
145
146
147
148
149
<?php
namespace Potherca\Takeaway\HomeworkAssignment;
set_error_handler(function($severity, $message, $file, $line) {
throw new \ErrorException($message, 0, $severity, $file, $line);
});
function getRestaurantsFromFile($filePath)
{
$fileContents = file_get_contents($filePath);
$data = json_decode($fileContents, true);
$restaurants = array_shift($data);
return $restaurants;
}
function calculateTopRestaurants($distance, $popularity, $ratingAverage)
{
$topScore = ($distance * $popularity) + $ratingAverage;
$topScore = round($topScore);
return $topScore;
}
function addTopRestaurants($restaurants)
{
return array_map(function ($restaurant) {
$values = $restaurant['sortingValues'];
$topScore = calculateTopRestaurants(
$values['distance'],
$values['popularity'],
$values['ratingAverage']
);
$restaurant['sortingValues']['topRestaurants'] = $topScore;
return $restaurant;
}, $restaurants);
}
function sortByKey($restaurants, $sortKey, $descendingValues)
{
$sortOrder = [
'open' => 0,
'order ahead' => 1,
'closed' => 2,
];
usort($restaurants, function ($left, $right) use ($descendingValues, $sortKey, $sortOrder) {
if ($left['status'] === $right['status']) {
if (in_array($sortKey, $descendingValues)) {
$sortValue = $left['sortingValues'][$sortKey] - $right['sortingValues'][$sortKey];
} else {
$sortValue = $right['sortingValues'][$sortKey] - $left['sortingValues'][$sortKey];
}
} else {
$sortValue = $sortOrder[$left['status']] - $sortOrder[$right['status']];
}
return $sortValue;
});
return $restaurants;
}
function createRestaurantCollection($restaurants, $sortKeys, $descendingValues)
{
$sortedRestaurants = [];
array_walk($sortKeys, function ($sortKey) use (&$sortedRestaurants, $descendingValues, $restaurants) {
$sortedRestaurants[$sortKey] = sortByKey($restaurants, $sortKey, $descendingValues);
});
return $sortedRestaurants;
}
function putRestaurantsToFile($restaurants, $apiDirectory)
{
array_walk($restaurants, function ($restaurants, $sortKeys) use ($apiDirectory) {
$filePath = sprintf("%s/%s.json", $apiDirectory, $sortKeys);
$data = json_encode(['restaurants' => $restaurants], JSON_PRETTY_PRINT);
file_put_contents($filePath, $data);
});
}
function getSortKeys($restaurants)
{
$restaurant = array_shift($restaurants);
$sortKeys = array_keys($restaurant['sortingValues']);
return $sortKeys;
}
function run($filePath, $apiDirectory, $descendingValues)
{
$restaurants = getRestaurantsFromFile($filePath);
$restaurants = addTopRestaurants($restaurants);
$sortKeys = getSortKeys($restaurants);
$restaurantsCollection = createRestaurantCollection($restaurants, $sortKeys, $descendingValues);
putRestaurantsToFile($restaurantsCollection, $apiDirectory);
}
$exitCode = 0;
if (in_array('--help', $argv)) {
$script = basename($argv[0]);
echo <<<TXT
== Takeaway Homework Assignment JSON files generator ==
Generates a JSON files containing restaurants for each sort-option.
Each file contains the sort order for a different sort option.
The restaurants and sort options are based on the data from the given sample JSON.
Usage: ${script} <path-to-sample-json> <api-directory>
Where:
- <path-to-sample-json> is the path to the sample.json file containing the sample of restaurants.
- <api-directory> is the path to the directory the JSON files are to written.
The <api-directory> MUST already exist.
TXT;
}elseif (isset($argv[1], $argv[2]) === false) {
echo 'ERROR ! Two parameter required: <path-to-sample-json> <api-directory>'.PHP_EOL;
$exitCode = 65;
} else {
$descendingValues = ['averageProductPrice', 'deliveryCosts', 'distance', 'minCost'];
run($argv[1], $argv[2], $descendingValues);
}
exit($exitCode);
#EOF