Skip to content

Commit 518560e

Browse files
📝 Add docstrings to fix-update-in-pohoda
Docstrings generation was requested by @Vitexus. * #19 (comment) The following files were modified: * `src/mServer/Client.php` * `src/mServer/Response.php`
1 parent f3d011d commit 518560e

File tree

2 files changed

+95
-13
lines changed

2 files changed

+95
-13
lines changed

src/mServer/Client.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,10 @@ public function setAuth(): bool
322322
}
323323

324324
/**
325-
* Set Instance http header.
325+
* Set the STW-Instance HTTP header for subsequent requests.
326+
*
327+
* @param string $instance Identifier of the mServer instance to send in the `STW-Instance` header.
328+
* @return self Fluent interface.
326329
*/
327330
public function setInstance(string $instance): self
328331
{
@@ -331,6 +334,15 @@ public function setInstance(string $instance): self
331334
return $this;
332335
}
333336

337+
/**
338+
* Set an external identifier in the payload under identity->extId->ids.
339+
*
340+
* Stores the provided external ID into the internal data structure so it
341+
* will be included in the next Pohoda request.
342+
*
343+
* @param string $extId External identifier to assign.
344+
* @return self Fluent interface.
345+
*/
334346
public function setExtId(string $extId): self
335347
{
336348
$this->data['identity']['extId']['ids'] = $extId;
@@ -666,6 +678,16 @@ public function addToPohoda($data = []): self
666678
return $this;
667679
}
668680

681+
/**
682+
* Finalizes the current Pohoda XML payload and sends it to the server.
683+
*
684+
* Closes the current Pohoda document, loads the generated XML from the temporary
685+
* cache into the next request body, and performs an HTTP POST to the server's
686+
* "/xml" endpoint. In debug mode, adds a status message suggesting an xmllint
687+
* validation command for the cached XML.
688+
*
689+
* @return bool True when the server response indicates success or warning; false otherwise.
690+
*/
669691
public function commit(): bool
670692
{
671693
$this->pohoda->close();
@@ -679,10 +701,15 @@ public function commit(): bool
679701
}
680702

681703
/**
682-
* Update prepared record in Pohoda.
704+
* Prepare and queue an update for the current agenda object in Pohoda.
705+
*
706+
* If $data is provided, it is taken as the current record (via takeData). If a request XML is present,
707+
* the method marks it with an "update" action (using the provided $filter or filterToMe() when $filter is empty)
708+
* and adds the item to the Pohoda engine for later commit.
683709
*
684-
* @param array<string, string> $data extra data
685-
* @param null|mixed $filter
710+
* @param array<string,mixed> $data Data to prepare as the current record (optional).
711+
* @param mixed $filter Filter to apply for the update; when empty, filterToMe() is used.
712+
* @return $this
686713
*/
687714
public function updateInPohoda(array $data = [], $filter = null): self
688715
{

src/mServer/Response.php

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,26 @@ public function __construct(Client $caller)
7878
$this->useCaller($caller);
7979
}
8080

