Completed
Push — master ( aafbda...c2b43f )
by Jan
04:12
created

AppExtension::siFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * part-db version 0.1
4
 * Copyright (C) 2005 Christoph Lechner
5
 * http://www.cl-projects.de/.
6
 *
7
 * part-db version 0.2+
8
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
9
 * http://code.google.com/p/part-db/
10
 *
11
 * Part-DB Version 0.4+
12
 * Copyright (C) 2016 - 2019 Jan Böhmer
13
 * https://github.com/jbtronics
14
 *
15
 * This program is free software; you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License
17
 * as published by the Free Software Foundation; either version 2
18
 * of the License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
28
 */
29
30
namespace App\Twig;
31
32
use App\Entity\Attachments\Attachment;
33
use App\Entity\Base\DBElement;
34
use App\Services\EntityURLGenerator;
35
use App\Services\MoneyFormatter;
36
use App\Services\SIFormatter;
37
use App\Services\TreeBuilder;
38
use Symfony\Component\Cache\Adapter\AdapterInterface;
39
use Symfony\Component\Serializer\SerializerInterface;
40
use Twig\Extension\AbstractExtension;
41
use Twig\TwigFilter;
42
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
43
use Twig\TwigFunction;
44
use Twig\TwigTest;
45
46
class AppExtension extends AbstractExtension
47
{
48
    protected $entityURLGenerator;
49
    protected $cache;
50
    protected $serializer;
51
    protected $treeBuilder;
52
    protected $moneyFormatter;
53
    protected $siformatter;
54
55
    public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
56
                                SerializerInterface $serializer, TreeBuilder $treeBuilder,
57
                                MoneyFormatter $moneyFormatter,
58
                                SIFormatter $SIFormatter)
59
    {
60
        $this->entityURLGenerator = $entityURLGenerator;
61
        $this->cache = $cache;
62
        $this->serializer = $serializer;
63
        $this->treeBuilder = $treeBuilder;
64
        $this->moneyFormatter = $moneyFormatter;
65
        $this->siformatter = $SIFormatter;
66
    }
67
68
    public function getFilters()
69
    {
70
        return [
71
            new TwigFilter('entityURL', [$this, 'generateEntityURL']),
72
            new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
73
            new TwigFilter('moneyFormat', [$this, 'formatCurrency']),
74
            new TwigFilter('siFormat', [$this, 'siFormat']),
75
        ];
76
    }
77
78
    public function getTests()
79
    {
80
        return [
81
            new TwigTest('instanceof', function ($var, $instance) {
82
                return $var instanceof $instance;
83
            } )
84
        ];
85
    }
86
87
    public function getFunctions()
88
    {
89
        return [
90
            new TwigFunction('generateTreeData', [$this, 'treeData'])
91
        ];
92
    }
93
94
    public function treeData(DBElement $element, string $type = 'newEdit') : string
95
    {
96
        $tree = $this->treeBuilder->typeToTree(get_class($element), $type, $element);
97
        return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]);
98
    }
99
100
    public function generateEntityURL(DBElement $entity, string $method = 'info'): string
101
    {
102
        return $this->entityURLGenerator->getURL($entity, $method);
103
    }
104
105
    public function parseBBCode(string $bbcode): string
106
    {
107
        if ('' === $bbcode) {
108
            return '';
109
        }
110
111
        $item = $this->cache->getItem('bbcode_'.md5($bbcode));
112
        if (!$item->isHit()) {
113
            $xml = TextFormatter::parse($bbcode);
114
            $item->set(TextFormatter::render($xml));
115
            $this->cache->save($item);
116
        }
117
118
        return $item->get();
119
    }
120
121
    public function formatCurrency($amount, $currency = "")
122
    {
123
        return $this->moneyFormatter->format($amount, $currency);
124
    }
125
126
    public function siFormat($value, $unit, $decimals = 2)
127
    {
128
        return $this->siformatter->format($value, $unit, $decimals);
129
    }
130
}
131