Passed
Push — master ( 82b0d0...1ac70f )
by Bobby
09:15
created

Configuration::loadDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ballen\Linguist;
4
5
/**
6
 * Linguist
7
 *
8
 * Linguist is a PHP library for parsing strings, it can extract and manipulate
9
 *  prefixed words in content ideal for working with @mentions, #topics and
10
 *  even custom action tags!
11
 *
12
 * @author Bobby Allen <[email protected]>
13
 * @license http://www.gnu.org/licenses/gpl-3.0.html
14
 * @link https://github.com/allebb/linguist
15
 * @link http://www.bobbyallen.me
16
 *
17
 */
18
class Configuration
19
{
20
21
    /**
22
     * Runtime  storage for instance configuration.
23
     * @var array
24
     */
25
    private $configuration = [];
26
27
    /**
28
     * A default configuration (based on Twitter)
29
     * @var array 
30
     */
31
    private $default_configuration = [
32
        'mentions' => [
33
            'prefix' => '@',
34
            'url' => 'https://twitter.com/%s',
35
            'target' => true,
36
            'class' => null,
37
        ],
38
        'topics' => [
39
            'prefix' => '#',
40
            'url' => 'https://twitter.com/hashtag/%s',
41
            'target' => true,
42
            'class' => null,
43
        ],
44
    ];
45
46
    /**
47
     * Push a tag configuraiton on to the configuration.
48
     * @param string $name The name of the tag eg. 'mentions'
49
     * @param string $prefix The tag prefix that we will search on eg. '@'
50
     * @param string $url_pattern The URL pattern to use when outputting to HTML. (use %s as the dynamic placeholder)
51
     * @param boolean $target_blank Optionally request that the HTML link is opened in a new window (target=_blank)
52
     * @param string $class Optional HTML class to be used in the HTML link.
53
     */
54 8
    public function push($name, $prefix, $url_pattern, $target_blank = false, $class = null)
55
    {
56 8
        $this->configuration[$name] = [
57 8
            'prefix' => $prefix,
58 8
            'url' => $url_pattern,
59 8
            'target' => $target_blank,
60 8
            'class' => $class,
61
        ];
62 8
    }
63
64
    /**
65
     * Remove a tag type from the configuration.
66
     * @param string $name The name of the tag type to remove eg. 'mentions'
67
     */
68 2
    public function drop($name)
69
    {
70 2
        if (key_exists($name, $this->configuration)) {
71 2
            unset($this->configuration[$name]);
72
        }
73 2
    }
74
75
    /**
76
     * Return the tags configuration.
77
     * @return array
78
     */
79 28
    public function get()
80
    {
81 28
        return $this->configuration;
82
    }
83
84
    /**
85
     * Load the 'default' configuration set (compatible with Twitter)
86
     * @return void
87
     */
88 24
    public function loadDefault()
89
    {
90 24
        $this->configuration = $this->default_configuration;
91 24
    }
92
}
93