<commit_before><?php

class ApiClient {
	private $url = "http://getrets.net";
	public function getUrl() { return $this->url; }
	public function setUrl($value) { $this->url = $value; return $this; }
	
	private $customerKey = "";
	public function getCustomerKey() { return $this->customerKey; }
	public function setCustomerKey($value) { $this->customerKey = $value; return $this; }
	
	public function __construct($customerKey, $url = null) {
		$this->setCustomerKey($customerKey);
		if ($url) {
			$this->setUrl($url);
		}
	}
	
	public function getFromApi($getUrl) {
		$contents = null;
		$results = null;

		if (file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
			$contents = file_get_contents($getUrl);
		}
		else if (function_exists('curl_version')) {
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $getUrl);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			$contents = curl_exec($curl);
			curl_close($curl);
		}
		else {
			die('You have neither cUrl installed nor allow_url_fopen activated. Please setup one of those!');
		}

		if (!empty($contents)) {
			$results = json_decode($contents);
		}

		return $results;
	}
	
	public function postToApi($postUrl, $postData, $encodeData = true) {
            
		$contents = null;
		$results = null;
                
		if (file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
                        $payload = ($encodeData ? json_encode($postData) : $postData);
                        $context = stream_context_create(array(
                                                        'http' => array(
                                                                'method' => 'POST',
                                                                'header' => "Content-Type: application/json; charset=utf-8\r\n",
                                                                'content' => $payload)));

                        $contents = file_get_contents($postUrl, false, $context);
                        
                        if($contents === false){
                                die('Error');
                        }
		}
		else if (function_exists('curl_version')) {
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $postUrl);
                        curl_setopt($curl, CURLOPT_POST, count($postData));
                        $postFields = null;
                        foreach($postData as $key => $value) {
                            $postFields .= $key . '=' . (is_bool($value) ? ($value ? 'true' : 'false') : $value) . '&';
                        }
                        //$postFields = http_build_query($postData);
                        curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
                        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			$contents = curl_exec($curl);
			curl_close($curl);
		}
		else {
			die('You have neither cUrl installed nor allow_url_fopen activated. Please setup one of those!');
		}

		if (!empty($contents)) {
			$results = json_decode($contents);
		}

		return $results;
	}
}

class Listing extends ApiClient {
	private $searchType = "Listing";
	public function getSearchType() { return $this->searchType; }
	public function setSearchType($value) { $this->searchType = $value; return $this; }
	
	private $sortBy = "rawListPrice";
	public function getSortBy() { return $this->sortBy; }
	public function setSortBy($value) { $this->sortBy = $value; return $this; }
        
        private $reverseSort = false;
	public function getReverseSort() { return $this->reverseSort; }
	public function setReverseSort($value) { $this->reverseSort = $value; return $this; }
        
        protected function sort() {
            $key = $this->getSortBy();
            $reverse = $this->getReverseSort();
            return function ($a, $b) use ($key, $reverse) {
                if (is_string($a->$key)) {
                    return strcmp($a->$key, $b->$key) * ($reverse ? -1 : 1);
                }
                else {
                    return ($reverse ? $a->$key < $b->$key : $a->$key > $b->$key);
                }
            };
        }
        
        protected function sortResults($results) {
            $output = $results;
            
            if (!empty($this->getSortBy())) {
                usort($output, $this->sort());
            }
            
            return $output;
        }
	
	public function searchByKeyword($keyword) {
            $results = $this->getFromAPI($this->getUrl() . '/api/' . $this->getCustomerKey() . '/'. $this->searchType . '/SearchByKeyword/' . rawurlencode($keyword));
            return $this->sortResults($results);
	}

