Passed
Pull Request — main (#18)
by
unknown
07:12 queued 03:32
created

ContentReplacer::replace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Publisher;
13
14
use RuntimeException;
15
16
/**
17
 * Remplacer le contenu du texte
18
 *
19
 * @credit <a href="http://codeigniter.com">CodeIgniter 4 - \CodeIgniter\Publisher\ContentReplacer</a>
20
 */
21
class ContentReplacer
22
{
23
    /**
24
     * Remplacer le contenu
25
     *
26
     * @param array $replaces [search => replace]
27
     */
28
    public function replace(string $content, array $replaces): string
29
    {
30 4
        return strtr($content, $replaces);
31
    }
32
33
    /**
34
     * Ajouter du texte
35
     *
36
     * @param string $text    Texte à ajouter.
37
     * @param string $pattern Modèle de recherche d'expression régulière.
38
     * @param string $replace Remplacement de Regexp incluant le texte à ajouter.
39
     *
40
     * @return string|null Contenu mis à jour, ou null si non mis à jour.
41
     */
42
    private function add(string $content, string $text, string $pattern, string $replace): ?string
43
    {
44 4
        $return = preg_match('/' . preg_quote($text, '/') . '/u', $content);
45
46
        if ($return === false) {
47
            // Erreur d'expression régulière.
48
            throw new RuntimeException('Erreur d\'expression régulière. Code d\'erreur PCRE: ' . preg_last_error());
49
        }
50
51
        if ($return === 1) {
52
            // Il a déjà été mis à jour.
53 2
            return null;
54
        }
55
56 4
        $return = preg_replace($pattern, $replace, $content);
57
58
        if ($return === null) {
59
            // Erreur d'expression régulière.
60
            throw new RuntimeException('Erreur d\'expression régulière. Code d\'erreur PCRE: ' . preg_last_error());
61
        }
62
63 4
        return $return;
64
    }
65
66
    /**
67
     * Ajouter une ligne après la ligne avec la chaîne
68
     *
69
     * @param string $content Contenu entier.
70
     * @param string $line    Ligne à ajouter.
71
     * @param string $after   Chaîne à rechercher.
72
     *
73
     * @return string|null Contenu mis à jour, ou null si non mis à jour.
74
     */
75
    public function addAfter(string $content, string $line, string $after): ?string
76
    {
77 4
        $pattern = '/(.*)(\n[^\n]*?' . preg_quote($after, '/') . '[^\n]*?\n)/su';
78 4
        $replace = '$1$2' . $line . "\n";
79
80 4
        return $this->add($content, $line, $pattern, $replace);
81
    }
82
83
    /**
84
     * Ajouter une ligne avant la ligne avec la chaîne
85
     *
86
     * @param string $content Contenu entier.
87
     * @param string $line    Ligne à ajouter.
88
     * @param string $before  String à rechercher.
89
     *
90
     * @return string|null Contenu mis à jour, ou null si non mis à jour.
91
     */
92
    public function addBefore(string $content, string $line, string $before): ?string
93
    {
94 4
        $pattern = '/(\n)([^\n]*?' . preg_quote($before, '/') . ')(.*)/su';
95 4
        $replace = '$1' . $line . "\n" . '$2$3';
96
97 4
        return $this->add($content, $line, $pattern, $replace);
98
    }
99
}
100