/** * Display the specified Element. * GET|HEAD /elements/{id} * * @param Request $request * @param int $id * * @return Response */ public function show(Request $request, $id) { $element = $this->elementRepository->apiFindOrFail($id); $response = $this->response(); //todo: move this to middleware in order to handle content negotiation $format = Str::lower($request->get('format', 'json')); $item = $response->item($element, new ElementTransformer()); $resource = new Item($element, new ElementTransformer()); $properties = []; //make sure we're sending utf-8 //sendResponse always sends json if ('json' == $format) { foreach ($element->ElementProperties as $elementProperty) { if ($elementProperty->profileProperty->has_language) { $properties[$elementProperty->language][$elementProperty->ProfileProperty->name] = $elementProperty->object; } else { $properties[$elementProperty->ProfileProperty->name] = $elementProperty->object; } } $result = ['uri' => $element->uri, 'properties' => $properties]; return $this->sendResponse($result, "Element retrieved successfully"); } else { //todo: move this to a formatter foreach ($element->ElementProperties as $elementProperty) { $properties[$elementProperty->ProfileProperty->name . '.' . $elementProperty->language] = $elementProperty; } ksort($properties, SORT_NATURAL | SORT_FLAG_CASE); //build the xml $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><response/>'); $xml->registerXPathNamespace('xml', 'http://www.w3.org/XML/1998/namespace'); $data = $xml->addChild('data'); $data->addAttribute('uri', $element->uri); $status = $data->addChild('status', $element->Status->display_name); $status->addAttribute('uri', $element->Status->uri); foreach ($properties as $elementProperty) { $key = $elementProperty->ProfileProperty->name; if (in_array($key, ['statusId', 'uri'])) { continue; } if ($elementProperty->profileProperty->has_language) { $property = $data->addChild($key, $elementProperty->object); $property->addAttribute('xml:lang', $elementProperty->language, 'xml'); } else { try { $relatedElement = $this->elementRepository->apiFindOrFail($elementProperty->related_schema_property_id); } catch (HttpException $e) { } $relatedProperties = []; foreach ($relatedElement->ElementProperties as $relElementProperty) { $relatedProperties[$relElementProperty->ProfileProperty->name . '.' . $relElementProperty->language] = $relElementProperty; } //todo: set the label language based on language request $relKey = array_key_exists('label.en', $relatedProperties) ? 'label.en' : 'label.'; //todo: Fix this hack if ('type' == $key) { $property = $data->addChild($key, str::studly($elementProperty->object)); continue; } $property = $data->addChild($key, $relatedProperties[$relKey]->object); $property->addAttribute('uri', $elementProperty->object); } } $message = $xml->addChild('message', "Element retrieved successfully"); return Response::make($xml->asXML(), 200)->header('Content-Type', 'application/xml'); } }