-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU.php
69 lines (65 loc) · 1.58 KB
/
CPU.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
<?php
// CPU:
class Cpu {
public $name;
public $price;
public $socket;
public function __construct($name, $price, $socket) {
$this->name = $name;
$this->price = $price;
$this->socket = $socket;
}
public function getCpuInformation($limitPrice, $purpose) {
// get list of cpu & get closest price.
$cpuResult;
$typeCpu = Array (
'E5',
'E3',
'i7',
'i5',
'i3',
'G'
);
$start = 0;
if ($purpose === 'Game') {
$start = 2;
}
for($i = $start; $i < sizeof ( $typeCpu ); $i ++) {
$cpuResult = getCpu ( $typeCpu [$i], $limitPrice );
if (! empty ( $cpuResult ) || isset ( $cpuResult )) {
$name = $cpuResult ['Name'];
$price = $cpuResult ['Price'];
$socket = $cpuResult ['Socket'];
return new Cpu($name, $price, $socket);
break;
}
}
}
}
function getCpu($type, $limitPrice) {
$rows = array_map ( 'str_getcsv', file ( 'data/computer/CPU.csv' ) );
$header = array_shift ( $rows );
$csv = array ();
$data = array ();
$cpuData = array ();
foreach ( $rows as $row ) {
$csv [] = array_combine ( $header, $row );
}
foreach ( $csv as $cpuData ) {
$value = floatval ( $cpuData ['Price'] );
$socketData = substr ( $cpuData ['Socket'], 0, 4 );
if ($value <= $limitPrice && $value > 0 && $cpuData ['Type'] === $type && $socketData === '2011') {
array_push ( $data, $cpuData );
} else if ($value <= $limitPrice && $value > 0 && $cpuData ['Type'] === $type) {
array_push ( $data, $cpuData );
}
}
if (empty($data)) {
return null;
} else {
$sort = new sort();
$sort->sortArray ( $data, 'Price' );
return $data[0];
}
}
?>