Completed
Push — 5.3 ( d18aef...16c32d )
by Jeroen
15:04 queued 08:59
created

AdminBundle/Form/MediaTokenTransformer.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Form;
4
5
use Symfony\Component\DomCrawler\Crawler;
6
use Symfony\Component\Form\DataTransformerInterface;
7
8
/**
9
 * Class MediaTokenTransformer.
10
 */
11
class MediaTokenTransformer implements DataTransformerInterface
12
{
13
    /**
14
     * @param mixed $content
15
     *
16
     * @return string
17
     */
18 2
    public function transform($content)
19
    {
20 2
        if (!trim($content)) {
21 1
            return '';
22
        }
23
24 1
        $crawler = new Crawler();
25 1
        $crawler->addHtmlContent($content);
26
27 1
        $crawler->filter('img')->each(
28
            function (Crawler $node) {
29 1
                $image = $node->getNode(0);
30 1
                if ($image->hasAttribute('data-src')) {
0 ignored issues
show
The method hasAttribute() does not exist on DOMNode. Did you maybe mean hasAttributes()?

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...
31 1
                    $src = $image->getAttribute('data-src');
32 1
                    $image->setAttribute('src', $src);
33 1
                    $image->removeAttribute('data-src');
34
                }
35 1
            }
36
        );
37
38
        try {
39 1
            return $crawler->html();
40
        } catch (\InvalidArgumentException $exception) {
41
            return $content;
42
        }
43
    }
44
45
    /**
46
     * @param mixed $content
47
     *
48
     * @return string
49
     */
50 2
    public function reverseTransform($content)
51
    {
52 2
        if (!trim($content)) {
53 1
            return '';
54
        }
55
56 2
        $crawler = new Crawler();
57 2
        $crawler->addHtmlContent($content);
58
59
        // Get all img tags and parse the token.
60 2
        $crawler->filter('img')->each(
61
            function (Crawler $node) {
62 1
                $image = $node->getNode(0);
63 1
                $src = $image->getAttribute('src');
64 1
                $parsed = parse_url($src, PHP_URL_QUERY);
65 1
                parse_str($parsed, $query);
66
67 1
                if (isset($query['token'])) {
68
                    $image->setAttribute('src', $query['token']);
69
                }
70 1
                $image->setAttribute('data-src', $src);
71 2
            }
72
        );
73
74
        try {
75 2
            $html = $crawler->filter('body')->html();
76
77
            // URL-decode square brackets in img and a tags
78 2
            $html = preg_replace_callback(
79 2
                '/<(img|a)\s+[^>]*>/',
80
                function ($matches) {
81 1
                    return str_replace(['%5B', '%5D'], ['[', ']'], $matches[0]);
82 2
                },
83 2
                $html
84
            );
85
86 2
            return $html;
87
        } catch (\InvalidArgumentException $exception) {
88
            return $content;
89
        }
90
    }
91
}
92