Skip to content

Commit

Permalink
Fix add attributes feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Edujugon committed Apr 23, 2018
1 parent 4ef0319 commit c9e7c7c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,12 @@ $mapper->mergeXML($newXml, 'desiredParentTag');
$mapper->wrapWith('tagName');
```

#### Add attributes based on tagName
#### Add attributes

```php
$mapper-addAttributes('tagName', ['attr1' => 'value1', 'attr2' => 'value2');
$mapper-addAttributes(['attr1' => 'value1', 'attr2' => 'value2']);
or
$mapper-addAttributes(['attr1' => 'value1', 'attr2' => 'value2'], 'tagName');
```

Enjoy :)
6 changes: 3 additions & 3 deletions src/XMLMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,16 +455,16 @@ public function wrapWith($tagName)
}

/**
* Add attributes to the xml based on tagName
* Add attributes to the xml
*
* @param $tagName
* @param array $attributes
*/
public function addAttributes($tagName, array $attributes)
public function addAttributes(array $attributes, $tagName = null)
{
$obj = $this->getObj();

if ($tagName === $obj->getName()) {
if (is_null($tagName) || $tagName === $obj->getName()) {
$this->addAttributesToObj($obj, $attributes);

return;
Expand Down
4 changes: 2 additions & 2 deletions tests/XMLMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function add_attributes_to_parent_node()

$this->assertNull($this->mapper->getAttribute('test'));

$this->mapper->addAttributes('bookstore', [ 'test' => 'found', 'test2' => 'found']);
$this->mapper->addAttributes([ 'test' => 'found', 'test2' => 'found']);

$this->assertEquals('found', $this->mapper->getAttribute('test'));
$this->assertEquals('found', $this->mapper->getAttribute('test2'));
Expand All @@ -347,7 +347,7 @@ public function add_attributes_to_direct_child_node()

$this->assertNull($this->mapper->getElement('book')->getAttribute('test'));

$this->mapper->addAttributes('book', [ 'test' => 'found', 'test2' => 'found']);
$this->mapper->addAttributes([ 'test' => 'found', 'test2' => 'found'], 'book');

$this->assertEquals('found', $this->mapper->getElement('book')->getAttribute('test'));
$this->assertEquals('found', $this->mapper->getElement('book')->getAttribute('test2'));
Expand Down

0 comments on commit c9e7c7c

Please sign in to comment.