$attributes пары KEY → VALUE для `MNT_ATTRIBUTES` * @throws \DOMException */ public static function build( string $accountId, string $transactionId, ResultCode $resultCode, Money $amount, string $signature, string $cms, array $attributes = [], ): string { $document = new DOMDocument('1.0', 'UTF-8'); $response = $document->appendChild($document->createElement('MNT_RESPONSE')); $elements = [ 'MNT_ID' => $accountId, 'MNT_TRANSACTION_ID' => $transactionId, 'MNT_RESULT_CODE' => (string) $resultCode->value, 'MNT_DESCRIPTION' => $resultCode->description(), 'MNT_AMOUNT' => $amount->toDecimal(), 'MNT_SIGNATURE' => $signature, 'MNT_CMS' => $cms, ]; foreach ($elements as $name => $value) { self::text($document, $response, $name, $value); } if ($attributes !== []) { $container = $response->appendChild($document->createElement('MNT_ATTRIBUTES')); foreach ($attributes as $key => $value) { $attribute = $container->appendChild($document->createElement('ATTRIBUTE')); self::text($document, $attribute, 'KEY', $key); self::text($document, $attribute, 'VALUE', $value); } } $xml = $document->saveXML(); if ($xml === false) { throw new \RuntimeException('Не удалось сериализовать MNT_RESPONSE.'); } return $xml; } /** * Текстовый узел, а не `createElement($name, $value)`: второй не экранирует `&`. */ private static function text(DOMDocument $document, DOMNode $parent, string $name, string $value): void { $parent->appendChild($document->createElement($name))->appendChild($document->createTextNode($value)); } }