getModifiersFromRequest($request); return $this->json($stops->all(...$modifiers)->toArray()); } /** * @SWG\Response( * response=200, * description="Returns grouped stops for specific provider, e.g. ZTM Gdańsk.", * @SWG\Schema(type="array", @SWG\Items(ref=@Model(type=StopGroup::class))) * ) * * @SWG\Parameter( * name="name", * in="query", * type="string", * description="Part of the stop name to search for.", * ) * * @Route("/groups", methods={"GET"}) */ public function groups(Request $request, StopRepository $stops) { $modifiers = $this->getModifiersFromRequest($request); return $this->json(static::group($stops->all(...$modifiers))->toArray()); } /** * @SWG\Response( * response=200, * description="Returns specific stop referenced via identificator.", * @SWG\Schema(ref=@Model(type=Stop::class)) * ) * * @SWG\Parameter( * name="id", * in="path", * type="string", * description="Stop identificator as provided by data provider." * ) * * @Route("/{id}", methods={"GET"}) */ public function one(Request $request, StopRepository $stops, $id) { return $this->json($stops->first(new IdFilter($id), new IncludeDestinations())); } /** * @Route("/{id}/tracks", methods={"GET"}) * * @SWG\Response( * response=200, * description="Returns specific stop referenced via identificator.", * @SWG\Schema(type="object", properties={ * @SWG\Property(property="track", type="object", ref=@Model(type=Track::class)), * @SWG\Property(property="order", type="integer", minimum="0") * }) * ) * * @SWG\Tag(name="Tracks") */ public function tracks(ReferenceFactory $reference, TrackRepository $tracks, $id) { $stop = $reference->get(Stop::class, $id); return $this->json($tracks->getByStop($stop)->map(function ($tuple) { return array_combine(['track', 'order'], $tuple); })); } public static function group(Collection $stops) { return $stops->groupBy(function (Stop $stop) { return $stop->getGroup(); })->map(function ($stops, $key) { $group = new StopGroup(); $group->setName($key); $group->setStops($stops); return $group; })->values(); } private function getModifiersFromRequest(Request $request) { if ($request->query->has('name')) { yield FieldFilter::contains('name', $request->query->get('name')); } if ($request->query->has('id')) { yield new IdFilter($request->query->get('id')); } if ($request->query->has('include-destinations')) { yield new IncludeDestinations(); } } }