Completed
Pull Request — master (#7)
by Asmir
32:22 queued 29:53
created

WhiteSpaceRemovalSubscriber   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 19
cbo 2
dl 0
loc 67
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A performXpathQuery() 0 8 2
B isAllowedNode() 0 12 5
A getSubscribedEvents() 0 6 1
D removeWhitespace() 0 27 10
1
<?php
2
namespace Goetas\TwitalBundle\EventSubscriber;
3
4
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
5
use Goetas\Twital\EventDispatcher\SourceEvent;
6
use Goetas\Twital\EventDispatcher\TemplateEvent;
7
use Goetas\Twital\Twital;
8
9
/**
10
 *
11
 * @author Asmir Mustafic <[email protected]>
12
 *
13
 */
14
class WhiteSpaceRemovalSubscriber implements EventSubscriberInterface
15
{
16
17
    protected $allowedTags = array();
18
19
    protected $allowedNamespaces = array();
20
21 23
    public function __construct(array $allowedTags = array(), array $allowedNamespaces = array())
22
    {
23 23
        $this->allowedTags = $allowedTags;
24 23
        $this->allowedNamespaces = $allowedNamespaces;
25 23
    }
26
27 18
    public static function getSubscribedEvents()
28
    {
29
        return array(
30
            'compiler.post_load' => array( 'removeWhitespace', - 10)
31 18
        );
32
    }
33 23
    private function performXpathQuery(\DOMXPath $xp, $query, \DOMNode $ref)
34
    {
35 23
        if (defined('HHVM_VERSION')) {
36
            return $xp->query($query, $ref);
37
        } else {
38 23
            return $xp->query($query, $ref, false);
39
        }
40
    }
41 23
    public function removeWhitespace(TemplateEvent $event)
42
    {
43 23
        $doc = $event->getTemplate()->getDocument();
44
45 23
        $xp = new \DOMXPath($doc);
46 23
        $xp->registerNamespace("t", Twital::NS);
47
48 23
        foreach ($this->performXpathQuery($xp, "//text()[ancestor::*[@t:trans or @t:trans-n]]", $doc) as $text) {
49 12
            if ($this->isAllowedNode($text->parentNode)) {
50 5
                $text->data = preg_replace('/\s+/', ' ', $text->data);
51
52 5
                if ($text->parentNode->childNodes->length === 1) {
53 5
                    $trimmed = trim($text->data);
54 5
                    if(!strlen($trimmed) && strlen($text->data)){
55 1
                        $trimmed =" ";
56
                    }
57 5
                    $text->data = $trimmed;
58 2
                } elseif ($text->parentNode->hasAttributeNs(Twital::NS, 'trans') || $text->parentNode->hasAttributeNs(Twital::NS, 'trans-n')) {
59 2
                    if ($text->parentNode->firstChild === $text) {
60 2
                        $text->data = ltrim($text->data);
61 2
                    }elseif ($text->parentNode->lastChild === $text) {
62 12
                        $text->data = rtrim($text->data);
63
                    }
64
                }
65
            }
66
        }
67 23
    }
68 12
    private function isAllowedNode(\DOMElement $element) {
69
70 12
        if ($this->allowedTags && !in_array($element->localName, $this->allowedTags)) {
71
        	return false;
72
        }
73
74 12
        if ($this->allowedNamespaces && !in_array($element->namespaceURI, $this->allowedNamespaces)) {
75 7
            return false;
76
        }
77
78 5
        return true;
79
    }
80
}
81