Completed
Push — wip-locale ( 9d603e...91a024 )
by
unknown
02:37
created

LibrinfoLabelTranslatorStrategy::doResetFix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\CoreBundle\Translator;
14
15
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
16
17
/**
18
 * Class LibrinfoLabelTranslatorStrategy.
19
 *
20
 * Provides a specific label translation strategy for Librinfo.
21
 * It is based on UnderscoreLabelTranslatorStrategy, but without the context,
22
 * and labels are prefixed by "librinfo.label."
23
 *
24
 * i.e. isValid => librinfo.label.is_valid
25
 */
26
class LibrinfoLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
27
{
28
    protected $namePrefix; /* may be the bundle name */
29
    protected $nameFix; /* may be the admin name */
30
    protected $nameFixSave; /* may be the admin name */
31
32
    public function __construct($prefix = 'Blast\CoreBundle', $fix = 'CoreAdmin')
33
    {
34
        $this->setPrefix($prefix);
35
        $this->setFix($fix);
36
    }
37
38
    public function setPrefix($prefix): LibrinfoLabelTranslatorStrategy
39
    {
40
        $this->namePrefix = $this->cleanStr($prefix);
41
42
        return $this;
43
    }
44
45
    public function setFix($fix, $isTmp = false): LibrinfoLabelTranslatorStrategy
46
    {
47
        if ($isTmp) {
48
            $this->nameFixSave = $this->nameFix;
49
        }
50
51
        /* Warning last set is current ... */
52
        $this->nameFix = $this->cleanStr($fix);
53
54
        return $this;
55
    }
56
57
    public function cleanStr($str): string
58
    {
59
        $str = strtolower(str_replace('\\', '.', $str));
60
        /* user love \ */
61
        $str = str_replace('..', '.', $str);
62
63
        return $str;
64
    }
65
66
    public function doResetFix()
67
    {
68
        if (isset($this->nameFixSave)) {
69
            $this->nameFix = $this->nameFixSave;
70
            $this->nameFixSave = null;
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getLabel($label, $context = '', $type = ''): string
78
    {
79
        $label = str_replace('.', '_', $label);
80
        $label = $this->cleanStr($label); /* if there is still some \ */
81
        $label = sprintf('%s', strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
82
83
        $resLabel = $this->namePrefix . '.' . $this->nameFix . '.' . $label;
84
85
        $this->doResetFix(); /* for $isTmp to true see setFix */
86
87
        return $resLabel;
88
    }
89
}
90