Completed
Pull Request — master (#333)
by Elan
01:18
created

XHGuiTwigExtension::formatDiff()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace XHGui\Twig;
4
5
use Slim\App;
6
use Slim\Http\Uri;
7
use Slim\Router;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFilter;
10
use Twig\TwigFunction;
11
12
class XHGuiTwigExtension extends AbstractExtension
13
{
14
    /** @var App */
15
    protected $_app;
16
    /** @var Router */
17
    private $router;
18
    /** @var string */
19
    private $pathPrefix;
20
    /** @var Uri */
21
    private $uri;
22
23
    public function __construct(Router $router, Uri $uri)
24
    {
25
        $this->router = $router;
26
        $this->uri = $uri;
27
        $this->pathPrefix = '/'; // null;//$app->config('path.prefix');
28
    }
29
30
    public function getFunctions(): array
31
    {
32
        $options = [
33
            'is_safe' => ['html'],
34
        ];
35
36
        return [
37
            new TwigFunction('url', [$this, 'url']),
38
            new TwigFunction('static', [$this, 'staticUrl']),
39
            new TwigFunction('percent', [$this, 'makePercent'], $options),
40
        ];
41
    }
42
43
    public function getFilters(): array
44
    {
45
        $options = [
46
            'is_safe' => ['html'],
47
        ];
48
49
        return [
50
            new TwigFilter('as_bytes', [$this, 'formatBytes'], $options),
51
            new TwigFilter('as_time', [$this, 'formatTime'], $options),
52
            new TwigFilter('as_diff', [$this, 'formatDiff'], $options),
53
            new TwigFilter('as_percent', [$this, 'formatPercent'], $options),
54
            new TwigFilter('truncate', [$this, 'truncate']),
55
        ];
56
    }
57
58
    public function truncate(string $input, int $length = 50): string
59
    {
60
        if (strlen($input) < $length) {
61
            return $input;
62
        }
63
64
        return substr($input, 0, $length) . "\xe2\x80\xa6";
65
    }
66
67
    /**
68
     * Get a URL for xhgui.
69
     *
70
     * @param string $name The file/path you want a link to
71
     * @param array|null $queryargs additional querystring arguments
72
     * @return string url
73
     */
74
    public function url(string $name, $queryargs = []): string
75
    {
76
        $query = '';
77
        if ($queryargs) {
78
            $query = '?' . http_build_query($queryargs);
79
        }
80
81
        // this is copy of \Slim\Slim::urlFor() to mix path prefix in
82
        // \Slim\Slim::urlFor
83
84
        return rtrim($this->pathPrefix(), '/') . $this->router->urlFor($name) . $query;
85
    }
86
87
    /**
88
     * Get the URL for static content relative to webroot
89
     *
90
     * @param string $path The file/path you want a link to
91
     * @return string url
92
     */
93
    public function staticUrl(string $path): string
94
    {
95
        $rootUri = $this->pathPrefix();
96
97
        return rtrim($rootUri, '/') . '/' . $path;
98
    }
99
100
    public function formatBytes($value): string
101
    {
102
        return number_format((float)$value) . '&nbsp;<span class="units">bytes</span>';
103
    }
104
105
    public function formatTime($value): string
106
    {
107
        return number_format((float)$value) . '&nbsp;<span class="units">µs</span>';
108
    }
109
110
    public function formatDiff($value): string
111
    {
112
        $class = $value > 0 ? 'diff-up' : 'diff-down';
113
        if ($value == 0) {
114
            $class = 'diff-same';
115
        }
116
117
        return sprintf(
118
            '<span class="%s">%s</span>',
119
            $class,
120
            number_format((float)$value)
121
        );
122
    }
123
124
    public function makePercent($value, $total): string
125
    {
126
        $value = (false === empty($total)) ? $value / $total : 0;
127
128
        return $this->formatPercent($value);
129
    }
130
131
    public function formatPercent($value): string
132
    {
133
        return number_format((float)$value * 100, 0) . ' <span class="units">%</span>';
134
    }
135
136
    private function pathPrefix(): string
137
    {
138
        if ($this->pathPrefix !== null) {
139
            return $this->pathPrefix;
140
        }
141
142
        $request = $this->_app->request();
0 ignored issues
show
Bug introduced by
The method request() does not exist on Slim\App. Did you maybe mean isHeadRequest()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
143
        $rootUri = $request->getRootUri();
144
145
        // Get URL part prepending index.php
146
        $indexPos = strpos($rootUri, 'index.php');
147
        if ($indexPos > 0) {
148
            return substr($rootUri, 0, $indexPos);
149
        }
150
151
        return $rootUri;
152
    }
153
}
154