Completed
Push — master ( 67c65c...9a618a )
by WEBEWEB
06:47
created

Base64ImageTwigExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A bootstrapBase64ImageFunction() 0 14 2
A getFunctions() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Twig\Extension\Image;
13
14
use SplFileObject;
15
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
16
use Twig_SimpleFunction;
17
use WBW\Library\Core\Utility\ArrayUtility;
18
19
/**
20
 * Base64 image Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Image
24
 * @final
25
 */
26
final class Base64ImageTwigExtension extends AbstractImageTwigExtension {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.image.base64";
34
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct() {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Displays a Bootstrap base 64 image.
44
     *
45
     * @param array $args The arguments.
46
     * @return string Returns the Bootstrap base 64 image.
47
     */
48
    public function bootstrapBase64ImageFunction(array $args = []) {
49
50
        // Initialize the URI.
51
        $uri = ArrayUtility::get($args, "src");
52
53
        // Initialize the src.
54
        $src = [];
55
        if (null !== $uri) {
56
            $src = (new DataUriNormalizer())->normalize(new SplFileObject($uri));
57
        }
58
59
        // Return the Bootstrap image.
60
        return $this->bootstrapImage($src, ArrayUtility::get($args, "alt"), ArrayUtility::get($args, "width"), ArrayUtility::get($args, "height"), ArrayUtility::get($args, "class"));
61
    }
62
63
    /**
64
     * Get the Twig functions.
65
     *
66
     * @return array Returns the Twig functions.
67
     */
68
    public function getFunctions() {
69
        return [
70
            new Twig_SimpleFunction("bootstrapBase64Image", [$this, "bootstrapBase64ImageFunction"], ["is_safe" => ["html"]]),
71
        ];
72
    }
73
74
}
75