Passed
Branch dev (b7aeac)
by Dispositif
03:10
created

OuvrageTemplate::getDataSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
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
/**
13
 * Parameters names changed on hydration (alias)
14
 * Raw datas : Value are not normalized (see rather OuvrageClean class for optimized values)
15
 * Note : Avec serialization(), extraction de extrait=,commentaire= (obsolètes)
16
 * sur {{citationbloc}},{{commentaire biblio}}...
17
 * Class OuvrageTemplate.
18
 */
19
class OuvrageTemplate extends OuvrageTemplateAlias implements WikiTemplateInterface
20
{
21
    use OuvrageTemplateParams, BiblioTemplateTrait;
22
23
    const WIKITEMPLATE_NAME = 'Ouvrage';
24
25
    const REQUIRED_PARAMETERS = ['titre'];
26
27
    const MINIMUM_PARAMETERS
28
        = [
29
            //            'langue' => '', // inutile avec 'fr'
30
            //            'auteur1' => '', // duplicate with prénom1/nom1
31
            'titre' => '', // obligatoire
32
            'éditeur' => '',
33
            'année' => '', // géré dans serialize
34
            'pages totales' => '',
35
            //            'passage' => '', // pas pertinent sur biblio et liste oeuvres
36
            'isbn' => '',
37
        ];
38
39
    public $externalTemplates = [];
40
41
    private $dataSource;
42
43
    /**
44
     * @return mixed
45
     */
46
    public function getDataSource()
47
    {
48
        return $this->dataSource;
49
    }
50
51
    /**
52
     * @param mixed $dataSource
53
     */
54
    public function setDataSource($dataSource): void
55
    {
56
        $this->dataSource = $dataSource;
57
    }
58
59
    /**
60
     * @param bool|null $cleanOrder
61
     *
62
     * @return string
63
     */
64
    public function serialize(?bool $cleanOrder = false): string
65
    {
66
        // modifier ici le this->userSeparator
67
        //        if('|' === $this->userSeparator) {
68
        //            $this->userSeparator = ' |';
69
        //        }
70
        $serial = parent::serialize($cleanOrder);
71
        $serial = $this->anneeOrDateSerialize($serial);
72
        $serial = $this->stripIsbnBefore1970($serial);
73
74
        return $serial.$this->serializeExternalTemplates();
75
    }
76
77
    /**
78
     * dirty.
79
     */
80
    public function serializeExternalTemplates(): string
81
    {
82
        $res = '';
83
        if (!empty($this->externalTemplates)) {
84
            foreach ($this->externalTemplates as $externalTemplate) {
85
                $res .= $externalTemplate->raw;
86
            }
87
        }
88
89
        return $res;
90
    }
91
92
    /**
93
     * Strip empty 'isbn' before 1970.
94
     *
95
     * @param string $serial
96
     *
97
     * @return string
98
     */
99
    private function stripIsbnBefore1970(string $serial): string
100
    {
101
        if (preg_match("#\|[\n ]*isbn=[\n ]*[|}]#", $serial) > 0
102
            && preg_match("#\|[\n ]*année=([0-9]+)[}| \n]#", $serial, $matches) > 0
103
        ) {
104
            $year = intval($matches[1]);
105
            if ($year > 0 && $year < 1970) {
106
                $serial = preg_replace("#\|[\n ]*isbn=[\n ]*#", '', $serial);
107
            }
108
        }
109
110
        return $serial;
111
    }
112
113
}
114