public function view($appId) { $app = App::firstOrNew(['id' => $appId]); // Update database if (!$app->exists || $app->updated_at < date('Y-m-d G:i:s', strtotime('-7 days')) || $this->_getRequest()->query->has('u')) { $api = new SteamStore($this->_getApi()); try { $response = $api->appDetails($appId); } catch (SteamAppNotFoundException $e) { return new AppNotFoundView(); } $app->type = $response->type; $app->name = $response->name; $app->required_age = $response->required_age; $app->is_free = $response->is_free; $app->detailed_description = $response->detailed_description; $app->about_the_game = $response->about_the_game; $app->supported_languages = $response->supported_languages; $app->header_image = $this->removeQueryString($response->header_image); $app->website = $response->website; $app->release_date = json_encode(Objects::property($response, 'release_date', [])); $app->support_info = json_encode(Objects::property($response, 'support_info', [])); $app->metacritic = json_encode(Objects::property($response, 'metacritic', [])); $app->categories = json_encode(Objects::property($response, 'categories', [])); $app->genres = json_encode(Objects::property($response, 'genres', [])); $app->screenshots = json_encode(Objects::property($response, 'screenshots', [])); $app->achievements = json_encode(Objects::property($response, 'achievements', [])); $app->movies = json_encode(Objects::property($response, 'movies', [])); // Categories $ids = []; if (is_array($response->categories)) { $ids = Arrays::ipull($response->categories, 'id'); } $app->categories()->sync($ids); // Genres $ids = []; if (is_array($response->genres)) { $ids = Arrays::ipull($response->genres, 'id'); } $app->genres()->sync($ids); // Save $app->save(); } $this->layout()->setData('title', $app->name); return new AppView($app); }
/** * Find all distinct values of a property in the collection * * @param $property * * @return array */ public function distinct($property) { if ($this->isEmpty()) { $select = new SelectClause(); $select->setDistinct(true); $select->addField($property); $originalClause = $this->_query->getClause('SELECT'); $this->_query->addClause($select); $results = $this->_getDataStore()->getData($this->_query); $this->_query->addClause($originalClause); if (empty($results)) { return []; } return Arrays::ipull($results, $property); } return parent::distinct($property); }
public function testIpull() { $list = [['name' => 'a', 'value' => 1], ['name' => 'b', 'value' => 2], ['name' => 'c', 'value' => 3]]; $expected = ["a", "b", "c"]; $this->assertEquals($expected, Arrays::ipull($list, 'name')); $expected = ['a' => 1, 'b' => 2, 'c' => 3]; $this->assertEquals($expected, Arrays::ipull($list, 'value', 'name')); $expected = ['a' => ['name' => 'a', 'value' => 1], 'b' => ['name' => 'b', 'value' => 2], 'c' => ['name' => 'c', 'value' => 3]]; $this->assertEquals($expected, Arrays::ipull($list, null, 'name')); }
/** * Choose an index from a list of arrays. Short for "index pull", this function * works just like @{function:mpull}, except that it operates on a list of * arrays and selects an index from them instead of operating on a list of * objects and calling a method on them. * * This function simplifies a common type of mapping operation: * * COUNTEREXAMPLE * $names = array(); * foreach ($list as $key => $dict) { * $names[$key] = $dict['name']; * } * * With ipull(): * * $names = ipull($list, 'name'); * * See @{function:mpull} for more usage examples. * * @param $list array Some list of arrays. * @param $index mixed|null Determines which **values** * will appear in the result array. Use a scalar to * select that index from each array, or null to * preserve the arrays unmodified as values. * @param $keyIndex string|null Determines which **keys** will * appear in the result array. Use a scalar to select * that index from each array, or null to preserve * the array keys. * * @return array A dictionary with keys and values derived according * to whatever you passed for $index and $key_index. * @group util * * @deprecated */ function ipull(array $list, $index, $keyIndex = null) { return \Packaged\Helpers\Arrays::ipull($list, $index, $keyIndex); }