-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXmlRepeater.php
executable file
·41 lines (36 loc) · 1.21 KB
/
XmlRepeater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace SymfonyXmlResponse\Responses;
class XmlRepeater implements XmlDecoratorInterface
{
protected $placeholder;
protected $repeatingElement;
protected $repeatingData;
/**
* XmlRepeater constructor.
* @param string $placeholder marker to look for in the content and replace with new (repeating) content
* @param string $repeatingElement name of the XML element that repeats
* @param array $repeatingData indexed array of repeating data
*/
public function __construct($placeholder, $repeatingElement, array $repeatingData)
{
$this->placeholder = $placeholder;
$this->repeatingElement = $repeatingElement;
$this->repeatingData = $repeatingData;
}
/**
* Accepts the current content, makes prescribed modifications and
* returns the modified content.
*
* @param string $content
* @return string
*/
public function run($content)
{
$xml = array();
$xr = new XmlResponse();
foreach ($this->repeatingData as $item) {
$xml[] = $xr->getFragment($this->repeatingElement, $item);
}
return str_replace($this->placeholder, implode("\n", $xml), $content);
}
}