Completed
Push — feature/EVO-7277-schema-title ( 0eb4b8 )
by
unknown
66:17
created

FieldTitleMapper::map()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 11
nc 3
nop 2
1
<?php
2
/**
3
 * Verify require Title for fields. Use fieldName otherwise
4
 */
5
6
namespace Graviton\GeneratorBundle\Generator\ResourceGenerator;
7
8
/**
9
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
10
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
11
 * @link     http://swisscom.ch
12
 */
13
class FieldTitleMapper implements FieldMapperInterface
14
{
15
    /**
16
     * @param array $field   mappable field with type attribute
17
     * @param mixed $context context for mapper to check
18
     *
19
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
20
     */
21
    public function map($field, $context = null)
22
    {
23
        $title = 'Please add title';
24
25
        if (array_key_exists('title', $field) && !empty($field['title'])) {
26
            $title = $field['title'];
27
        } elseif (array_key_exists('fieldName', $field) && !empty($field['fieldName'])) {
28
            // Field can be metadata.action.0.command, so .0 is and Array
29
            $value = str_replace('.0', '.array', $field['fieldName']);
30
            // Field could have metadata.mime So upercase it
31
            $value = ucwords(strtolower($value), '\'.');
32
            $value = str_replace('.', '', $value);
33
            $title = preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $value);
34
        }
35
        $field['title'] = trim($title);
36
        return $field;
37
    }
38
}
39