Completed
Pull Request — wip-lisem (#51)
by
unknown
05:34 queued 02:32
created

LibrinfoLabelTranslatorStrategy::setPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
31
    public function __construct($prefix = 'Blast\CoreBundle', $fix = 'CoreAdmin')
32
    {
33
        $this->setPrefix($prefix);
34
        $this->setFix($fix);
35
    }
36
37
    public function setPrefix($prefix): LibrinfoLabelTranslatorStrategy
38
    {
39
        $this->namePrefix = $this->cleanStr($prefix);
40
41
        return $this;
42
    }
43
44
    public function setFix($fix, $doClone = false): LibrinfoLabelTranslatorStrategy
45
    {
46
        $res = $this;
47
        if ($doClone) {
48
            $res = clone $this;
49
        }
50
        /* Warning last set is current ... */
51
        $res->nameFix = $res->cleanStr($fix);
52
53
        return $res;
54
    }
55
56
    public function cleanStr($str): string
57
    {
58
        $str = strtolower(str_replace('\\', '.', $str));
59
        /* user love \ */
60
        $str = str_replace('..', '.', $str);
61
62
        return $str;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getLabel($label, $context = '', $type = ''): string
69
    {
70
        $label = str_replace('.', '_', $label);
71
        $label = $this->cleanStr($label); /* if there is still some \ */
72
        $label = sprintf('%s', strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
73
74
        return $this->namePrefix . '.' . $this->nameFix . '.' . $label;
75
    }
76
}
77