Completed
Push — master ( b10a48...29cde4 )
by Ori
05:27
created

InferSchema   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 49
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A descriptor() 0 4 1
A castRow() 0 17 3
A lock() 0 5 1
1
<?php
2
namespace frictionlessdata\tableschema;
3
4
/**
5
 *  Table Schema which updates the descriptor based on input data
6
 */
7
class InferSchema extends Schema
8
{
9
    public function __construct($descriptor=null, $lenient=false)
10
    {
11
        $this->descriptor = empty($descriptor) ? (object)["fields" => []] : $descriptor;
12
        $this->fieldsInferer = new Fields\FieldsInferrer(null, $lenient);
13
    }
14
15
    /**
16
     * @return object
17
     */
18
    public function descriptor()
19
    {
20
        return $this->descriptor;
21
    }
22
23
    /**
24
     * @param mixed[] $row
25
     * @return mixed[]
26
     * @throws Exceptions\FieldValidationException
27
     */
28
    public function castRow($row)
29
    {
30
        if ($this->isLocked) {
31
            // schema is locked, no more inferring is needed
32
            return parent::castRow($row);
33
        } else {
34
            // add the row to the inferrer, update the descriptor according to the best inferred fields
35
            $this->fieldsInferer->addRows([$row]);
36
            $this->descriptor->fields = [];
37
            foreach ($this->fieldsInferer->infer() as $fieldName => $inferredField) {
38
                /** @var Fields\BaseField $inferredField */
39
                $this->descriptor->fields[] = $inferredField->descriptor();
40
            }
41
            $this->castRows = $this->fieldsInferer->castRows();
42
            return $this->castRows[count($this->castRows)-1];
43
        }
44
    }
45
46
    public function lock()
47
    {
48
        $this->isLocked = true;
49
        return $this->castRows;
50
    }
51
52
    protected $isLocked = false;
53
    protected $fieldsInferer;
54
    protected $castRows;
55
}