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