<?php
namespace shop;

class ProductSet implements \ArrayAccess
{
  public function offsetExists($offset) {
    return isset($this->$offset);
  }

  public function offsetGet($offset) {
    return $this->$offset;
  }

  public function offsetSet($offset , $value) {
    $this->$offset = $value;
  }

  public function offsetUnset($offset) {
    unset($this->$offset);
  }

  public function __construct( int $set_id )
  {
    global $mdb;

    $result = $mdb -> get( 'pp_shop_product_sets', '*', [ 'id' => $set_id ] );
    if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
      $this -> $key = $val;

    $this -> products = $mdb -> select( 'pp_shop_product_sets_products', 'product_id', [ 'set_id' => $set_id ] );
  }

  //lista dostępnych kompletów
  static public function sets_list()
  {
    global $mdb;
    return $mdb -> select( 'pp_shop_product_sets', [ 'id', 'name' ], [ 'ORDER' => [ 'name' => 'ASC' ] ] );
  }

  // usuwanie kompletu produktów
  static public function set_delete( int $set_id )
  {
    global $mdb;

    if (
        $mdb -> delete( 'pp_shop_product_sets_products', [ 'set_id' => $set_id ] )
      and
        $mdb -> delete( 'pp_shop_product_sets', [ 'id' => $set_id ] )
    )
      return true;

    return false;
  }
}