Passed
Push — feature/6.x ( 53903f...32797a )
by Schlaefer
05:38 queued 02:15
created

Preprocessor   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 48
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A process() 0 3 1
A _hashInternalEntryLinks() 0 17 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Saito - The Threaded Web Forum
6
 *
7
 * @copyright Copyright (c) the Saito Project Developers
8
 * @link https://github.com/Schlaefer/Saito
9
 * @license http://opensource.org/licenses/MIT
10
 */
11
12
namespace BbcodeParser\Lib;
13
14
class Preprocessor
15
{
16
    protected $_settings;
17
18
    /**
19
     * Constructor
20
     *
21
     * @param array $settings settings
22
     */
23
    public function __construct($settings)
24
    {
25
        $this->_settings = $settings;
26
    }
27
28
    /**
29
     * Process
30
     *
31
     * @param string $string string
32
     * @return string
33
     */
34
    public function process($string)
35
    {
36
        return $this->_hashInternalEntryLinks($string);
37
    }
38
39
    /**
40
     * Convert full internal links (http://domain/…/view/123) to hashes (#123)
41
     *
42
     * @param string $string string
43
     * @return string
44
     */
45
    protected function _hashInternalEntryLinks($string)
46
    {
47
        $server = $this->_settings->get('server');
48
        $webroot = $this->_settings->get('webroot');
49
        $hashBaseUrl = $this->_settings->get('hashBaseUrl');
50
        $url = $server . $webroot . $hashBaseUrl;
51
        $string = preg_replace(
52
            "%
53
				(?<!=) # don't hash if part of [url=…
54
				{$url}
55
				(\d+)  # the id
56
				%imx",
57
            "#\\1",
58
            $string
59
        );
60
61
        return $string;
62
    }
63
}
64