<?php
/**
 * Yelp API Implementation
 * @example 
 * 	$Yelp = new Yelp();
 * 	//$res = $Yelp->businessSearch(array('term'=>'tacos', 'location'=>'sf', 'output'=>'php'));
 * 	$res = $Yelp->getBusinessDetail('tacos-el-san-francisco');
 * 	echo "<pre>";
 * 	print_r($res);
 * 
 */

/**
 * Loading OAuth lib (Required for Yelp API version 2.0)
 */
require_once ('lib/php/include/OAuth.php');

/**
 * A class to load Yelp Listings & their details
 *
 */
class Yelp {

	/**
	 * YWSID (Required for Yelp API version 1.0)
	 *
	 * @var string
	 */
	private $YWSID = "syJlZreoSV3RRhovF436WQ";
	
	/**
	 * consumer_key (Required for Yelp API version 2.0)
	 *
	 * @var string
	 */
	private $consumer_key = "Qbeq5HfbDvhH76zgSotlKA";
	
	/**
	 * consumer_secret (Required for Yelp API version 2.0)
	 *
	 * @var string
	 */
	private $consumer_secret = "GIqWWy0TkjU6yLX1o_ozKvQUzeY";
	
	/**
	 * token (Required for Yelp API version 2.0)
	 *
	 * @var string
	 */
	private $token = "ytZ9p-5xKpf1XuEDQMFyVn1_MMs3_EpX";
	
	/**
	 * token_secret (Required for Yelp API version 2.0)
	 *
	 * @var string
	 */
	private $token_secret = "Gv92uLmQ4nzHAygZ_VxcnyZmMk8";
	
	/**
	 * URL to get business details
	 *
	 * @var string
	 */
	private $business_url = "http://api.yelp.com/v2/business/";
	
	/**
	 * Search URL
	 *
	 * @var string
	 */
	private $search_url = "http://api.yelp.com/v2/search?";
	
	/**
	 * Business Reviews Search URL
	 *
	 * @var string
	 */
	private $business_search_url = "http://api.yelp.com/business_review_search?";
	
	/**
	 * Result output format
	 * default - php (serialized)
	 *
	 * @var string
	 */
	private $output_format = "php";
	
	/**
	 * Allowed output formats
	 *
	 * @var array
	 */
	private $allowed_formats = array("php", "json");

	/**
	 * contructor function
	 *
	 */
	public function __construct() {}

	/**
	 * Sign the URL with authentication token
	 *
	 * @param string $unsigned_url
	 * @return string Signed URL
	 */
	private function getSignedURL($unsigned_url) {

		// Token object built using the OAuth library
		$token = new OAuthToken($this->token, $this->token_secret);

		// Consumer object built using the OAuth library
		$consumer = new OAuthConsumer($this->consumer_key, $this->consumer_secret);

		// Yelp uses HMAC SHA1 encoding
		$signature_method = new OAuthSignatureMethod_HMAC_SHA1();

		// Build OAuth Request using the OAuth PHP library. Uses the consumer and token object created above.
		$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);

		// Sign the request
		$oauthrequest->sign_request($signature_method, $consumer, $token);

		// Get the signed URL
		return $oauthrequest->to_url();
	}

	/**
	 * Send CURL request to load url response
	 *
	 * @param string $signed_url
	 * @return json decoded or php unserialized array depending upon 'output_format' variable
	 */
	private function sendRequest($signed_url) {

		$ch = curl_init($signed_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
		$data = curl_exec($ch);
		curl_close($ch);
		if($this->output_format == 'json') {
			
			return json_decode($data);
		}
		else {
			//Default - PHP
			return unserialize($data);
		}
	}

	/**
	 * Set response output format
	 *
	 * @param array_type $params
	 * @return array
	 */
	private function setOutputFormat($params) {

		if(isset($params['output']) && in_array($params['output'], $this->allowed_formats)) {
			$this->output_format = $params['output'];
		}
		else {
			$params['output'] = $this->output_format;
		}
		return $params;
	}

	/**
	 * Call a search query to Yelp
	 *
	 * @param array $params
	 * @param string $output_format
	 * @return 'php array' or 'PHP object'
	 */
	public function businessSearch($params, $output_format='php') {

		foreach ($this->setOutputFormat($params) as $key=>$val) {
			$this->search_url .= "&$key=".urlencode($val);
		}
		return $this->sendRequest($this->getSignedURL($this->search_url));
	}
	
	/**
	 * Search for business reviews
	 *
	 * @param array $params
	 * @param string $output_format
	 * @return 'php array' or 'PHP object'
	 */
	public function businessReviewsSearch($params, $output_format='php') {

		foreach ($this->setOutputFormat($params) as $key=>$val) {
			$this->business_search_url .= "&$key=".urlencode($val);
		}
		
		//Review search is using Yelp API version 1.0 so doesn't require signed URL
		return $this->sendRequest($this->business_search_url."&ywsid=$this->YWSID");
	}

	/**
	 * Get business details
	 *
	 * @param string $id
	 * @param string $output_format
	 * @return 'php array' or 'PHP object'
	 */
	public function getBusinessDetail($id, $output_format='php') {

		$this->output_format = in_array($output_format, $this->allowed_formats)?$output_format:$this->output_format;
		return $this->sendRequest($this->getSignedURL($this->business_url.$id."?output=".$this->output_format));
	}
}
?>
