public static function create($value) { if (!is_array($value) || !Arrays::isAssoc($value)) { throw new \InvalidArgumentException('Map requires an associative array'); } return parent::create($value); }
protected function _getAggregate($method, ISelectExpression $expression) { $originalClause = $this->_query->getClause('SELECT'); $this->_query->addClause((new SelectClause())->addExpression($expression)); $result = Arrays::first(Arrays::first($this->_getDataStore()->getData($this->_query))); $this->_query->addClause($originalClause); return $result; }
/** * @return array */ protected function _getData() { $interactor = new CurlInteractor(); $interactor->setResponseFactory(new SlackResponseFactory()); $commander = new Commander($this->_token, $interactor); $x = $commander->execute('channels.list'); return Arrays::value($x->getBody(), 'channels', []); }
/** * @return mixed */ protected function _getData() { $interactor = new CurlInteractor(); $interactor->setResponseFactory(new SlackResponseFactory()); $commander = new Commander($this->_token, $interactor); $x = $commander->execute('channels.history', ['channel' => $this->_channelId, 'inclusive' => 1, 'count' => 100]); return Arrays::value($x->getBody(), 'messages', []); }
protected function _verifyClause(GroupByClause $clause, $fields) { $fields = Arrays::fuse($fields); foreach ($clause->getFields() as $field) { unset($fields[$field->getField()]); } return empty($fields); }
public function getMessage() { if ($this->isSuccess()) { return 'Invitation sent'; } else { $error = Arrays::value($this->getResponse(), 'error', 'Error'); return $this->_decodeErrorMessage($error); } }
public static function getBWDisplayValue($value) { $bits = []; for ($i = 1; $i <= $value; $i = $i * 2) { if ($i & $value) { $bits[] = static::getDisplayValue($i); } } return Arrays::toList($bits); }
private function _getNewSet(...$expressions) { $newPredicates = WhereClause::buildPredicates($expressions); if (count($newPredicates) === 1) { $newSet = Arrays::first($newPredicates); } else { $newSet = new PredicateSet(); $newSet->setPredicates($newPredicates); } return $newSet; }
/** * Hydrate the public properties * * @param $data */ public function hydrate($data) { if ($data) { foreach (Objects::propertyValues($this) as $key => $value) { if (is_array($data)) { $this->{$key} = Arrays::value($data, $key, $value); } else { if (is_object($data)) { $this->{$key} = Objects::property($data, $key, $value); } } } } }
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); }
public function __construct($uri) { $uri = (string) $uri; $matches = null; if (preg_match('(^([^/:]*://[^/]*)(\\?.*)\\z)', $uri, $matches)) { // If the URI is something like `idea://open?file=/path/to/file`, the // `parse_url()` function will parse `open?file=` as the host. This is // not the expected result. Break the URI into two pieces, stick a slash // in between them, parse that, then remove the path. See T6106. $parts = parse_url($matches[1] . '/' . $matches[2]); unset($parts['path']); } else { $parts = parse_url($uri); } // The parse_url() call will accept URIs with leading whitespace, but many // other tools (like git) will not. See T4913 for a specific example. If // the input string has leading whitespace, fail the parse. if ($parts) { if (ltrim($uri) != $uri) { $parts = false; } } // NOTE: `parse_url()` is very liberal about host names; fail the parse if // the host looks like garbage. if ($parts) { $host = Arrays::value($parts, 'host', ''); if (!preg_match('/^([a-zA-Z0-9\\.\\-]*)$/', $host)) { $parts = false; } } if (!$parts) { $parts = []; } // stringyness is to preserve API compatibility and // allow the tests to continue passing $this->setProtocol(Arrays::value($parts, 'scheme', '')); $this->setUser(rawurldecode(Arrays::value($parts, 'user', ''))); $this->setPass(rawurldecode(Arrays::value($parts, 'pass', ''))); $this->setDomain(Arrays::value($parts, 'host', '')); $this->setPort((string) Arrays::value($parts, 'port', '')); $this->setPath(Arrays::value($parts, 'path', '')); parse_str(Arrays::value($parts, 'query'), $this->query); $this->setFragment(Arrays::value($parts, 'fragment', '')); }
public static function getClientIp() { static $ip; if ($ip === null) { $ipKeys = ['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR']; foreach ($ipKeys as $ipKey) { $ipString = Arrays::value($_SERVER, $ipKey); if ($ipString !== null) { foreach (explode(",", $ipString) as $ip) { if (filter_var($ip, FILTER_VALIDATE_IP) !== false) { return $ip; } } } } $ip = ""; } return $ip; }
public function getDescription() { $policy = $this->_dataNodeLink('policy', null, 'Traffic Blocking Policy'); $return = 'Affiliate ' . $policy; if ($this->policy->resource) { $return .= ' for ' . $this->_dataNodeLink('policy', 'resource'); } // Extras $extras = []; if ($this->redirectUrl) { $extras[] = ' a redirect URL of "' . $this->redirectUrl . '"'; } if ($this->blockMessage) { $extras[] = ' a message of "' . $this->blockMessage . '"'; } if ($extras) { $return .= ' with ' . Arrays::toList($extras, ', ', ' and '); } return $return . ' was created'; }
public function getValueRow() { $cols = [new Th('Value')]; foreach (SortFieldEnum::all() as $col) { $value = Arrays::value($this->getUser(), $col, 0); $formatted = number_format($value); switch ($col) { case SortFieldEnum::LEVEL: $val = new A('/experience/' . $value, $formatted); break; case SortFieldEnum::TIME: $val = $this->getMinutes($value * 60); break; default: $val = $formatted; break; } $cols[] = new Td($val); } return new Tr($cols); }
public function getLinks() { $id = $this->getApp()->id; $links = []; $links['Steam Community'] = 'https://steamcommunity.com/app/' . $id; $links['Steam Store'] = 'http://store.steampowered.com/app/' . $id; $links['Official Website'] = $this->getApp()->website; $arr = $this->getApp()->support_info ?: '[]'; $arr = json_decode($arr, true); $links['Support Site'] = Arrays::value($arr, 'url'); $arr = $this->getApp()->metacritic ?: '[]'; $arr = json_decode($arr, true); $links['Metacritic'] = Arrays::value($arr, 'url'); $return = []; foreach ($links as $k => $v) { if ($v) { $return[] = new Tr(new Td(new A($v, $k))); } } return (new Table())->setContent($return)->addClass('table table-hover'); }
/** * 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); }
/** * Retrieve the current visitor ID from the $_COOKIE global * * @return string|null */ public function getVisitorIdCookie() { return Arrays::value($_COOKIE, CookieReference::VISITOR_ID); }
/** * @param int $steamId * * @return BadgesResponse * * @throws SteamException */ public function getBadges($steamId) { $path = 'GetBadges/v1'; $query = ['steamId' => $steamId]; $data = $this->_get($path, $query)['response']; if (!$data) { return new BadgesResponse(); } $badges = new BadgesResponse(['playerXp' => $data['player_xp'], 'playerLevel' => $data['player_level'], 'playerXpNeededToLevelUp' => $data['player_xp_needed_to_level_up'], 'playerXpNeededCurrentLevel' => $data['player_xp_needed_current_level'], 'badges' => Arrays::value($data, 'badges', [])]); $items = []; foreach ($badges->badges as $badge) { $items[] = new BadgeResponse(['id' => $badge['badgeid'], 'level' => $badge['level'], 'completion_time' => $badge['completion_time'], 'xp' => $badge['xp'], 'scarcity' => $badge['scarcity']]); } $badges->badges = $items; return $badges; }
/** * return the first non-empty value of an array from * a specified list of keys * * @param array $array * @param array $properties * @param null $default * * @return mixed * * @deprecated */ function inonempty(array $array, array $properties, $default = null) { return \Packaged\Helpers\Arrays::inonempty($array, $properties, $default); }
public function ajaxSearchMaintainersInit() { $data = $this->_getRequest()->query->all(); $ids = Arrays::value($data, 'ids', ''); $ids = explode(',', $ids); $ids = array_filter($ids); asort($ids); $memcache = new \Memcached(); $key = 'searchMaintainersInit-' . implode('-', $ids); $searchMaintainersInit = $memcache->get($key); if ($searchMaintainersInit === false) { if (!$ids) { return []; } $authors = Maintainer::select('id', 'name')->whereIn('id', $ids)->orderBy('name', 'asc')->get(); $searchMaintainersInit = []; foreach ($authors as $item) { $searchMaintainersInit[] = ['id' => $item->id, 'text' => $item->name]; } $memcache->set($key, $searchMaintainersInit, self::WEEK); } return $searchMaintainersInit; }
/** * @return $this */ private function _sortByRand() { $this->words = Arrays::shuffleAssoc($this->words); return $this; }
public function setPredicates(array $predicates) { $this->_predicates = Arrays::instancesOf($predicates, '\\Packaged\\QueryBuilder\\Predicate\\IPredicate'); }
/** * @return string */ public function getRequestHeader() { return Arrays::value($this->_info, 'request_header'); }
/** * @return ForumCategoriesResponse[] */ public function getForumCategories() { $response = $this->_get(['action' => 'forum', 'type' => 'main']); $response = Arrays::value($response, 'categories', []); foreach ($response as $k => $section) { $response[$k] = new ForumCategoriesResponse($section); foreach ($response[$k]->forums as $k2 => $result) { $response[$k]->forums[$k2] = new ForumCategoriesResultResponse($result); } } return $response; }
/** * Get all changed properties since load * * @return array[property] => ['from' => '','to' => ''] */ public function getDaoChanges() { $current = $this->getDaoPropertyData(); $changes = []; foreach ($current as $key => $val) { if ($val !== $this->_savedData[$key]) { if ($this->{$key} instanceof Counter && !$this->{$key}->hasChanged()) { continue; } $changes[$key] = ['from' => Arrays::value($this->_savedData, $key), 'to' => Arrays::value($current, $key)]; } } return $changes; }
/** * Add an element between every two elements of some array. That is, given a * list `A, B, C, D`, and some element to interleave, `x`, this function returns * `A, x, B, x, C, x, D`. This works like `implode()`, but does not concatenate * the list into a string. In particular: * * implode('', array_interleave($x, $list)); * * ...is equivalent to: * * implode($x, $list); * * One case where this is useful is in rendering lists of HTML elements * separated by some character, like a middle dot: * * phutil_tag( * 'div', * array(), * array_interleave(" \xC2\xB7 ", $stuff)); * * This function does not preserve keys. * * @param $interleave mixed Element to interleave. * @param $array array List of elements to be interleaved. * * @return array Original list with the new element interleaved. * @group util * * @deprecated */ function array_interleave($interleave, array $array) { return \Packaged\Helpers\Arrays::interleave($interleave, $array); }
public function testShuffleAssoc() { $this->assertEquals('string', Arrays::shuffleAssoc('string')); $expected = ['x' => 'x', 'y' => 'y', 'z' => 'z']; $shuffled = Arrays::shuffleAssoc($expected); $this->assertFalse((bool) array_diff_assoc($expected, $shuffled)); $this->assertFalse((bool) array_diff_assoc($shuffled, $expected)); srand(40); $expected = ['z' => 'z', 'y' => 'y', 'x' => 'x']; $shuffled = Arrays::shuffleAssoc($expected); $this->assertEquals(json_encode($expected), json_encode($shuffled)); }
/** * Retrieve the first available dao * * @param mixed $default * * @return IDao */ public function first($default = null) { $this->_prepareDaos(); if (empty($this->_daos)) { return $default; } return Arrays::first((array) $this->_daos); }
public function setFields(array $fields) { $this->_fields = Arrays::instancesOf($fields, '\\Packaged\\QueryBuilder\\Expression\\FieldExpression'); return $this; }
public function getOrder($field, $default = 'ASC') { return Arrays::value($this->_order, array_search($field, $this->_fields), $default); }