1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoDraw\Draw\LoopHelper; |
12
|
|
|
|
13
|
|
|
use ArrayObject; |
14
|
|
|
use WebinoDraw\Exception\InvalidLoopHelperOptionException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
class ElementWrapper implements HelperInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
* @throws InvalidLoopHelperOptionException |
24
|
|
|
*/ |
25
|
|
|
public function __invoke(ArrayObject $loopArgument, array $options) |
26
|
|
|
{ |
27
|
|
|
if (empty($options['elementName'])) { |
28
|
|
|
throw new InvalidLoopHelperOptionException('elementName', $loopArgument['spec']); |
29
|
|
|
} |
30
|
|
|
if (!empty($options['elementAttribs']) && !is_array($options['elementAttribs'])) { |
31
|
|
|
throw new InvalidLoopHelperOptionException('elementAttribs', $loopArgument['spec']); |
32
|
|
|
} |
33
|
|
|
if (empty($options['each'])) { |
34
|
|
|
throw new InvalidLoopHelperOptionException('each', $loopArgument['spec']); |
35
|
|
|
} |
36
|
|
|
if (($loopArgument['index'] - 1) % $options['each']) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// create wrapper |
41
|
|
|
$newParentNode = $loopArgument['parentNode']->ownerDocument |
42
|
|
|
->createElement($options['elementName']); |
43
|
|
|
|
44
|
|
|
$parentNode = !empty($loopArgument['wrapperParentNode']) |
45
|
|
|
? $loopArgument['wrapperParentNode'] |
46
|
|
|
: $loopArgument['wrapperParentNode'] = $loopArgument['parentNode']; |
47
|
|
|
|
48
|
|
|
$beforeNode = !empty($loopArgument['wrapperBeforeNode']) |
49
|
|
|
? $loopArgument['wrapperBeforeNode'] |
50
|
|
|
: $loopArgument['wrapperBeforeNode'] = $loopArgument['beforeNode']; |
51
|
|
|
|
52
|
|
|
$loopArgument['beforeNode'] = null; |
53
|
|
|
$loopArgument['parentNode'] = $beforeNode |
54
|
|
|
? $parentNode->insertBefore($newParentNode, $beforeNode) |
55
|
|
|
: $parentNode->appendChild($newParentNode); |
56
|
|
|
|
57
|
|
|
empty($options['elementAttribs']) or |
58
|
|
|
$newParentNode->setAttributes($options['elementAttribs']); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|