Passed
Push — dev ( 043eb4...bf3609 )
by Dispositif
06:23
created

ArticleTemplate::anneeOrDateSerialize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Models\Wiki;
11
12
use App\Domain\Utils\TextUtil;
13
use App\Domain\Utils\WikiTextUtil;
14
use Exception;
15
16
/**
17
 * Class ArticleTemplate
18
 */
19
class ArticleTemplate extends AbstractWikiTemplate implements ArticleTemplateAlias, ArticleOrLienBriseInterface
20
{
21
    use ArticleTemplateParams, InfoTrait;
22
23
    const MODEL_NAME = 'Article';
24
25
    const REQUIRED_PARAMETERS
26
        = [
27
            //            'langue' => '',
28
            'auteur1' => '',
29
            'titre' => '', // <!-- Paramètre obligatoire -->
30
            'périodique' => '', // <!-- Paramètre obligatoire -->
31
            //            'volume' => '',
32
            //            'numéro' => '',
33
            'date' => '', // <!-- Paramètre obligatoire -->
34
            'pages' => '',
35
            'issn' => '',
36
            //            'e-issn' => '',
37
            'lire en ligne' => '',
38
            //            'consulté le' => '', // 16 mars 2020
39
            //            'id',
40
        ];
41
42
    /*
43
     * Default separator
44
     */
45
    public $userSeparator = ' |';
46
47
    public $externalTemplates = [];
48
49
    private $source;
1 ignored issue
show
introduced by
The private property $source is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @param bool|null $cleanOrder
53
     *
54
     * @return string
55
     */
56
    public function serialize(?bool $cleanOrder = false): string
57
    {
58
        // modifier ici le this->userSeparator
59
        //        if('|' === $this->userSeparator) {
60
        //            $this->userSeparator = ' |';
61
        //        }
62
        $serial = parent::serialize($cleanOrder);
63
64
        //        $serial = $this->anneeOrDateSerialize($serial);
65
66
67
        return $serial.$this->serializeExternalTemplates();
68
    }
69
70
    /**
71
     * todo move to abstract ? + refac
72
     * dirty.
73
     */
74
    public function serializeExternalTemplates(): string
75
    {
76
        $res = '';
77
        if (!empty($this->externalTemplates)) {
78
            foreach ($this->externalTemplates as $externalTemplate) {
79
                $res .= $externalTemplate->raw;
80
            }
81
        }
82
83
        return $res;
84
    }
85
86
    /**
87
     * Pas de serialization année vide si date non vide.
88
     *
89
     * @param string $serial
90
     *
91
     * @return string
92
     */
93
    private function anneeOrDateSerialize(string $serial): string
1 ignored issue
show
Unused Code introduced by
The method anneeOrDateSerialize() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
94
    {
95
        if (preg_match("#\|[\n ]*année=[\n ]*\|#", $serial) > 0
96
            && preg_match("#\|[\n ]*date=#", $serial) > 0
97
        ) {
98
            $serial = preg_replace("#\|[\n ]*année=[\n ]*#", '', $serial);
99
        }
100
101
        return $serial;
102
    }
103
104
    /**
105
     * Propose fubar pour un <ref name="fubar"> ou un {{article|'id=fubar'}}.
106
     *
107
     * @return string
108
     * @throws Exception
109
     */
110
    public function generateRefName(): string
111
    {
112
        // Style "auto1234"
113
        if (empty($this->getParam('périodique') || empty($this->getParam('date')))) {
114
            return 'auto'.(string)rand(1000, 9999);
115
        }
116
        // Style "LeMonde15022017"
117
        $periodique = str_replace(
118
            ' ',
119
            '',
120
            TextUtil::stripPunctuation(
121
                TextUtil::stripAccents(
122
                    WikiTextUtil::unWikify(
123
                        $this->getParam('périodique')
1 ignored issue
show
Bug introduced by
It seems like $this->getParam('périodique') can also be of type null; however, parameter $text of App\Domain\Utils\WikiTextUtil::unWikify() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
                        /** @scrutinizer ignore-type */ $this->getParam('périodique')
Loading history...
124
                    )
125
                )
126
            )
127
        );
128
        $date = str_replace([' ', '-'], ['', ''], $this->getParam('date'));
129
130
        return $periodique.$date;
131
    }
132
133
}
134