|
1
|
|
|
<?php |
|
2
|
|
|
use Agavi\Model\SingletonModelInterface; |
|
3
|
|
|
|
|
4
|
|
|
class ProductFinderModel extends SampleAppBaseModel implements SingletonModelInterface |
|
5
|
|
|
{ |
|
6
|
|
|
// imagine this stuff is in a database :) |
|
7
|
|
|
protected static $products = array( |
|
8
|
|
|
array( |
|
9
|
|
|
'id' => 8172401, |
|
10
|
|
|
'name' => 'TPS Report Cover Sheet', |
|
11
|
|
|
'price' => 0.89, |
|
12
|
|
|
), |
|
13
|
|
|
array( |
|
14
|
|
|
'id' => 917246, |
|
15
|
|
|
'name' => 'Weighted Companion Cube', |
|
16
|
|
|
'price' => 129.99, |
|
17
|
|
|
), |
|
18
|
|
|
array( |
|
19
|
|
|
'id' => 7856122, |
|
20
|
|
|
'name' => 'Longcat', |
|
21
|
|
|
'price' => 14599, |
|
22
|
|
|
), |
|
23
|
|
|
array( |
|
24
|
|
|
'id' => 123456, |
|
25
|
|
|
'name' => 'Red Stapler', |
|
26
|
|
|
'price' => 3.14, |
|
27
|
|
|
), |
|
28
|
|
|
array( |
|
29
|
|
|
'id' => 3165463, |
|
30
|
|
|
'name' => 'Sildenafil Citrate', |
|
31
|
|
|
'price' => 14.69, |
|
32
|
|
|
), |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
public function retrieveAll() |
|
36
|
|
|
{ |
|
37
|
|
|
$retval = array(); |
|
38
|
|
|
|
|
39
|
|
|
foreach(self::$products as $product) { |
|
40
|
|
|
$retval[] = $this->context->getModel('Product', null, array($product)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $retval; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function retrieveRandom() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->context->getModel('Product', null, array(self::$products[array_rand(self::$products)])); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function retrieveByName($productName) |
|
52
|
|
|
{ |
|
53
|
|
|
foreach(self::$products as $product) { |
|
54
|
|
|
if($product['name'] == $productName) { |
|
55
|
|
|
return $this->context->getModel('Product', null, array($product)); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function retrieveById($productId) |
|
61
|
|
|
{ |
|
62
|
|
|
foreach(self::$products as $product) { |
|
63
|
|
|
if($product['id'] == $productId) { |
|
64
|
|
|
return $this->context->getModel('Product', null, array($product)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function retrieveByIdAndName($productId, $productName) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach(self::$products as $product) { |
|
72
|
|
|
if($product['id'] == $productId && $product['name'] == $productName) { |
|
73
|
|
|
return $this->context->getModel('Product', null, array($product)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|