Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace GK\Entities;
- use GK\Collections\SizeCollection;
- use GK\Container;
- use Interop\Container\ContainerInterface;
- /**
- * Class Product
- * @package GK\Entities
- * @property int $id
- * @property string $name
- * @property string $url
- * @property string $brand
- * @property string $description
- * @property string $price_usd
- * @property string $picture1
- * @property string $picture2
- * @property string $picture3
- * @property string $picture4
- * @property string $picture5
- */
- class Product
- {
- //db fields mapping
- protected $attributes = [
- 'id' =>0,
- 'name' =>'',
- 'url' =>'',
- 'brand' =>'',
- 'description'=>'',
- 'price_usd' =>0,
- 'size_eur' =>0,
- 'size_us' =>0,
- 'size_ru' =>0,
- 'picture1' =>'',
- 'picture2' =>'',
- 'picture3' =>'',
- 'picture4' =>'',
- 'picture5' =>'',
- ];
- /**
- * @var float
- */
- protected $dollar_rate;
- /**
- * @var SizeCollection
- */
- protected $sizes;
- /**
- * @var Container
- */
- private $container;
- public function setSizes(SizeCollection $sizes) {
- $this->sizes = $sizes;
- }
- /**
- * @return Container
- */
- protected function getContainer() {
- if ($this->container === NULL){
- $this->container = Container::getInst();
- }
- return $this->container;
- }
- /**
- * @param Container|ContainerInterface $container
- */
- public function setContainer(ContainerInterface $container) {
- $this->container = $container;
- }
- /**
- * Counts the price in UAH rounded to tens
- * @return mixed
- */
- public function getPrice() {
- return ceil( $this->getDollarRate() * $this->attributes['price_usd'] / 10 ) * 10;
- }
- /**
- * Loads data into attributes with a DB query
- * @param $id
- * @return bool
- */
- public function loadById($id) {
- if (!$id || !is_numeric($id)) {
- return false;
- }
- $Select = Container::getInst()->qbf->newSelect()
- ->cols([
- 'id','brand','name','size_eur','size_us','size_ru',
- 'picture1','picture2','picture3','picture4','picture5',
- ])
- ->from('products p')
- ->where('p.id=?',$id)
- ;
- $res = Container::getInst()->db->perform(
- $Select->getStatement(),
- $Select->getBindValues()
- )->fetchAll();
- if ($res) {
- $this->attributes = $res;
- }
- }
- /**
- * @param array $attributes
- */
- public function setAttributes(array $attributes) {
- if (array_diff_key($attributes,$this->attributes) == []) {
- $this->attributes = $attributes;
- return;
- }
- foreach ($this->attributes as $k=>$v) {
- if (isset($attributes[$k])){
- $this->attributes[$k] = $attributes[$k];
- }
- }
- }
- /**
- * @param float $rate
- */
- public function setDollarRate($rate) {
- $this->dollar_rate = $rate;
- }
- /**
- * @return float
- */
- public function getDollarRate() {
- if ($this->dollar_rate === NULL) {
- $this->dollar_rate = $this->container->settings['dollar_rate'];
- }
- return $this->dollar_rate;
- }
- /**
- * @param $key
- * @return mixed
- */
- public function __get($key) {
- return $this->attributes[$key];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment