-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProducts.class.php
59 lines (40 loc) · 978 Bytes
/
Products.class.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
<?php
class Products{
private $products;
function __construct($jsonFile = null){
if ($jsonFile != null)
$this->loadProducts($jsonFile);
}
private function loadProducts($jsonFile){
$json = file_get_contents($jsonFile);
$infoObj = json_decode($json);
foreach ($infoObj as $prodInfo){
$this->products[] = new Product($prodInfo);
}
}
function getAll (){
return $this->products;
}
function getProductById($id){
foreach($this->products as $product){
if ($product->id == $id)
return $product;
}
return new Product();
}
}
class Product{
public $id,
$description,
$category,
$price;
function __construct(stdClass $prodInfo = null){
if ($prodInfo != "") {
//Product Init
$this->id = $prodInfo->id;
$this->description= $prodInfo->description;
$this->category= $prodInfo->category;
$this->price= $prodInfo->price;
}
}
}