EmphasisConverter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 86
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 3 1
A convert() 0 23 4
A prefixSuffix() 0 7 3
A hasLinks() 0 7 1
A getSupportedTags() 0 3 1
1
<?php
2
3
namespace Pilipinews\Common\Converters;
4
5
use League\HTMLToMarkdown\Configuration;
6
use League\HTMLToMarkdown\ConfigurationAwareInterface;
7
use League\HTMLToMarkdown\Converter\ConverterInterface;
8
use League\HTMLToMarkdown\ElementInterface;
9
10
/**
11
 * Emphasis Converter
12
 *
13
 * @package Pilipinews
14
 * @author  Rougin Gutib <[email protected]>
15
 */
16
class EmphasisConverter implements ConverterInterface, ConfigurationAwareInterface
17
{
18
    /**
19
     * @var \League\HTMLToMarkdown\Configuration
20
     */
21
    protected $config;
22
23
    /**
24
     * Sets the configuration instance.
25
     *
26
     * @param \League\HTMLToMarkdown\Configuration $config
27
     */
28 15
    public function setConfig(Configuration $config)
29
    {
30 15
        $this->config = $config;
31 15
    }
32
33
    /**
34
     * Converts the specified element into a parsed string.
35
     *
36
     * @param  \League\HTMLToMarkdown\ElementInterface $element
37
     * @return string
38
     */
39 15
    public function convert(ElementInterface $element)
40
    {
41 15
        $tag = $element->getTagName();
42
43 15
        $value = $element->getValue();
44
45 15
        if ($tag === 'i' || $tag === 'em')
46 5
        {
47 3
            $style = $this->config->getOption('italic_style');
48 1
        }
49
        else
50
        {
51 15
            $style = $this->config->getOption('bold_style');
52
53 15
            if (! $this->hasLinks($value))
54 5
            {
55 15
                $value = mb_convert_case($value, MB_CASE_UPPER, 'UTF-8');
56 5
            }
57
        }
58
59 15
        list($prefix, $suffix) = $this->prefixSuffix($value);
60
61 15
        return $prefix . $style . trim($value) . $style . $suffix;
62
    }
63
64
    /**
65
     * Returns the supported HTML tags.
66
     *
67
     * @return string[]
68
     */
69 15
    public function getSupportedTags()
70
    {
71 15
        return array('em', 'i', 'strong', 'b');
72
    }
73
74
    /**
75
     * Checks if the specified string contains a URL.
76
     *
77
     * @param  string  $string
78
     * @return boolean
79
     */
80 15
    protected function hasLinks($string)
81
    {
82 15
        $regex = '(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})';
83
84 15
        preg_match($regex, $string, $matches);
85
86 15
        return isset($matches[0]) === true;
87
    }
88
89
    /**
90
     * Returns prefix and suffix of a specified string.
91
     *
92
     * @param  string $string
93
     * @return string
94
     */
95 15
    protected function prefixSuffix($string)
96
    {
97 15
        $prefix = ltrim($string) !== $string ? ' ' : '';
98
99 15
        $suffix = rtrim($string) !== $string ? ' ' : '';
100
101 15
        return (array) array($prefix, $suffix);
102
    }
103
}
104