Advertisement
Guest User

fake model

a guest
Mar 2nd, 2014
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2. namespace common\models;
  3.  
  4. class ProductModel // вообще тут должно быть 'extends \yii\db\ActiveRecord'
  5. {
  6.     public $mockProducts = array( // начиная с этой строки идет живой пример того, как НЕ надо писать реальную модель. Все написанное здесь написано исключительно ради примера.
  7.         array('id' => 1, 'title' => 'CodeIgniter', 'description' => 'Quite popular framework from previous decade',),
  8.         array('id' => 2, 'title' => 'Symfony', 'description' => 'A framework that promotes organizing everything in reusable bundles',),
  9.         array('id' => 3, 'title' => 'Yii', 'description' => 'Framework that attracts developers by it\'s simplicity',),
  10.     );
  11.     public function get($id)
  12.     {
  13.         $id = (int) $id;
  14.         if ($id < 1) {
  15.             throw new \InvalidArgumentException('Invalid ID provided');
  16.         }
  17.         foreach ($this->mockProducts as $product) {
  18.             if ($product['id'] === $id) {
  19.                 return $product;
  20.             }
  21.         }
  22.         return false;
  23.     }
  24.     public function productExists($id)
  25.     {
  26.         foreach ($this->mockProducts as $product) {
  27.             if ($product['id'] === $id) {
  28.                 return true;
  29.             }
  30.         }
  31.         return false;
  32.     }
  33.     public function getAll()
  34.     {
  35.          return $this->mockProducts;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement