grikdotnet

entity sample

Apr 3rd, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.59 KB | None | 0 0
  1. <?php
  2.  
  3. namespace GK\Entities;
  4.  
  5. use GK\Collections\SizeCollection;
  6. use GK\Container;
  7. use Interop\Container\ContainerInterface;
  8.  
  9. /**
  10.  * Class Product
  11.  * @package GK\Entities
  12.  * @property int $id
  13.  * @property string $name
  14.  * @property string $url
  15.  * @property string $brand
  16.  * @property string $description
  17.  * @property string $price_usd
  18.  * @property string $picture1
  19.  * @property string $picture2
  20.  * @property string $picture3
  21.  * @property string $picture4
  22.  * @property string $picture5
  23.  */
  24. class Product
  25. {
  26.     //db fields mapping
  27.     protected $attributes = [
  28.         'id'        =>0,
  29.         'name'      =>'',
  30.         'url'       =>'',
  31.         'brand'     =>'',
  32.         'description'=>'',
  33.         'price_usd' =>0,
  34.         'size_eur'  =>0,
  35.         'size_us'   =>0,
  36.         'size_ru'   =>0,
  37.         'picture1'  =>'',
  38.         'picture2'  =>'',
  39.         'picture3'  =>'',
  40.         'picture4'  =>'',
  41.         'picture5'  =>'',
  42.     ];
  43.  
  44.     /**
  45.      * @var float
  46.      */
  47.     protected $dollar_rate;
  48.  
  49.     /**
  50.      * @var SizeCollection
  51.      */
  52.     protected $sizes;
  53.  
  54.     /**
  55.      * @var Container
  56.      */
  57.     private $container;
  58.    
  59.     public function setSizes(SizeCollection $sizes) {
  60.         $this->sizes = $sizes;
  61.     }
  62.  
  63.     /**
  64.      * @return Container
  65.      */
  66.     protected function getContainer() {
  67.         if ($this->container === NULL){
  68.             $this->container = Container::getInst();
  69.         }
  70.         return $this->container;
  71.     }
  72.  
  73.     /**
  74.      * @param Container|ContainerInterface $container
  75.      */
  76.     public function setContainer(ContainerInterface $container) {
  77.         $this->container = $container;
  78.     }
  79.  
  80.     /**
  81.      * Counts the price in UAH rounded to tens
  82.      * @return mixed
  83.      */
  84.     public function getPrice() {
  85.         return ceil( $this->getDollarRate() * $this->attributes['price_usd'] / 10 ) * 10;
  86.     }
  87.    
  88.     /**
  89.      * Loads data into attributes with a DB query
  90.      * @param $id
  91.      * @return bool
  92.      */
  93.     public function loadById($id) {
  94.         if  (!$id || !is_numeric($id)) {
  95.             return false;
  96.         }
  97.         $Select = Container::getInst()->qbf->newSelect()
  98.             ->cols([
  99.                 'id','brand','name','size_eur','size_us','size_ru',
  100.                 'picture1','picture2','picture3','picture4','picture5',
  101.             ])
  102.             ->from('products p')
  103.             ->where('p.id=?',$id)
  104.             ;
  105.         $res = Container::getInst()->db->perform(
  106.             $Select->getStatement(),
  107.             $Select->getBindValues()
  108.         )->fetchAll();
  109.         if ($res) {
  110.             $this->attributes = $res;
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * @param array $attributes
  116.      */
  117.     public function setAttributes(array $attributes) {
  118.         if (array_diff_key($attributes,$this->attributes) == []) {
  119.             $this->attributes = $attributes;
  120.             return;
  121.         }
  122.         foreach ($this->attributes as $k=>$v) {
  123.             if (isset($attributes[$k])){
  124.                 $this->attributes[$k] = $attributes[$k];
  125.             }
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * @param float $rate
  131.      */
  132.     public function setDollarRate($rate) {
  133.         $this->dollar_rate = $rate;
  134.     }
  135.  
  136.     /**
  137.      * @return float
  138.      */
  139.     public function getDollarRate() {
  140.         if ($this->dollar_rate === NULL) {
  141.             $this->dollar_rate = $this->container->settings['dollar_rate'];
  142.         }
  143.         return $this->dollar_rate;
  144.     }
  145.  
  146.     /**
  147.      * @param $key
  148.      * @return mixed
  149.      */
  150.     public function __get($key) {
  151.         return $this->attributes[$key];
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment