FieldFormatGuesser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 59
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldDefinitionGuess() 0 17 3
A strtocamelcase() 0 7 1
A putRow() 0 6 2
A getOrCreateFieldGuess() 0 9 2
A addGuess() 0 5 1
1
<?php
2
3
namespace Mathielen\ImportEngineBundle\Generator\ValueObject;
4
5
class FieldFormatGuesser
6
{
7
    /**
8
     * @var FieldFormatGuess[]
9
     */
10
    private $fields = array();
11
12
    public function getFieldDefinitionGuess($defaultFieldFormat = 'string')
13
    {
14
        $fieldDefinitions = array();
15
        foreach ($this->fields as $fieldName => $fieldGuess) {
16
            $fieldDefinition = $fieldGuess->guessFieldType($defaultFieldFormat);
17
18
            $sanitizedFieldName = self::strtocamelcase($fieldName);
19
            if ($sanitizedFieldName != $fieldName) {
20
                $fieldDefinition['serialized_name'] = $fieldName;
21
                $fieldName = $sanitizedFieldName;
22
            }
23
24
            $fieldDefinitions[$fieldName] = $fieldDefinition;
25
        }
26
27
        return $fieldDefinitions;
28
    }
29
30
    private static function strtocamelcase($str)
31
    {
32
        $str = iconv('utf-8', 'ascii//TRANSLIT', $str);
33
34
        return preg_replace_callback('#[^\w]+(.)#',
35
            create_function('$r', 'return strtoupper($r[1]);'), $str);
36
    }
37
38
    public function putRow(array $row)
39
    {
40
        foreach ($row as $k => $v) {
41
            $this->addGuess($k, $v);
42
        }
43
    }
44
45
    /**
46
     * @return FieldFormatGuess
47
     */
48
    private function getOrCreateFieldGuess($fieldname)
49
    {
50
        $fieldname = strtolower($fieldname);
51
        if (!isset($this->fields[$fieldname])) {
52
            $this->fields[$fieldname] = new FieldFormatGuess();
53
        }
54
55
        return $this->fields[$fieldname];
56
    }
57
58
    private function addGuess($fieldname, $fieldvalue)
59
    {
60
        $fieldGuess = $this->getOrCreateFieldGuess($fieldname);
61
        $fieldGuess->addValue($fieldvalue);
62
    }
63
}
64