1 | <?php |
||
6 | class FieldsInferrer |
||
7 | { |
||
8 | /** |
||
9 | * @param null|array $rows optional initial rows to infer by, each row is an array of field name => field value |
||
10 | */ |
||
11 | public function __construct($rows=null, $lenient=false) |
||
18 | |||
19 | /** |
||
20 | * add rows and updates the fieldsPopularity array - to make the inferred fields more accurate |
||
21 | * @param $rows |
||
22 | * @throws FieldValidationException |
||
23 | */ |
||
24 | public function addRows($rows) |
||
46 | |||
47 | /** |
||
48 | * return the best inferred fields along with the best value casting according to the rows received so far |
||
49 | * @return array field name => inferred field object |
||
50 | * @throws FieldValidationException |
||
51 | */ |
||
52 | public function infer() |
||
60 | |||
61 | /** |
||
62 | * returns all the input rows got so far with the best cast value for each field |
||
63 | * @return array of arrays of field name => best cast value |
||
64 | */ |
||
65 | public function castRows() |
||
69 | |||
70 | protected $inputRows = []; |
||
71 | protected $castRows = []; |
||
72 | protected $fieldsPopularity = []; |
||
73 | protected $fieldsPopularityObjects = []; |
||
74 | protected $lenient; |
||
75 | |||
76 | /** |
||
77 | * infer field objects for the given row |
||
78 | * raises exception if fails to infer a field |
||
79 | * @param $row array field name => value to infer by |
||
80 | * @return array field name => inferred field object |
||
81 | * @throws FieldValidationException |
||
82 | */ |
||
83 | protected function inferRow($row) |
||
91 | |||
92 | /** |
||
93 | * @return array |
||
94 | */ |
||
95 | protected function getFieldNames() |
||
101 | |||
102 | /** |
||
103 | * finds the best inferred fields for the given field name according to the popularity |
||
104 | * also updates the castRows array with the latest cast values |
||
105 | * @param $fieldName |
||
106 | * @param $fieldTypesPopularity |
||
107 | * @return BaseField|null |
||
108 | */ |
||
109 | protected function inferField($fieldName, $fieldTypesPopularity) |
||
131 | } |