public function iso3166Action() { if ($this->getRequest()->isGet()) { // get $countryTable = new CountryTable($this->getServiceLocator()->get('country-table-gateway')); try { $data = $countryTable->getCountryByISO3166($this->params()->fromRoute('iso3166')); $this->data = new Country(); $this->data->exchangeArray($data); } catch (\Exception $e) { if ($e->getCode() === 0) { // row not found $this->code = Response::STATUS_CODE_404; } else { if ($e->getCode() === 1) { // wrong finder (wrong request) $this->code = Response::STATUS_CODE_405; } else { // undefined error $this->code = Response::STATUS_CODE_400; } } } } else { if ($this->getRequest()->isPatch()) { // patch // check if PATCH message is a JSON object if ($this->getRequest()->getHeader('Content-Type')->getFieldValue() === "application/json") { $countryTable = new CountryTable($this->getServiceLocator()->get('country-table-gateway')); $newCountry = new Country(); $newCountry->exchangeArray(json_decode($this->getRequest()->getContent())); try { $countryTable->saveCountry($newCountry); } catch (\Exception $e) { $this->code = Response::STATUS_CODE_404; // ID not found } } else { $this->code = Response::STATUS_CODE_406; // Only accept JSON object } } else { if ($this->getRequest()->isDelete()) { // delete $countryTable = new CountryTable($this->getServiceLocator()->get('country-table-gateway')); try { $countryTable->deleteCountry($this->params()->fromRoute('iso3166')); } catch (\Exception $e) { $this->code = Response::STATUS_CODE_404; // iso3166 not found } } else { if ($this->getRequest()->isPut()) { // put -> forbidden $this->code = Response::STATUS_CODE_403; } else { $this->code = Response::STATUS_CODE_405; // 405 } } } } return $this->parseResponse(); }
protected function createCountry(array $data) { $country = new CountryModel(); $country->exchangeArray($data); $countryTable = $this->getServiceLocator()->get('country-table'); try { $countryTable->saveCountry($country); return true; } catch (\Exception $e) { return false; } }