class Card
{
public $item=array();
public function __construct($move)
{
$this->method = $move;
}
public function destributor($data)
{
$this->{$this->method[$data['move']]}($data);
}
public function put($data)
{
$this->item[$data['item']]['name'] = $data['item'];
$this->item[$data['item']]['count'] = $data['count'];
$this->item[$data['item']]['price'] = $data['price'];
}
public function add($data)
{
$this->item[$data['item']]['count'] = $this->item[$data['item']]['count'] + $data['count'];
}
public function ded($data)
{
$this->item[$data['item']]['count'] = $this->item[$data['item']]['count'] - $data['count'];
}
public function del()
{
unset($this->item);
}
public function run()
{
return !empty($this->item) ? $this->item : ' корзина итак пуста, что там удалять еще, если денег нет то их нет! ';
}
}
class Discount
{
public $total;
public function __construct($discount, $item, $function)
{
foreach( $discount as $key=>$val )
{
$function = empty($function) ? $this->convert($val['discount'], $val['edge']) : $function;
$item[$val['item']]['price'] = $function($item[$val['item']]['price'], $item[$val['item']]['count']);
$this->total += $item[$val['item']]['price'] * $item[$val['item']]['count'];
}
$this->item = $item;
}
public function convert($discount, $edge)
{
return function ($price, $count) use ($discount, $edge)
{
return ( $price*$count > $edge ) ? $price = $price - ( $price*($discount/100) ) : $price ;
};
}
public function run()
{
return !empty($this->item) ? $this->item : ' корзина итак пуста, что там удалять еще, если денег нет то их нет! ';
}
}
$move = array( 1=>'put', 2=>'add', 3=>'ded', 4=>'del' );
$today = 'today';
$birthday = 'yestoday';
$edge = 300;
$discount1 = 25;
if( $today == $birthday )
{
$function = function ($price, $count) use ($discount1, $edge) {
return ( $price*$count > $edge ) ? $price = $price - ( $price*($discount1/100) ) : $price ;
};
}
else
$function = '';
$discount[1] = array( 'item'=>'Кепка', 'discount'=>15, 'edge'=>2399 );
$discount[2] = array( 'item'=>'Носки', 'discount'=>10, 'edge'=>1599 );
$data[1] = array( 'move'=>1, 'item'=>'Носки', 'count'=>4, 'price'=>800 );
$data[2] = array( 'move'=>2, 'item'=>'Носки', 'count'=>1, 'price'=>800 );
$data[3] = array( 'move'=>1, 'item'=>'Кепка', 'count'=>2, 'price'=>1200 );
$data[4] = array( 'move'=>3, 'item'=>'Носки', 'count'=>2, 'price'=>800 );
$data[5] = array( 'move'=>3, 'item'=>'Кепка', 'count'=>1, 'price'=>1200 );
$obj = new card($move);
foreach( $data as $val )
$obj-> destributor($val);
var_dump($obj->run());
$disc = new Discount($discount, $obj->run(), $function);
var_dump( $disc->run() );
var_dump( $disc->total );