Completed
Push — master ( 4e2c55...2c21d8 )
by Andrey
01:28
created

src/TableMultilanguageField.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Itstructure\FieldWidgets;
4
5
use yii\base\{Model, Widget, InvalidConfigException};
6
use Itstructure\FieldWidgets\interfaces\LanguageFieldInterface;
7
8
/**
9
 * Class TableMultilanguageField
10
 * The Widget class of the translation output for specified field.
11
 *
12
 * @property string $name
13
 * @property string $label
14
 * @property Model $model
15
 * @property LanguageFieldInterface $language
16
 * @property string $default
17
 *
18
 * @package Itstructure\FieldWidgets
19
 */
20
class TableMultilanguageField extends Widget
21
{
22
    /**
23
     * Field name.
24
     *
25
     * @var string
26
     */
27
    public $name;
28
29
    /**
30
     * Field label.
31
     *
32
     * @var string
33
     */
34
    public $label;
35
36
    /**
37
     * The model of the current field.
38
     *
39
     * @var Model
40
     */
41
    public $model;
42
43
    /**
44
     * Model of languages.
45
     *
46
     * @var LanguageFieldInterface
47
     */
48
    protected $language;
49
50
    /**
51
     * The value of the empty field is the default.
52
     *
53
     * @var string
54
     */
55
    public $default = '-';
56
57
    /**
58
     * Set the language model.
59
     *
60
     * @param LanguageFieldInterface $language
61
     *
62
     * @return void
63
     */
64
    public function setLanguage(LanguageFieldInterface $language): void
65
    {
66
        $this->language = $language;
67
    }
68
69
    /**
70
     * Returns the translation of the field.
71
     *
72
     * @throws InvalidConfigException
73
     *
74
     * @return string
75
     */
76
    public function run(): string
77
    {
78
        if (null === $this->language) {
79
            throw new InvalidConfigException('Language model is not defined.');
80
        }
81
82
        if (strpos($this->name, '.') !== false) {
83
84
            $out = $this->getDataByPath($this->name);
85
86
        } else {
87
            $out = $this->model->{$this->name.'_'.$this->language->getShortName()};
88
        }
89
90
        return empty($out) ? $this->default : $out;
91
    }
92
93
    /**
94
     * Obtaining a field translation of another model along a given path through a series
95
     * of entities, separated by a point.
96
     *
97
     * @param $name string, separated by dot.
98
     *
99
     * @return string
100
     */
101
    private function getDataByPath($name): string
102
    {
103
        $namePath = explode('.', $name);
104
        $out = $this->model;
105
106
        for ($i=0; $i < count($namePath); $i++) {
0 ignored issues
show
Performance Best Practice introduced by Andrey Girnik
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
107
108
            $out = $i+1 < count($namePath) ?
109
                $out->{$namePath[$i]} : $out->{$namePath[$i].'_'.$this->language->getShortName()};
110
111
            if (is_array($out)) {
112
                for ($j=0; $j < count($out); $j++) {
0 ignored issues
show
Performance Best Practice introduced by Andrey Girnik
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
113
                    $out[$j] = $out[$j]->{$namePath[$i+1].'_'.$this->language->getShortName()};
114
                }
115
                $out = implode(', ', $out);
116
                break;
117
            }
118
        }
119
120
        return trim($out, ', ');
121
    }
122
}
123