Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00%
1 / 1
100.00%
10 / 10
CRAP
100.00%
49 / 49
ImageProcessor
100.00%
1 / 1
100.00%
10 / 10
22
100.00%
49 / 49
 setIgnoredPatterns(array $ignoredPatterns)
100.00%
1 / 1
2
100.00%
5 / 5
 addIgnoredPattern($ignoredPattern)
100.00%
1 / 1
1
100.00%
2 / 2
 getIgnoredPatterns()
100.00%
1 / 1
1
100.00%
1 / 1
 setOverrideImage($overrideImage)
100.00%
1 / 1
1
100.00%
2 / 2
 process(array $items)
100.00%
1 / 1
2
100.00%
4 / 4
 setImage(Item $item)
100.00%
1 / 1
3
100.00%
4 / 4
 setImageFromContent(Item $item)
100.00%
1 / 1
3
100.00%
8 / 8
 isOnIgnoredPatterns($imageSrc)
100.00%
1 / 1
3
100.00%
5 / 5
 createDOM($content)
100.00%
1 / 1
2
100.00%
6 / 6
 getImages($content)
100.00%
1 / 1
4
100.00%
12 / 12
<?php
/**
* This file is part of the FastFeed package.
*
* (c) Daniel González <daniel@desarrolla2.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FastFeed\Processor;
use DOMDocument;
use FastFeed\Item;
/**
* ImageProcessor
*/
class ImageProcessor implements ProcessorInterface
{
/**
* @var array
*/
protected $ignoredPatterns = array();
/**
* @var bool
*/
protected $overrideImage = false;
/**
* @param array $ignoredPatterns
*/
public function setIgnoredPatterns(array $ignoredPatterns)
{
$this->ignoredPatterns = array();
foreach ($ignoredPatterns as $ignoredPattern) {
$this->addIgnoredPattern($ignoredPattern);
}
}
/**
* @param $ignoredPattern
*/
public function addIgnoredPattern($ignoredPattern)
{
$this->ignoredPatterns[] = (string) $ignoredPattern;
}
/**
* @return array
*/
public function getIgnoredPatterns()
{
return $this->ignoredPatterns;
}
/**
* @param boolean $overrideImage
*/
public function setOverrideImage($overrideImage)
{
$this->overrideImage = (bool) $overrideImage;
}
/**
* Execute processor
*
* @param array $items
*
* @return array
*/
public function process(array $items)
{
foreach ($items as $key => $item) {
$items[$key] = $this->setImage($item);
}
return $items;
}
/**
* @param Item $item
*
* @return Item
*/
protected function setImage(Item $item)
{
if ($item->hasImage() && !$this->overrideImage) {
return $item;
}
$this->setImageFromContent($item);
return $item;
}
/**
* @param Item $item
*
* @return bool|string
*/
protected function setImageFromContent(Item $item)
{
$images = $this->getImages($item->getContent());
foreach ($images as $image) {
if ($this->isOnIgnoredPatterns($image)) {
continue;
}
$item->setImage($image);
return;
}
}
/**
* @param $imageSrc
*
* @return bool
*/
protected function isOnIgnoredPatterns($imageSrc)
{
foreach ($this->ignoredPatterns as $ignoredPattern) {
if (preg_match($ignoredPattern, $imageSrc)) {
return true;
}
}
return false;
}
/**
* @param $content
*
* @return bool|DOMDocument
*/
protected function createDOM($content)
{
if (!$content) {
return false;
}
$dom = new DOMDocument();
$dom->loadHTML($content);
$dom->preserveWhiteSpace = false;
return $dom;
}
/**
* @param $content
*
* @return bool|array
*/
protected function getImages($content)
{
$images = array();
$dom = $this->createDOM($content);
if (!$dom) {
return $images;
}
$domList = $dom->getElementsByTagName('img');
foreach ($domList as $image) {
$imageSrc = $image->getAttribute('src');
if (!$imageSrc) {
continue;
}
$images[] = $imageSrc;
}
return $images;
}
}