Skip to content

Commit

Permalink
Merge pull request #23 from lukasleitsch/feature/disable-time-parsing
Browse files Browse the repository at this point in the history
Add option to disable the time parsing in interest by subregion
  • Loading branch information
x-fran authored Nov 9, 2019
2 parents b53a788 + 8f5cccf commit 9aa18cf
Showing 1 changed file with 76 additions and 46 deletions.
122 changes: 76 additions & 46 deletions src/Google/GTrends.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,28 +430,32 @@ public function getRelatedTopics($kWord=null, $category=0, $time='today 12-m', $
return false;
}

/**
* @param array $keyWordList
* @param $resolution
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @return array|bool
* @throws \Exception
*/
private function _interestBySubregion(array $keyWordList, $resolution, $category=0, $time='now 1-h', $property='', $sleep=0.5)
/**
* @param array $keyWordList
* @param $resolution
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @param bool $disableTimeParsing
*
* @return array|bool
* @throws \Exception
*/
private function _interestBySubregion(array $keyWordList, $resolution, $category=0, $time='now 1-h', $property='', $sleep=0.5, $disableTimeParsing = false)
{
if (count($keyWordList) == 0 or count($keyWordList) > 5) {

throw new \Exception('Invalid number of items provided in keyWordList');
}

$timeInfo = explode('-', $time);
$timeInfo[0] = strtolower($timeInfo[0]);
$timeUnit = array_pop($timeInfo);
$timeInfo[] = strtoupper($timeUnit);
$time = implode('-', $timeInfo);
if(!$disableTimeParsing) {
$timeInfo = explode('-', $time);
$timeInfo[0] = strtolower($timeInfo[0]);
$timeUnit = array_pop($timeInfo);
$timeInfo[] = strtoupper($timeUnit);
$time = implode('-', $timeInfo);
}

$comparisonItem = [];
foreach ($keyWordList as $kWord) {
Expand Down Expand Up @@ -504,57 +508,83 @@ private function _interestBySubregion(array $keyWordList, $resolution, $category
return false;
}

/**
* @param array $keyWordList
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @return array|bool
*/
public function interestBySubregion(array $keyWordList, $category=0, $time='now 1-h', $property='', $sleep=0.5)
/**
* @param array $keyWordList
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @param bool $disableTimeParsing
*
* @return array|bool
*/
public function interestBySubregion(array $keyWordList, $category=0, $time='now 1-h', $property='', $sleep=0.5, $disableTimeParsing = false)
{
try {
return $this->_interestBySubregion($keyWordList, 'SUBREGION', $category, $time, $property, $sleep);
return $this->_interestBySubregion($keyWordList, 'SUBREGION', $category, $time, $property, $sleep, $disableTimeParsing);
} catch (\Exception $e) {
die($e->getMessage());
}
}

/**
* @param array $keyWordList
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @return array|bool
*/
public function interestByCity(array $keyWordList, $category=0, $time='now 1-h', $property='', $sleep=0.5)
/**
* @param array $keyWordList
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @param bool $disableTimeParsing
*
* @return array|bool
*/
public function interestByCity(array $keyWordList, $category=0, $time='now 1-h', $property='', $sleep=0.5, $disableTimeParsing = false)
{
try {
return $this->_interestBySubregion($keyWordList, 'CITY', $category, $time, $property, $sleep);
return $this->_interestBySubregion($keyWordList, 'CITY', $category, $time, $property, $sleep, $disableTimeParsing);
} catch (\Exception $e) {
die($e->getMessage());
}
}

/**
* @param array $keyWordList
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @return array|bool
*/
public function interestByMetro(array $keyWordList, $category=0, $time='now 1-h', $property='', $sleep=0.5)
/**
* @param array $keyWordList
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @param bool $disableTimeParsing
*
* @return array|bool
*/
public function interestByMetro(array $keyWordList, $category=0, $time='now 1-h', $property='', $sleep=0.5, $disableTimeParsing = false)
{
try {
return $this->_interestBySubregion($keyWordList, 'DMA', $category, $time, $property, $sleep, $disableTimeParsing);
} catch (\Exception $e) {
die($e->getMessage());
}
}

/**
* @param array $keyWordList
* @param int $category
* @param string $time
* @param string $property
* @param float $sleep
* @param bool $disableTimeParsing
*
* @return array|bool
*/
public function interestByRegion(array $keyWordList, $category=0, $time='now 1-h', $property='', $sleep=0.5, $disableTimeParsing = false)
{
try {
return $this->_interestBySubregion($keyWordList, 'DMA', $category, $time, $property, $sleep);
return $this->_interestBySubregion($keyWordList, 'REGION', $category, $time, $property, $sleep, $disableTimeParsing);
} catch (\Exception $e) {
die($e->getMessage());
}
}


/**
* @param $kWord
*
Expand Down

0 comments on commit 9aa18cf

Please sign in to comment.