<?php
/**
 * Search class
 *
 * @author Lucas Dantas <lucas@caju.nu>
 * @package classes
 * @subpackage search
 * @version 1.0
 * @since 2018/04/11 14:42
 */
Class SearchController{

	/**
	 * Defining the repository
	 *
	 * @access public
	 * @static
	 * @param string $type Instance's type
	 * @uses SearchRepositoryDBR::getInstance Defining the instance
	 * @return mixed
	 */
	public static function getRepository( $type = null ){

		//Returning
		return SearchRepositoryDBR::getInstance( $type );

	}

	/**
	 * Searching by your identifier
	 *
	 * @access public
	 * @static
	 * @param string $type Type
	 * @param integer $id Identifier
	 * @uses Validation::numericFields Validating the numeric fields
	 * @uses self::getRepository Getting the repository
	 * @uses SearchRepositoryDBR->byId Searching by your identifier
	 * @return stdclass
	 */
	public static function byId( $type = 'institutional', $id = 1 ){

		//Validating
		Validation::numericFields( [ 'identificador' => $id ] );

		//Searching and returning
		return self::getRepository( $type )->byId( $id );

	}

	/**
	 * Searching by your slug
	 *
	 * @access public
	 * @static
	 * @param string $type Type
	 * @param integer $slug Slug
	 * @uses Validation::emptyFields Validating the empty fields
	 * @uses self::getRepository Getting the repository
	 * @uses SearchRepositoryDBR->bySlug Searching by your slug
	 * @uses self::byId Searching by your identifier
	 * @return stdclass
	 */
	public static function bySlug( $type = 'product', $slug = null ){

		//Validating
		Validation::emptyFields( [ 'slug' => $slug ] );

		//Defining
		$object	= self::getRepository( $type )->bySlug( $slug );

		//Checking
		if( $type == 'product' )
			//Defining
			$object->category	= self::byId( 'product-category', 5 );

		//Returning
		return $object;

	}

	/**
	 * Searching by your identifier and destination
	 *
	 * @access public
	 * @static
	 * @param integer $id Identifier
	 * @param integer $destination Destination
	 * @uses Validation::numericFields Validating the numeric fields
	 * @uses self::getRepository Defining the repository
	 * @uses SearchRepositoryDBR->byId Searching by your identifier
	 * @uses Util::defineDestinationOptions Defining the destination's options
	 * @uses ListController::byFormOrderly Listing by form orderly
	 * @return stdclass
	 */
	public static function byIdByDestination( $id = null, $destination = null ){

		//Validating
		Validation::numericFields( [ 'identificador' => $id, 'destino' => $destination ] );

		//Defining
		$options			= Util::defineDestinationOptions( $destination );

		//Defining
		$object				= self::getRepository( $options->type )->byId( $id );

		//Defining the answers
		$object->answers	= ListController::byFormOrderly( 'answer-' . $options->type, $id );

		//Returning
		return $object;

	}

}