Completed
Push — master ( 505018...16cf89 )
by D.
13s
created

ImageCollector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 64
wmc 5
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setContent() 0 6 1
A collect() 0 15 3
A getCollectedData() 0 4 1
1
<?php
2
/**
3
 * This file is part of sitemap-common.
4
 *
5
 * (c) 2016 Daniele Moraschi
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SiteMap\Collect;
12
13
14
use SiteMap\Http\Url;
15
use SiteMap\Http\UrlUtil;
16
17
class ImageCollector implements Collector
18
{
19
20
    /**
21
     * @var string  REGEX
22
     */
23
    const REGEX = "/(img|src)=(\"|')[^\"'>]+\.(gif|jpg|jpeg|png|tif|svg)/i";
24
    const REGEX2 = "/(img|src)(\"|'|=\"|=')(.*)/i";
25
26
    /**
27
     * @var Url
28
     */
29
    private $url;
30
31
    /**
32
     * @var string
33
     */
34
    private $content;
35
36
    /**
37
     * @var array
38
     */
39
    private $data = [];
40
41
    /**
42
     * @param Url $url
43
     * @param mixed $content
44
     * @return $this
45
     */
46
    public function setContent(Url $url, $content)
47
    {
48
        $this->url = $url;
49
        $this->content = (string) $content;
50
        return $this;
51
    }
52
53
    /**
54
     * @return $this
55
     */
56
    public function collect()
57
    {
58
        if(! isset($this->data[$this->url->getWebUrl()])) {
59
            $this->data[$this->url->getWebUrl()] = [];
60
        }
61
62
        preg_match_all(self::REGEX, $this->content, $media);
63
        $data = preg_replace(self::REGEX2, "$3", $media[0]);
64
        foreach($data as $url) {
65
            $this->data[$this->url->getWebUrl()][] =
66
                UrlUtil::getAbsoluteLink($this->url, $url);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getCollectedData()
76
    {
77
        return $this->data;
78
    }
79
80
}