public function includeNominations(Beatmapset $beatmapset, ParamBag $params = null)
 {
     if ($beatmapset->isPending()) {
         if ($params !== null) {
             $userId = get_int($params->get('user_id')[0] ?? null);
         }
         $nominations = $beatmapset->recentEvents()->get();
         foreach ($nominations as $nomination) {
             if ($nomination->type === BeatmapsetEvent::DISQUALIFY) {
                 $disqualifyEvent = $nomination;
             }
             if (isset($userId) && $nomination->user_id === $userId && $nomination->type === BeatmapsetEvent::NOMINATE) {
                 $alreadyNominated = true;
             }
         }
         $result = ['required' => $beatmapset->requiredNominationCount(), 'current' => $beatmapset->currentNominationCount()];
         if (isset($disqualifyEvent)) {
             $result['disqualification'] = ['reason' => $disqualifyEvent->comment, 'created_at' => json_time($disqualifyEvent->created_at)];
         }
         if (isset($userId)) {
             $result['nominated'] = $alreadyNominated ?? false;
         }
         return $this->item($beatmapset, function ($beatmapset) use($result) {
             return $result;
         });
     } elseif ($beatmapset->qualified()) {
         $eta = $beatmapset->rankingETA();
         $result = ['ranking_eta' => json_time($eta)];
         return $this->item($beatmapset, function ($beatmapset) use($result) {
             return $result;
         });
     } else {
         return;
     }
 }
Esempio n. 2
0
 /**
  * Parse the given ParamBag and merge them with default values.
  *
  * @return array
  */
 protected function parse()
 {
     $config = config('api.include.params');
     $partialKey = config('api.partial.key');
     $limit = $this->params->get('limit') ?: $config['limit'];
     $sort = $this->params->get('sort') ?: $config['sort'];
     $fields = $this->params->get($partialKey);
     $this->parsed = ['limit' => $limit[0], 'offset' => $limit[1], 'sort' => $sort[0], 'order' => $sort[1], $partialKey => $fields];
 }
Esempio n. 3
0
 /**
  * Validate include params.
  * We already define the white lists in the config.
  *
  * @return bool
  * @throws \UnexpectedValueException
  */
 protected function validateIncludeParams()
 {
     $validParams = array_keys($this->config['include']['params']);
     $usedParams = array_keys(iterator_to_array($this->paramBag));
     if ($invalidParams = array_diff($usedParams, $validParams)) {
         // This validates query string KEY passed by an API client.
         throw new UnexpectedValueException(sprintf('Used param(s): "%s". Valid param(s): "%s"', implode(',', $usedParams), implode(',', $validParams)));
     }
     $errors = [];
     if ($limit = $this->paramBag->get('limit')) {
         if (count($limit) !== 2) {
             array_push($errors, 'Invalid "limit" value. Valid usage: limit(int|int) where the first int is number of items to retrieve and the second is offset to skip over.');
         }
         foreach ($limit as $item) {
             if (!is_numeric($item)) {
                 array_push($errors, 'Invalid "limit" value. Expecting: integer. Given: ' . gettype($item) . " \"{$item}\".");
             }
         }
     }
     if ($sort = $this->paramBag->get('sort')) {
         if (count($sort) !== 2) {
             array_push($errors, 'Invalid "sort" value. Valid usage: sort(string|string) where the first string is attribute name to order by and the second is the sort direction(asc or desc)');
         }
         $allowedSortDirection = ['asc', 'desc'];
         if (isset($sort[1]) && !in_array(strtolower($sort[1]), $allowedSortDirection)) {
             array_push($errors, 'Invalid "sort" value. Allowed: ' . implode(',', $allowedSortDirection) . ". Given: \"{$sort[1]}\"");
         }
     }
     if (!empty($errors)) {
         throw new UnexpectedValueException(implode(PHP_EOL, $errors));
     }
     return true;
 }
 public function includeCurrentUserAttributes(BeatmapDiscussion $discussion, ParamBag $params = null)
 {
     if ($params === null) {
         return;
     }
     $userId = get_int($params->get('user_id')[0] ?? null);
     if ($userId === null) {
         return;
     }
     $score = 0;
     // This assumes beatmapDiscussionVotes are already preloaded and
     // thus will save one query.
     foreach ($discussion->beatmapDiscussionVotes as $vote) {
         if ($vote->user_id === $userId) {
             $score = $vote->score;
             break;
         }
     }
     return $this->item($discussion, function ($discussion) use($score) {
         return ['vote_score' => $score];
     });
 }