| 1 | <?php |
||
| 3 | class ProductModel extends SampleAppBaseModel |
||
| 4 | { |
||
| 5 | protected $id; |
||
| 6 | protected $name; |
||
| 7 | protected $price; |
||
| 8 | |||
| 9 | protected static $fields = array( |
||
| 10 | 'id', |
||
| 11 | 'name', |
||
| 12 | 'price' |
||
| 13 | ); |
||
| 14 | |||
| 15 | public function __construct(array $data = array()) |
||
| 16 | { |
||
| 17 | $this->fromArray($data); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function fromArray(array $data) |
||
| 21 | { |
||
| 22 | foreach(self::$fields as $field) { |
||
| 23 | if(isset($data[$field])) { |
||
| 24 | $this->$field = $data[$field]; |
||
| 25 | } |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | public function toArray() |
||
| 30 | { |
||
| 31 | $retval = array(); |
||
| 32 | |||
| 33 | foreach(self::$fields as $field) { |
||
| 34 | $retval[$field] = $this->$field; |
||
| 35 | } |
||
| 36 | |||
| 37 | return $retval; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function getId() |
||
| 41 | { |
||
| 42 | return $this->id; |
||
| 43 | } |
||
| 44 | |||
| 45 | public function getName() |
||
| 46 | { |
||
| 47 | return $this->name; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function getPrice() |
||
| 51 | { |
||
| 52 | return $this->price; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function setId($id) |
||
| 56 | { |
||
| 57 | $this->id = $id; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function setName($name) |
||
| 61 | { |
||
| 62 | $this->name = $name; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function setPrice($price) |
||
| 66 | { |
||
| 67 | $this->price = $price; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function isAvailable() |
||
| 71 | { |
||
| 72 | // imagine this makes a very complicated SOAP call to an ERP system to figure out whether or not this product is in stock |
||
| 73 | return (bool)mt_rand(0, 1); |
||
| 74 | } |
||
| 75 | } |
||
| 76 |