Exemplo n.º 1
0
 public static function searchForGames($query, $limit = 10)
 {
     self::validateApiKey();
     //Change these if we need to include more stuff.
     $fieldArr = ['id', 'name', 'original_release_date', 'image', 'api_detail_url', 'deck', 'image', 'platforms'];
     //sanitise query
     $query = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($query))))));
     //generate url
     $url = self::$gb . 'games?api_key=' . self::$apikey . '&format=json' . self::formatFieldList($fieldArr) . '&limit=' . $limit . '&filter=platforms:17|94|152,name:' . $query;
     //Limit to Mac, PC, Linux respectively.
     try {
         $json = self::makeApiCall($url);
     } catch (Exception $e) {
         throw new GBApiException('Error contacting Giantbomb. Please try again later.');
     }
     //QueryDb and try to find the object using the giantbomb unique id
     if ($json->results == null || count($json->results) < 1) {
         throw new GBApiException('No results found. Please try another search.');
     }
     $games = [];
     //initialise as empty array
     foreach ($json->results as $result) {
         if (!isset($result->original_release_date) || $result->original_release_date == null) {
             continue;
         }
         //skip if not out yet.
         $game = GameQuery::create()->findOneByGbId($result->id);
         if (!isset($game)) {
             $game = new Game();
             $game->setGbId($result->id);
             $game->setName(Game::generateUniqueName($result->name, $result->original_release_date));
         }
         $game->setGbUrl($result->api_detail_url);
         $game->setTitle($result->name);
         $game->setDescription($result->deck);
         //We don't care if they don't have thumbs. Catch the exceptions and move on
         try {
             $game->setGbThumb($result->image->screen_url);
         } catch (Exception $e) {
         }
         try {
             $game->setGbImage($result->image->medium_url);
         } catch (Exception $e) {
         }
         $game->save();
         //Fetch all of the title's platforms from the results we pulled from GB and push them into an array
         $gbplatforms = [];
         foreach ($result->platforms as $gbplatform) {
             array_push($gbplatforms, $gbplatform->id);
         }
         //Remove platforms no longer associated with the title
         $currentPlatforms = $game->getPlatforms();
         foreach ($currentPlatforms as $plat) {
             if (!in_array($plat->getGbId(), $gbplatforms)) {
                 $game->removePlatform($plat);
             }
         }
         //add new platforms associated with the title
         $allPlatforms = Platform::getAllPlatforms();
         foreach ($allPlatforms as $plat) {
             if (in_array($plat->getGbId(), $gbplatforms)) {
                 $found = false;
                 foreach ($currentPlatforms as $curPlat) {
                     if ($curPlat->getId() == $plat->getId()) {
                         $found = true;
                         break;
                     }
                 }
                 if ($found) {
                     continue;
                 }
                 $game->addPlatform($plat);
             }
         }
         //append result to list.
         $game->save();
         array_push($games, $game);
     }
     return $games;
 }