	public function search($keyword, $maxPrice, $minPrice, $includeResidential, $includeLand, $includeCommercial) {
		$postData = array('Keyword' => $keyword,
						  'MaxPrice' => intval($maxPrice),
						  'MinPrice' => intval($minPrice),
						  'IncludeResidential' => boolval($includeResidential),
						  'IncludeLand' => boolval($includeLand),
						  'IncludeCommercial' => boolval($includeCommercial));

		$results = $this->postToAPI($this->getUrl() . '/api/' . $this->getCustomerKey() . '/'. $this->getSearchType() . '/Search', $postData);
                <commit_msg>Fix for php versions before 5.5 (no empty checks) - Line 118 of GetRETS.php.<commit_after><?php

class ApiClient {
	private $url = "http://getrets.net";
	public function getUrl() { return $this->url; }
	public function setUrl($value) { $this->url = $value; return $this; }
	
	private $customerKey = "";
	public function getCustomerKey() { return $this->customerKey; }
	public function setCustomerKey($value) { $this->customerKey = $value; return $this; }
	
	public function __construct($customerKey, $url = null) {
		$this->setCustomerKey($customerKey);
		if ($url) {
			$this->setUrl($url);
		}
	}
	
	public function getFromApi($getUrl) {
		$contents = null;
		$results = null;

		if (file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
			$contents = file_get_contents($getUrl);
		}
		else if (function_exists('curl_version')) {
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $getUrl);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			$contents = curl_exec($curl);
			curl_close($curl);
		}
		else {
			die('You have neither cUrl installed nor allow_url_fopen activated. Please setup one of those!');
		}

		if (!empty($contents)) {
			$results = json_decode($contents);
		}

		return $results;
	}
	
	public function postToApi($postUrl, $postData, $encodeData = true) {
            
		$contents = null;
		$results = null;
                
		if (file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
                        $payload = ($encodeData ? json_encode($postData) : $postData);
                        $context = stream_context_create(array(
                                                        'http' => array(
                                                                'method' => 'POST',
                                                                'header' => "Content-Type: application/json; charset=utf-8\r\n",
                                                                'content' => $payload)));

                        $contents = file_get_contents($postUrl, false, $context);
                        
                        if($contents === false){
                                die('Error');
                        }
		}
		else if (function_exists('curl_version')) {
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $postUrl);
                        curl_setopt($curl, CURLOPT_POST, count($postData));
                        $postFields = null;
                        foreach($postData as $key => $value) {
                            $postFields .= $key . '=' . (is_bool($value) ? ($value ? 'true' : 'false') : $value) . '&';
                        }
                        //$postFields = http_build_query($postData);
                        curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
                        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			$contents = curl_exec($curl);
			curl_close($curl);
		}
		else {
			die('You have neither cUrl installed nor allow_url_fopen activated. Please setup one of those!');
		}

		if (!empty($contents)) {
			$results = json_decode($contents);
		}

		return $results;
	}
}

class Listing extends ApiClient {
	private $searchType = "Listing";
	public function getSearchType() { return $this->searchType; }
	public function setSearchType($value) { $this->searchType = $value; return $this; }
	
	private $sortBy = "rawListPrice";
	public function getSortBy() { return $this->sortBy; }
	public function setSortBy($value) { $this->sortBy = $value; return $this; }
        
        private $reverseSort = false;
	public function getReverseSort() { return $this->reverseSort; }
	public function setReverseSort($value) { $this->reverseSort = $value; return $this; }
        
        protected function sort() {
            $key = $this->getSortBy();
            $reverse = $this->getReverseSort();
            return function ($a, $b) use ($key, $reverse) {
                if (is_string($a->$key)) {
                    return strcmp($a->$key, $b->$key) * ($reverse ? -1 : 1);
                }
                else {
                    return ($reverse ? $a->$key < $b->$key : $a->$key > $b->$key);
                }
            };
        }
        
        protected function sortResults($results) {
            $output = $results;
            
			$sbCheck = $this->getSortBy();
            if (!empty($sbCheck)) {
                usort($output, $this->sort());
            }
            
            return $output;
        }
	
	public function searchByKeyword($keyword) {
            $results = $this->getFromAPI($this->getUrl() . '/api/' . $this->getCustomerKey() . '/'. $this->searchType . '/SearchByKeyword/' . rawurlencode($keyword));
            return $this->sortResults($results);
	}

	public function search($keyword, $maxPrice, $minPrice, $includeResidential, $includeLand, $includeCommercial) {
		$postData = array('Keyword' => $keyword,
						  'MaxPrice' => intval($maxPrice),
						  'MinPrice' => intval($minPrice),
						  'IncludeResidential' => boolval($includeResidential),
						  'IncludeLand' => boolval($includeLand),
						  'IncludeCommercial' => boolval($includeCommercial));

		$results = $this->postToAPI($this->getUrl() . '/api/' . $this->getCustomerKey() . '/'. $this->getSearchType() . '/Search', $postData);
                
{'fraction_non_alphanumeric': 0.14620715845438148, 'fraction_numerical': 0.0010443368461027249, 'mean_word_length': 2.0693473193473193, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1615691', 'n_tokens_mistral': 3264, 'n_tokens_neox': 2939, 'n_words': 628}