<?
namespace shop;
class Producer implements \ArrayAccess
{
  public function __construct( int $producer_id )
  {
    global $mdb;

    $result = $mdb -> get( 'pp_shop_producer', '*', [ 'id' => $producer_id ] );
    foreach ( $result as $key => $val )
      $this -> $key = $val;

    $rows = $mdb -> select( 'pp_shop_producer_lang', '*', [ 'producer_id' => $producer_id ] );
    foreach ( $rows as $row )
    {
      $languages[ $row['lang_id'] ]['description'] = $row['description'];
      $languages[ $row['lang_id'] ]['meta_title'] = $row['meta_title'];
    }

    $this -> languages = $languages;
  }

  static public function producer_products( $producer_id, $lang_id, $bs )
  {
    global $mdb;

    $count = $mdb -> count( 'pp_shop_products', [ 'AND' => [ 'producer_id' => $producer_id, 'status' => 1 ] ] );
    $ls = ceil( $count / 12 );

    if ( $bs < 1 )
      $bs = 1;
    else if ( $bs > $ls )
      $bs = $ls;

    $from = 12 * ( $bs - 1 );

    if ( $from < 0 )
      $from = 0;

    $results['products'] = $mdb -> select( 'pp_shop_products', 'id', [ 'AND' => [ 'producer_id' => $producer_id, 'status' => 1 ], 'LIMIT' => [ $from, 12 ] ] );

    $results['ls'] = $ls;

    return $results;
  }

  public function __get( $variable )
  {
    if ( array_key_exists( $variable, $this -> data ) )
      return $this -> $variable;
  }

  public function __set( $variable, $value )
  {
    $this -> $variable = $value;
  }

  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 );
  }
}