Completed
Push — master ( 750af5...855623 )
by Propa
04:20
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 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
ccs 9
cts 9
cp 1
cc 4
eloc 9
nc 3
nop 2
crap 4
1
<?php namespace Propaganistas\LaravelTranslatableBootForms\Form;
2
3
use AdamWathan\Form\FormBuilder as _FormBuilder;
4
5
class FormBuilder extends _FormBuilder
6
{
7
8
    /**
9
     * Array of locale keys.
10
     *
11
     * @var array
12
     */
13
    protected $locales;
14
15
    /**
16
     * Sets the available locales for translatable fields.
17
     *
18
     * @param array $locales
19
     */
20 42
    public function setLocales(array $locales)
21
    {
22 42
        $this->locales = $locales;
23 28
    }
24
25
    /**
26
     * Getting value from Model or ModelTranslation to populate form.
27
     * Courtesy of TypiCMS/TranslatableBootForms (https://github.com/TypiCMS/TranslatableBootForms/blob/master/src/TranslatableFormBuilder.php)
28
     *
29
     * @param string $name key
30
     * @return string value
31
     */
32 4
    protected function getBoundValue($name, $default)
33
    {
34 4
        $inputName = preg_split('/[\[\]]+/', $name, - 1, PREG_SPLIT_NO_EMPTY);
35 4
        if (count($inputName) == 2 && in_array($inputName[0], $this->locales)) {
36 2
            list($lang, $name) = $inputName;
37 2
            $value = isset($this->boundData->data()->translate($lang)->{$name})
38 2
                ? $this->boundData->data()->translate($lang)->{$name}
39 2
                : '';
40
41 2
            return $this->escape($value);
42
        }
43
44 2
        return $this->escape($this->boundData->get($name, $default));
45
    }
46
}
47