Completed
Push — master ( aabeef...856c1c )
by Propa
41:29
created

FormBuilder::bind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 39
    public function setLocales(array $locales)
21
    {
22 39
        $this->locales = $locales;
23 39
    }
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 6
    protected function getBoundValue($name, $default)
33
    {
34 6
        $inputName = preg_split('/[\[\]]+/', $name, - 1, PREG_SPLIT_NO_EMPTY);
35 6
        if (count($inputName) == 2 && in_array($inputName[0], $this->locales)) {
36 3
            list($lang, $name) = $inputName;
37 3
            $value = isset($this->boundData->data()->translate($lang)->{$name})
38 3
                ? $this->boundData->data()->translate($lang)->{$name}
39 3
                : '';
40
41 3
            return $this->escape($value);
42
        }
43
44 3
        return $this->escape($this->boundData->get($name, $default));
45
    }
46
}
47