Completed
Push — wip-locale ( 34a478...492bdd )
by
unknown
02:38
created

LibrinfoLabelTranslatorStrategy::setPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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;
29
30
    public function __construct($prefix = 'Blast\CoreBundle')
31
    {
32
        $this->setPrefix($prefix);
33
    }
34
35
    public function setPrefix($prefix, $append = false) : void
36
    {
37
        $prefix = strtolower(str_replace('\\', '.', $prefix));
38
        $this->namePrefix = ($append) ? $this->namePrefix . '.' . $prefix : $prefix;
39
40
        /* user love \ */
41
        $this->namePrefix = str_replace('..', '.', $this->namePrefix);
42
    }
43
    
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getLabel($label, $context = '', $type = ''): string
48
    {
49
        $label = str_replace('.', '_', $label);
50
51
        $label = sprintf('%s', strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
52
53
        return $this->namePrefix . '.' . $label;
54
    }
55
}
56