-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
88 lines (61 loc) · 1.86 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
<?php
require('classes/PaginatedAPI.php');
require('classes/UnfulfilledOrders.php');
require('classes/Orders.php');
/**
* Converts the json file data into array
* @param int $page
* @param object $api
* @return array containing data
*/
function get_url($page, $api){
$orders_url = $api->orders_url($page);
$url = $api->get_data($orders_url);
return $url;
}
//gets data from json page 1
$api = new PaginatedAPI;
$page = 1;
$url = get_url($page, $api);
$unfulfilled_list = new UnfulfilledOrders;
$res = array();
$available = $url->available_cookies;
//gets data from other json pages, stops looping when page is empty
while (!empty($url->orders)){
foreach($url->orders as $orders) {
$new_orders = new Orders($orders->id);
foreach($orders->products as $products){
$new_orders->addProducts($products->title, $products->amount);
}
if($new_orders->isCookie() != 0){
$unfulfilled_list->addOrders($new_orders);
}
}
// gets data from json pages
$page++;
$url = get_url($page, $api);
}
$size = count($unfulfilled_list->unfulfilled_orders);
// sort by object->amount
$unfulfilled_list->sortArray();
// removes orders from unfulfilled_orders if enough cookies are availble. Otherwise, adds the id of the order to the results array
for($i = 0; $i < $size; $i++){
if($unfulfilled_list->unfulfilled_orders[$i]->lessThanAvailable($available) != 0) {
unset($unfulfilled_list->unfulfilled_orders[$i]);
} else {
array_push($res, $unfulfilled_list->unfulfilled_orders[$i]->getID());
}
}
// create array that will be encoded into json
$output = array(
"remaining_cookies: " => $available,
"unfulfilled_orders: " => implode(", ", $res)
);
// encode $output in json and prints it.
$json_final = json_encode($output, JSON_PRETTY_PRINT);
printf("<pre>%s</pre>", $json_final);
/*
* correct answer:
* available: 0
* unfulfilled: 8, 10, 5, 7, 11
*/