Completed
Push — master ( a0dfb5...677152 )
by Propa
03:21
created

FormBuilder::unbind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Propaganistas\LaravelTranslatableBootForms\Form;
2
3
use AdamWathan\Form\Elements\Label;
4
use AdamWathan\Form\FormBuilder as _FormBuilder;
5
6
class FormBuilder extends _FormBuilder
7
{
8
9
    /**
10
     * Array of locale keys.
11
     *
12
     * @var array
13
     */
14
    protected $locales;
15
16
    /**
17
     * Since $model is a private property in the parent class, we need to define the model in this child class as well.
18
     * {@inheritdoc}
19
     */
20
    protected $model;
21
22
    /**
23
     * Sets the available locales for translatable fields.
24
     *
25
     * @param array $locales
26
     */
27
    public function setLocales(array $locales)
28
    {
29
        $this->locales = $locales;
30
    }
31
32
    /**
33
     * Since $model is a private property in the parent class, we need to bind the model in this child class as well.
34
     * {@inheritdoc}
35
     *
36
     */
37
    public function bind($model)
38
    {
39
        $this->model = is_array($model) ? (object) $model : $model;
40
        parent::bind($model);
41
    }
42
43
    /**
44
     * Since $model is a private property in the parent class, we need to unbind the model in this child class as well.
45
     * {@inheritdoc}
46
     *
47
     */
48
    protected function unbindModel()
49
    {
50
        $this->model = null;
51
        parent::unbindModel();
52
    }
53
54
    /**
55
     * Getting value from Model or ModelTranslation to populate form.
56
     * Courtesy of TypiCMS/TranslatableBootForms (https://github.com/TypiCMS/TranslatableBootForms/blob/master/src/TranslatableFormBuilder.php)
57
     *
58
     * @param string $name key
59
     * @return string value
60
     */
61
    protected function getModelValue($name)
62
    {
63
        $inputName = preg_split('/[\[\]]+/', $name, - 1, PREG_SPLIT_NO_EMPTY);
64
        if (count($inputName) == 2 && in_array($inputName[0], $this->locales)) {
65
            list($lang, $name) = $inputName;
66
            $value = isset($this->model->translate($lang)->{$name})
67
                ? $this->model->translate($lang)->{$name}
68
                : '';
69
70
            return $this->escape($value);
71
        }
72
73
        return $this->escape($this->model->{$name});
74
    }
75
76
    /**
77
     * Create a label.
78
     *
79
     * @param string $label
80
     * @return \AdamWathan\Form\Elements\Label
81
     */
82
    public function label($label)
83
    {
84
        // Wrap the label title in span.
85
        if (strpos($label, '<span') !== 0) {
86
            $length = strpos($label, '<') ?: strlen($label);
87
            $label_title = substr($label, 0, $length);
88
            $label = str_replace($label_title, '<span>' . $label_title . '</span>', $label);
89
        }
90
91
        return new Label($label);
92
    }
93
}