Skip to content

Commit

Permalink
Added declaration option to Xml::fromArray
Browse files Browse the repository at this point in the history
  • Loading branch information
jamielsharief committed Jun 2, 2021
1 parent b31b58c commit 8ff43e2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2021-06-02

### Added

- Added declaration option to Xml::fromArray

## [2.0.0] - 2021-01-03

### Changed
Expand All @@ -30,4 +36,4 @@ No major changes.

## [1.0.0] - 2019-10-12

This component has been decoupled from the [OriginPHP framework](https://www.originphp.com/).
This component has been decoupled from the [OriginPHP framework](https://www.originphp.com/).
8 changes: 8 additions & 0 deletions src/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Xml
* - version: default: 1.0
* - encoding: default: UTF-8
* - pretty: default false. Formats XML nicely
* - declaration: default:true all XML documents should start with this, but if you need to turn it off
* @return string
*/
public static function fromArray(array $data, array $options = []): string
Expand All @@ -65,6 +66,7 @@ public static function fromArray(array $data, array $options = []): string
'version' => '1.0',
'encoding' => 'UTF-8',
'pretty' => false,
'declaration' => true
];
$options += $defaults;

Expand All @@ -76,6 +78,12 @@ public static function fromArray(array $data, array $options = []): string

static::convertArray($dom, $dom, $data);

if ($options['declaration'] === false) {
$tmp = new DOMDocument();
$tmp->loadXML($dom->saveXML());
return $tmp->saveXML($tmp->documentElement);
}

return $dom->saveXML();
}

Expand Down
32 changes: 32 additions & 0 deletions tests/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ public function testFromArray()
$this->assertStringContainsString($needle, Xml::fromArray($data));
}

public function testFromArrayDeclaration()
{
$data = [
'post' => [
'@category' => 'how tos', // to set attribute use @
'id' => 12345,
'title' => 'How to create an XML block',
'body' => Xml::cdata('A quick brown fox jumps of a lazy dog.'),
'author' => [
'name' => 'James',
],
],
];

$expected = '<post category="how tos"><id>12345</id><title>How to create an XML block</title><body>&lt;![CDATA["A quick brown fox jumps of a lazy dog."]]&gt;</body><author><name>James</name></author></post>';
$this->assertEquals($expected, Xml::fromArray($data, ['declaration'=>false]));

// Check pretty still works
$expected= <<<EOT
<post category="how tos">
<id>12345</id>
<title>How to create an XML block</title>
<body>&lt;![CDATA["A quick brown fox jumps of a lazy dog."]]&gt;</body>
<author>
<name>James</name>
</author>
</post>
EOT;

$this->assertEquals($expected, Xml::fromArray($data, ['declaration'=>false,'pretty'=>true]));
}

/**
* @depends testFromArray
*/
Expand Down

0 comments on commit 8ff43e2

Please sign in to comment.