<?php

class FavoriteStationRepository extends ModelRepository
{
  private static $_singletonInstance = null;

  public function __construct()
  {
    parent::__construct('FavoriteStation');
  }

  /**
   * Returns an initiated FavoriteStationRepository
   *
   * @return FavoriteStationRepository
   */
  public static function getInstance()
  {
    if (self::$_singletonInstance === null)
    {
        self::$_singletonInstance = new FavoriteStationRepository();
    }

    return self::$_singletonInstance;
  }


  /**
   * Get FavoriteStation object by id
   *
   * @param  int $id
   * @return FavoriteStation
   */
  public function getObjectById($id)
  {
    if (!isInt($id))
    {
        return new FavoriteStation(0);
    }

    static $cache = array();
    $key = $id;
    if (!isset($cache[$key]))
    {
        $cache[$key] = $this->getObjectFromSql('select * from favorite_stations where id = ? LIMIT 1', [$id]);
    }
    return $cache[$key];
  }


  /**
   * Get FavoriteStation object by id
   *
   * @param  int $id
   * @return FavoriteStation
   */
  public function getObjectByUserAndStationId($user_id, $station_id)
  {
    if (!isInt($user_id) || !isInt($station_id))
    {
        return new FavoriteStation(0);
    }

    static $cache = array();
    $key = $user_id . '-'. $station_id;
    if (!isset($cache[$key]))
    {
        $cache[$key] = $this->getObjectFromSql('select * from favorite_stations where user_id = ? AND station_id = ? LIMIT 1', [$user_id, $station_id]);
    }
    return $cache[$key];
  }


  /**
   * Get all FavoriteStations by user id
   *
   * @param  int $id
   * @return FavoriteStation
   */
  public function getObjectListByUserId($user_id)
  {
    if (!isInt($user_id))
    {
        return new FavoriteStation(0);
    }

    static $cache = array();
    $key = $user_id;
    if (!isset($cache[$key]))
    {
        $cache[$key] = $this->getObjectListFromSql('select * from favorite_stations where user_id = ?', [$user_id]);
    }
    return $cache[$key];
  }


  /**
   * Returns an object based on the provided sql
   *
   * @param  string $sql
   * @param  array  $arg
   * @return StandardItem
   */
  public function getObjectFromSql($sql, $arg)
  {
    $pdo = PDOMysqlConnection::getInstance();

    $stmt = $pdo->prepareAndExec($sql, $arg);
    if ($record = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        return $this->_getObjectFromRecord($record);
    }

    // No object found, return empty object
    return new FavoriteStation(0);
  }


  /**
   * Returns an array of object based on the provided sql
   *
   * @param  string $sql
   * @param  array  $arg
   * @param string $index - Column to use as array index for values returned
   * @return array
   */
  public function getObjectListFromSql($sql, $arg, $index = null)
  {
    $pdo = PDOMysqlConnection::getInstance();

    $stmt = $pdo->prepareAndExec($sql, $arg);
    $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
    if (is_array($records) && !empty($records))
    {
        return $this->_getObjectListFromRecords($records, $index);
    }

    // No object found, return empty array
    return [];
  }
}
