Completed
Push — master ( 3a696f...2fe722 )
by Propa
10s
created

FormBuilder::getBoundValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 9
nc 3
nop 2
crap 4
1
<?php namespace Propaganistas\LaravelTranslatableBootForms\Form;
2
3
use AdamWathan\Form\Elements\Label;
4
use AdamWathan\Form\FormBuilder as _FormBuilder;
5
use Propaganistas\LaravelTranslatableBootForms\Form\Binding\BoundData;
6
7
class FormBuilder extends _FormBuilder
8
{
9
10
    /**
11
     * Array of locale keys.
12
     *
13
     * @var array
14
     */
15
    protected $locales;
16
17
    /**
18
     * Sets the available locales for translatable fields.
19
     *
20
     * @param array $locales
21
     */
22 39
    public function setLocales(array $locales)
23
    {
24 39
        $this->locales = $locales;
25 39
    }
26
27 18
    public function bind($data)
28
    {
29 18
        $this->boundData = new BoundData($data);
0 ignored issues
show
Bug introduced by
The property boundData does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30 18
    }
31
32
    /**
33
     * Getting value from Model or ModelTranslation to populate form.
34
     * Courtesy of TypiCMS/TranslatableBootForms (https://github.com/TypiCMS/TranslatableBootForms/blob/master/src/TranslatableFormBuilder.php)
35
     *
36
     * @param string $name key
37
     * @return string value
38
     */
39 6
    protected function getBoundValue($name, $default)
40
    {
41 6
        $inputName = preg_split('/[\[\]]+/', $name, - 1, PREG_SPLIT_NO_EMPTY);
42 6
        if (count($inputName) == 2 && in_array($inputName[0], $this->locales)) {
43 3
            list($lang, $name) = $inputName;
44 3
            $value = isset($this->boundData->data()->translate($lang)->{$name})
45 3
                ? $this->boundData->data()->translate($lang)->{$name}
46 3
                : '';
47
48 3
            return $this->escape($value);
49
        }
50
51 3
        return $this->escape($this->boundData->get($name, $default));
52
    }
53
}
54