Code Duplication    Length = 118-118 lines in 2 locations

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

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

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

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