Completed
Pull Request — develop (#609)
by Narcotic
12:02 queued 07:18
created

FieldTitleMapper::map()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
rs 8.8571
cc 6
eloc 12
nc 4
nop 2
crap 6.0208
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 8
    public function map($field, $context = null)
22
    {
23 8
        $title = 'Please add title';
24
25 8
        if (array_key_exists('title', $field) && !empty($field['title'])) {
26
            $title = $field['title'];
27 8
        } elseif (array_key_exists('fieldName', $field) && !empty($field['fieldName'])) {
28 8
            $value = $field['fieldName'];
29
            // Field have dots
30 8
            if (strpos($value, '.') !== false) {
31 4
                $value = str_replace('.', ' ', str_replace('.0', '.array', $value));
32
            }
33 8
            $value = preg_replace('/(?<=\\w)(?=[A-Z])/', " $1", $value);
34 8
            $title = ucfirst(strtolower($value));
35
        }
36 8
        $field['title'] = trim($title);
37 8
        return $field;
38
    }
39
}
40