84 lines
1.7 KiB
PHP
84 lines
1.7 KiB
PHP
<?php
|
|
namespace shop;
|
|
class Coupon implements \ArrayAccess
|
|
{
|
|
private $data = [];
|
|
|
|
public function __construct( int $element_id )
|
|
{
|
|
global $mdb;
|
|
if ( $element_id )
|
|
{
|
|
$result = $mdb -> get( 'pp_shop_coupon', '*', [ 'id' => $element_id ] );
|
|
if ( is_array( $result ) )
|
|
$this -> data = $result;
|
|
}
|
|
}
|
|
|
|
public function load_from_db_by_name( string $name )
|
|
{
|
|
global $mdb;
|
|
$result = $mdb -> get( 'pp_shop_coupon', '*', [ 'name' => $name ] );
|
|
if ( is_array( $result ) )
|
|
$this -> data = $result;
|
|
}
|
|
|
|
public function is_one_time()
|
|
{
|
|
return (bool)( $this -> data['one_time'] ?? 0 );
|
|
}
|
|
|
|
public function is_available()
|
|
{
|
|
if ( !( $this -> data['id'] ?? 0 ) )
|
|
return false;
|
|
|
|
if ( !( $this -> data['status'] ?? 0 ) )
|
|
return false;
|
|
|
|
return !( $this -> data['used'] ?? 0 );
|
|
}
|
|
|
|
public function set_as_used()
|
|
{
|
|
$id = (int)( $this -> data['id'] ?? 0 );
|
|
if ( $id > 0 )
|
|
{
|
|
global $mdb;
|
|
$repo = new \Domain\Coupon\CouponRepository( $mdb );
|
|
$repo -> markAsUsed( $id );
|
|
$this -> data['used'] = 1;
|
|
}
|
|
}
|
|
|
|
public function __get( $variable )
|
|
{
|
|
return isset( $this -> data[$variable] ) ? $this -> data[$variable] : null;
|
|
}
|
|
|
|
public function __set( $variable, $value )
|
|
{
|
|
$this -> data[$variable] = $value;
|
|
}
|
|
|
|
public function offsetExists( $offset )
|
|
{
|
|
return isset( $this -> data[$offset] );
|
|
}
|
|
|
|
public function offsetGet( $offset )
|
|
{
|
|
return isset( $this -> data[$offset] ) ? $this -> data[$offset] : null;
|
|
}
|
|
|
|
public function offsetSet( $offset, $value )
|
|
{
|
|
$this -> data[$offset] = $value;
|
|
}
|
|
|
|
public function offsetUnset( $offset )
|
|
{
|
|
unset( $this -> data[$offset] );
|
|
}
|
|
}
|