/** * Search for physicians. * * @return JSON */ public function search(Request $request) { $physicians = null; $searchDistance = $request->distance ? $request->distance : 0; $coords = $this->getCoordinates($request); $orderBy = $request->has('order_by') ? $request->order_by : 'distance'; $sort = $request->has('sort') ? $request->sort : 'asc'; $limit = $request->has('per_page') ? $request->per_page : '25'; // if we don't have a requested distance, we'll cycle through // our fallback distances until we get at least 1 result; // if we don't have anything by our max distance, we'll return 0. if (!$request->has('distance')) { while (!$physicians || $physicians->count() == 0) { $searchDistance = $this->getNextDistance($searchDistance); $physicians = Physician::withinDistance($coords['lat'], $coords['lon'], $searchDistance)->alias($request->alias_id)->name($request->q)->gender($request->gender); if ($searchDistance == $this->maxDistance) { break; } } } else { $physicians = Physician::withinDistance($coords['lat'], $coords['lon'], $searchDistance)->alias($request->alias_id)->name($request->q)->gender($request->gender); } $alias = Alias::find($request->alias_id); $queryMeta = ['city' => Str::title(urldecode($request->city)), 'state' => mb_strtoupper($request->state), 'zip' => $request->zip ? $request->zip : $this->getZip($request->city, $request->state), 'alias' => $alias ? $alias->alias : null, 'alias_id' => $alias ? $alias->id : null, 'aggregate' => AggregateReporter::report($physicians, $request->alias_id), 'q' => $request->q, 'gender' => $request->gender, 'count' => $physicians ? $physicians->count() : 0, 'radius' => $searchDistance, 'order_by' => $request->order_by, 'sort' => $request->sort, 'center' => ['lat' => $coords['lat'], 'lon' => $coords['lon']]]; $physicians = $physicians->orderBy($orderBy, $sort)->paginate($limit)->appends($request->query()); return $this->response->withPaginator($physicians, new PhysicianTransformer(), null, $queryMeta); }
/** * Convert array of [[alias_id => count]] items into * an array suitable for returning in the meta entry. * * @param array * @return array */ private static function translateAliases($aliasCounts) { $aliasesForMeta = []; foreach ($aliasCounts as $aliasCount) { $a = Alias::find($aliasCount['id']); $aliasesForMeta[$a->alias] = ['id' => $a->id, 'alias' => $a->alias, 'count' => $aliasCount['count']]; } return $aliasesForMeta; }