diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c664ab..7ba027f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Added - Tags in ActionSchema - Entity name in ActionSchema +## Changed +- Removed empty elements from transport writes ## [1.2.8] - 2017-11-07 ## Changed diff --git a/src/Mapper/CompactTransportMapper.php b/src/Mapper/CompactTransportMapper.php index 2c2db52..2f38392 100644 --- a/src/Mapper/CompactTransportMapper.php +++ b/src/Mapper/CompactTransportMapper.php @@ -88,13 +88,27 @@ public function writeTransport(Transport $transport) { $output = []; $output = $this->writeTransportMeta($transport->getMeta(), $output); - $output = $this->writeTransportFiles($transport->getFiles(), $output); - $output = $this->writeTransportData($transport->getData(), $output); - $output = $this->writeTransportRelations($transport->getRelations(), $output); - $output = $this->writeTransportLinks($transport->getLinks(), $output); - $output = $this->writeTransportCalls($transport->getCalls(), $output); - $output = $this->writeTransportTransactions($transport->getTransactions(), $output); - $output = $this->writeTransportErrors($transport->getErrors(), $output); + if ($transport->getFiles()->getAll()) { + $output = $this->writeTransportFiles($transport->getFiles(), $output); + } + if ($transport->getData()->get()) { + $output = $this->writeTransportData($transport->getData(), $output); + } + if ($transport->getRelations()->get()) { + $output = $this->writeTransportRelations($transport->getRelations(), $output); + } + if ($transport->getLinks()->get()) { + $output = $this->writeTransportLinks($transport->getLinks(), $output); + } + if ($transport->getCalls()->get()) { + $output = $this->writeTransportCalls($transport->getCalls(), $output); + } + if ($transport->getTransactions()->get()) { + $output = $this->writeTransportTransactions($transport->getTransactions(), $output); + } + if ($transport->getErrors()->get()) { + $output = $this->writeTransportErrors($transport->getErrors(), $output); + } if ($transport->hasBody()) { $output = $this->writeTransportBody($transport->getBody(), $output); } @@ -459,9 +473,6 @@ public function writeTransportTransactions(TransportTransactions $transactions, if ($transaction->getParams()) { $transactionData['p'] = array_map([$this, 'writeParam'], $transaction->getParams()); - } else { - // todo: remove when katana makes parameters optional - $transactionData['p'] = []; } $type = [ @@ -517,11 +528,17 @@ public function getTransportErrors(array $raw) public function writeTransportErrors(TransportErrors $errors, array $output) { foreach ($errors->get() as $error) { - $output['e'][$error->getAddress()][$error->getService()][$error->getVersion()][] = [ - 'm' => $error->getMessage(), - 'c' => $error->getCode(), - 's' => $error->getStatus(), - ]; + $errorData = []; + if ($error->getMessage()) { + $errorData['m'] = $error->getMessage(); + } + if ($error->getCode()) { + $errorData['c'] = $error->getCode(); + } + if ($error->getStatus()) { + $errorData['s'] = $error->getStatus(); + } + $output['e'][$error->getAddress()][$error->getService()][$error->getVersion()][] = $errorData; } return $output; diff --git a/src/Mapper/ExtendedTransportMapper.php b/src/Mapper/ExtendedTransportMapper.php index d3e42e5..854b060 100644 --- a/src/Mapper/ExtendedTransportMapper.php +++ b/src/Mapper/ExtendedTransportMapper.php @@ -88,13 +88,28 @@ public function writeTransport(Transport $transport) { $output = []; $output = $this->writeTransportMeta($transport->getMeta(), $output); - $output = $this->writeTransportFiles($transport->getFiles(), $output); - $output = $this->writeTransportData($transport->getData(), $output); - $output = $this->writeTransportRelations($transport->getRelations(), $output); - $output = $this->writeTransportLinks($transport->getLinks(), $output); - $output = $this->writeTransportCalls($transport->getCalls(), $output); - $output = $this->writeTransportTransactions($transport->getTransactions(), $output); - $output = $this->writeTransportErrors($transport->getErrors(), $output); + + if ($transport->getFiles()->getAll()) { + $output = $this->writeTransportFiles($transport->getFiles(), $output); + } + if ($transport->getData()->get()) { + $output = $this->writeTransportData($transport->getData(), $output); + } + if ($transport->getRelations()->get()) { + $output = $this->writeTransportRelations($transport->getRelations(), $output); + } + if ($transport->getLinks()->get()) { + $output = $this->writeTransportLinks($transport->getLinks(), $output); + } + if ($transport->getCalls()->get()) { + $output = $this->writeTransportCalls($transport->getCalls(), $output); + } + if ($transport->getTransactions()->get()) { + $output = $this->writeTransportTransactions($transport->getTransactions(), $output); + } + if ($transport->getErrors()->get()) { + $output = $this->writeTransportErrors($transport->getErrors(), $output); + } if ($transport->hasBody()) { $output = $this->writeTransportBody($transport->getBody(), $output); } @@ -439,9 +454,6 @@ public function writeTransportTransactions(TransportTransactions $transactions, if ($transaction->getParams()) { $transactionData['params'] = array_map([$this, 'writeParam'], $transaction->getParams()); - } else { - // todo: remove when katana makes parameters optional - $transactionData['params'] = []; } $type = $transaction->getType(); @@ -490,11 +502,17 @@ public function getTransportErrors(array $raw) public function writeTransportErrors(TransportErrors $errors, array $output) { foreach ($errors->get() as $error) { - $output['errors'][$error->getService()][$error->getVersion()][] = [ - 'message' => $error->getMessage(), - 'code' => $error->getCode(), - 'status' => $error->getStatus(), - ]; + $errorData = []; + if ($error->getMessage()) { + $errorData['message'] = $error->getMessage(); + } + if ($error->getCode()) { + $errorData['code'] = $error->getCode(); + } + if ($error->getStatus()) { + $errorData['status'] = $error->getStatus(); + } + $output['errors'][$error->getService()][$error->getVersion()][] = $errorData; } return $output;