81+
/**
82+
* Associate a Caller with this Response and initialize state from its last HTTP response.
83+
*
84+
* If the caller has a lastCurlResponse, stores it in $rawXML, attempts to load it as XML,
85+
* reads the root `rsp` attributes `state` and `note`, and processes each child `rsp` element
86+
* by delegating to processResponsePackItem().
87+
*
88+
* If there is no lastCurlResponse, sets the response state to "error" and the note to the
89+
* caller's lastCurlError.
90+
*/
8191
public function useCaller(Client $caller): void
8292
{
8393
$this->caller = $caller;
8494
if ($caller->lastCurlResponse) {
8595
$this->rawXML = $caller->lastCurlResponse;
86-
$xml = simplexml_load_string($this->rawXML, 'SimpleXMLElement', \LIBXML_NONET | \LIBXML_NOCDATA);
96+
$xml = simplexml_load_string($this->rawXML);
8797
if ($xml) {
88-
$attrsRsp = $xml->attributes('rsp', true);
89-
$attrs = $xml->attributes();
90-
$this->state = (string) (($attrsRsp['state'] ?? $attrs['state'] ?? '') );
91-
$this->note = (string) (($attrsRsp['note'] ?? $attrs['note'] ?? '') );
92-
$children = iterator_to_array($xml->children('rsp', true) ?: $xml->children());
93-
foreach ($children as $responsePackItem) {
98+
$this->state = (string) $xml->attributes('rsp', true)->state;
99+
$this->note = (string) $xml->attributes('rsp', true)->note;
100+
foreach ($xml->children('rsp', true) as $responsePackItem) {
94101
$this->processResponsePackItem($responsePackItem);
95102
}
96103
}
@@ -100,11 +107,28 @@ public function useCaller(Client $caller): void
100107
}
101108
}
102109

110+
/**
111+
* No-op placeholder for processing a responsePack XML element; retained for compatibility.
112+
*
113+
* This method intentionally does nothing and is deprecated — response pack items are processed
114+
* via processResponsePackItem() when XML is loaded.
115+
*
116+
* @param \SimpleXMLElement $responsePackData The <responsePack> XML element (ignored).
117+
* @deprecated Use processResponsePackItem(\SimpleXMLElement $responsePackItem) or let useCaller() handle response pack processing.
118+
*/
103119
public function processResponsePack(\SimpleXMLElement $responsePackData): void
104120
{
105121
// This method is no longer used
106122
}
107123

124+
/**
125+
* Process a single <responsePackItem> XML node: update the overall state and handle its child response nodes.
126+
*
127+
* Reads the node's "state" attribute into the instance state and delegates each child element to
128+
* processResponseData(), which may populate parsed data, producedDetails, and messages.
129+
*
130+
* @param \SimpleXMLElement $responsePackItem XML element representing one responsePackItem.
131+
*/
108132
public function processResponsePackItem(\SimpleXMLElement $responsePackItem): void
109133
{
110134
$this->state = (string) $responsePackItem->attributes()->state;
@@ -113,12 +137,36 @@ public function processResponsePackItem(\SimpleXMLElement $responsePackItem): vo
113137
}
114138
}
115139

140+
/**
141+
* Extracts produced detail ID from an `rdc` namespaced XML node and stores it.
142+
*
143+
* Reads the `<rdc:id>` child of the provided SimpleXMLElement, casts it to int,
144+
* sets $this->producedDetails to ['id' => (int)], and synchronizes $this->parsed
145+
* with the same value.
146+
*
147+
* @param \SimpleXMLElement $productDetails XML node containing an `rdc:id` child (uses the `rdc` namespace)
148+
*/
116149
public function processProducedDetails(\SimpleXMLElement $productDetails): void
117150
{
118151
$this->producedDetails = ['id' => (int) $productDetails->children('rdc', true)->id];
119152
$this->parsed = $this->producedDetails;
120153
}
121154

155+
/**
156+
* Process import-detail nodes from a Pohoda response and record their messages.
157+
*
158+
* Iterates over children in the `rdc` namespace of the provided import details element,
159+
* collects each detail's child elements into an associative array (element name => string value),
160+
* groups those arrays into $this->messages keyed by each detail's `state` value, and updates
161+
* the response state and parsed representation accordingly.
162+
*
163+
* Side effects:
164+
* - Appends parsed detail arrays into $this->messages (keys like 'error' or 'warning').
165+
* - Sets $this->state to 'error' if any error messages exist, otherwise to 'warning' if any warnings exist.
166+
* - Updates $this->parsed to reference the messages array.
167+
*
168+
* @param \SimpleXMLElement $importDetails XML element containing import detail entries (expects children in the `rdc` namespace)
169+
*/
122170
public function processImportDetails(\SimpleXMLElement $importDetails): void
123171
{
124172
foreach ($importDetails->children('rdc', true) as $detail) {
@@ -138,8 +186,15 @@ public function processImportDetails(\SimpleXMLElement $importDetails): void
138186
}
139187

140188
/**
141-
* @param \SimpleXMLElement $responseData
142-
*/
189+
* Process a single <responseData> XML node.
190+
*
191+
* Updates the object's overall state from the node's `state` attribute and, when present,
192+
* delegates processing of child elements `producedDetails` and `importDetails` to
193+
* processProducedDetails() and processImportDetails() respectively. Those handlers
194+
* update internal parsed data, messages, and producedDetails.
195+
*
196+
* @param \SimpleXMLElement $responseData The <responseData> element to process.
197+
*/
143198
public function processResponseData(\SimpleXMLElement $responseData): void
144199
{
145200
$this->state = (string) $responseData->attributes()->state;

0 commit comments

Comments
 (0)