Code Duplication    Length = 118-118 lines in 2 locations

src/Ui/Form/Component/Field/Guesser/InstructionsGuesser.php 1 location

@@ 14-131 (lines=118) @@
11
 * @author PyroCMS, Inc. <[email protected]>
12
 * @author Ryan Thompson <[email protected]>
13
 */
14
class InstructionsGuesser
15
{
16
17
    /**
18
     * Guess the field instructions.
19
     *
20
     * @param FormBuilder $builder
21
     */
22
    public function guess(FormBuilder $builder)
23
    {
24
        $fields = $builder->getFields();
25
        $stream = $builder->getFormStream();
26
27
        foreach ($fields as &$field) {
28
            $locale = array_get($field, 'locale');
29
30
            /*
31
             * If the instructions are already set then use it.
32
             */
33
            if (isset($field['instructions'])) {
34
                if (str_is('*::*', $field['instructions'])) {
35
                    $field['instructions'] = trans($field['instructions'], [], null, $locale);
36
                }
37
38
                continue;
39
            }
40
41
            /*
42
             * If we don't have a field then we
43
             * can not really guess anything here.
44
             */
45
            if (!isset($field['field'])) {
46
                continue;
47
            }
48
49
            /*
50
             * No stream means we can't
51
             * really do much here.
52
             */
53
            if (!$stream instanceof StreamInterface) {
54
                continue;
55
            }
56
57
            $assignment = $stream->getAssignment($field['field']);
58
            $object     = $stream->getField($field['field']);
59
60
            /*
61
             * No assignment means we still do
62
             * not have anything to do here.
63
             */
64
            if (!$assignment instanceof AssignmentInterface) {
65
                continue;
66
            }
67
68
            /*
69
             * Next try using the fallback assignment
70
             * instructions system as generated verbatim.
71
             */
72
            $instructions = $assignment->getInstructions() . '.default';
73
74
            if (!isset($field['instructions']) && str_is('*::*', $instructions) && trans()->has(
75
                    $instructions,
76
                    $locale
77
                )
78
            ) {
79
                $field['instructions'] = trans($instructions, [], null, $locale);
80
            }
81
82
            /*
83
             * Next try using the default assignment
84
             * instructions system as generated verbatim.
85
             */
86
            $instructions = $assignment->getInstructions();
87
88
            if (
89
                !isset($field['instructions'])
90
                && str_is('*::*', $instructions)
91
                && trans()->has($instructions, $locale)
92
                && is_string($translated = trans($instructions, [], null, $locale))
93
            ) {
94
                $field['instructions'] = $translated;
95
            }
96
97
            /*
98
             * Check if it's just a standard string.
99
             */
100
            if (!isset($field['instructions']) && $instructions && !str_is('*::*', $instructions)) {
101
                $field['instructions'] = $instructions;
102
            }
103
104
            /*
105
             * Next try using the default field
106
             * instructions system as generated verbatim.
107
             */
108
            $instructions = $object->getInstructions();
109
110
            if (
111
                !isset($field['instructions'])
112
                && str_is('*::*', $instructions)
113
                && trans()->has($instructions, $locale)
114
                && is_string($translated = trans($instructions, [], null, $locale))
115
            ) {
116
                $field['instructions'] = $translated;
117
            }
118
119
            /*
120
             * Check if it's just a standard string.
121
             */
122
            if (!isset($field['instructions']) && $instructions && !str_is('*::*', $instructions)) {
123
                $field['instructions'] = $instructions;
124
            }
125
        }
126
127
        $builder->setFields($fields);
128
    }
129
}
130

src/Ui/Form/Component/Field/Guesser/PlaceholdersGuesser.php 1 location

@@ 14-131 (lines=118) @@
11
 * @author PyroCMS, Inc. <[email protected]>
12
 * @author Ryan Thompson <[email protected]>
13
 */
14
class PlaceholdersGuesser
15
{
16
17
    /**
18
     * Guess the field placeholders.
19
     *
20
     * @param FormBuilder $builder
21
     */
22
    public function guess(FormBuilder $builder)
23
    {
24
        $fields = $builder->getFields();
25
        $stream = $builder->getFormStream();
26
27
        foreach ($fields as &$field) {
28
            $locale = array_get($field, 'locale');
29
30
            /*
31
             * If the placeholder are already set then use it.
32
             */
33
            if (isset($field['placeholder'])) {
34
                if (str_is('*::*', $field['placeholder'])) {
35
                    $field['placeholder'] = trans($field['placeholder'], [], null, $locale);
36
                }
37
38
                continue;
39
            }
40
41
            /*
42
             * If we don't have a field then we
43
             * can not really guess anything here.
44
             */
45
            if (!isset($field['field'])) {
46
                continue;
47
            }
48
49
            /*
50
             * No stream means we can't
51
             * really do much here.
52
             */
53
            if (!$stream instanceof StreamInterface) {
54
                continue;
55
            }
56
57
            $assignment = $stream->getAssignment($field['field']);
58
            $object     = $stream->getField($field['field']);
59
60
            /*
61
             * No assignment means we still do
62
             * not have anything to do here.
63
             */
64
            if (!$assignment instanceof AssignmentInterface) {
65
                continue;
66
            }
67
68
            /*
69
             * Next try using the fallback assignment
70
             * placeholder system as generated verbatim.
71
             */
72
            $placeholder = $assignment->getPlaceholder() . '.default';
73
74
            if (!isset($field['placeholder']) && str_is('*::*', $placeholder) && trans()->has(
75
                    $placeholder,
76
                    $locale
77
                )
78
            ) {
79
                $field['placeholder'] = trans($placeholder, [], null, $locale);
80
            }
81
82
            /*
83
             * Next try using the default assignment
84
             * placeholder system as generated verbatim.
85
             */
86
            $placeholder = $assignment->getPlaceholder();
87
88
            if (
89
                !isset($field['placeholder'])
90
                && str_is('*::*', $placeholder)
91
                && trans()->has($placeholder, $locale)
92
                && is_string($translated = trans($placeholder, [], null, $locale))
93
            ) {
94
                $field['placeholder'] = $translated;
95
            }
96
97
            /*
98
             * Check if it's just a standard string.
99
             */
100
            if (!isset($field['placeholder']) && $placeholder && !str_is('*::*', $placeholder)) {
101
                $field['placeholder'] = $placeholder;
102
            }
103
104
            /*
105
             * Next try using the default field
106
             * placeholder system as generated verbatim.
107
             */
108
            $placeholder = $object->getPlaceholder();
109
110
            if (
111
                !isset($field['placeholder'])
112
                && str_is('*::*', $placeholder)
113
                && trans()->has($placeholder, $locale)
114
                && is_string($translated = trans($placeholder, [], null, $locale))
115
            ) {
116
                $field['placeholder'] = $translated;
117
            }
118
119
            /*
120
             * Check if it's just a standard string.
121
             */
122
            if (!isset($field['placeholder']) && $placeholder && !str_is('*::*', $placeholder)) {
123
                $field['placeholder'] = $placeholder;
124
            }
125
        }
126
127
        $builder->setFields($fields);
128
    }
129
}
130