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

FieldTitleMapper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 26
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B map() 0 17 5
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