Completed
Branch dbal-improvement (06db1a)
by Anton
03:59
created

AttributesExporter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B mountBlocks() 0 28 6
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Stempler\Exporters;
9
10
use Spiral\Stempler\ConditionalExporter;
11
12
/**
13
 * Export user defined (outer) blocks as tag attributes.
14
 *
15
 * Use following pattern: node:attributes[="condition"]
16
 */
17
class AttributesExporter extends ConditionalExporter
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function mountBlocks($content, array $blocks)
23
    {
24
        if (preg_match_all('/ node:attributes(?:=\"([^\'"]+)\")?/i', $content, $matches)) {
25
            //We have to sort from longest to shortest
26
            uasort($matches[0], function ($replaceA, $replaceB) {
27
                return strlen($replaceB) - strlen($replaceA);
28
            });
29
30
            foreach ($matches[0] as $id => $replace) {
31
                $inject = [];
32
33
                //That's why we need longest first (prefix mode)
34
                foreach ($this->filterBlocks($matches[1][$id], $blocks) as $name => $value) {
35
                    if ($value === null) {
36
                        $inject[$name] = $name;
37
                        continue;
38
                    }
39
40
                    $inject[$name] = $name . '="' . $value . '"';
41
                }
42
43
                //Injecting
44
                $content = str_replace($replace, $inject ? ' ' . join(' ', $inject) : '', $content);
45
            }
46
        }
47
48
        return $content;
49
    }
50
}