Completed
Push — master ( 782061...5c46be )
by
unknown
13:53
created

LibrinfoLabelTranslatorStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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
 * Type to find missing translation : bin/console blast:translations:extract -f YAML
27
 */
28
class LibrinfoLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
29
{
30
    protected $namePrefix; /* may be the bundle name */
31
    protected $nameFix; /* may be the admin name */
32
    protected $nameFixSave; /* may be the admin name */
33
34
    public function __construct($prefix = 'Blast\CoreBundle', $fix = 'CoreAdmin')
35
    {
36
        $this->setPrefix($prefix);
37
        $this->setFix($fix);
38
    }
39
40
    public function setPrefix($prefix): LibrinfoLabelTranslatorStrategy
41
    {
42
        $this->namePrefix = $this->cleanStr($prefix);
43
44
        return $this;
45
    }
46
47
    public function setFix($fix, $isTmp = false): LibrinfoLabelTranslatorStrategy
48
    {
49
        if ($isTmp) {
50
            $this->nameFixSave = $this->nameFix;
51
        }
52
53
        /* Warning last set is current ... */
54
        $this->nameFix = $this->cleanStr($fix);
55
56
        return $this;
57
    }
58
59
    public function cleanStr($str): string
60
    {
61
        $str = strtolower(str_replace('\\', '.', $str));
62
        /* user love \ */
63
        $str = str_replace('..', '.', $str);
64
        $str = str_replace(' ', '_', $str);
65
66
        return $str;
67
    }
68
69
    public function doResetFix()
70
    {
71
        if (isset($this->nameFixSave)) {
72
            $this->nameFix = $this->nameFixSave;
73
            $this->nameFixSave = null;
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getLabel($label, $context = '', $type = ''): string
81
    {
82
        $label = str_replace('.', '_', $label);
83
        $label = $this->cleanStr($label); /* if there is still some \ */
84
        $label = sprintf('%s', strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
85
86
        //$resLabel = $this->namePrefix . '.' . $this->nameFix . '.' . $context . '.' . $label;
87
        //$this->doResetFix(); /* for $isTmp to true see setFix */
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
        $resLabel = $this->namePrefix . '.' . $label;
89
90
        $resLabel = $this->cleanStr($resLabel);
91
92
        return $resLabel;
93
    }
94
}
95