1 | <?php |
||
7 | class FieldsInferrer |
||
8 | { |
||
9 | /** |
||
10 | * @param null|array $rows optional initial rows to infer by, each row is an array of field name => field value |
||
11 | */ |
||
12 | public function __construct($rows = null, $lenient = false) |
||
19 | |||
20 | /** |
||
21 | * add rows and updates the fieldsPopularity array - to make the inferred fields more accurate. |
||
22 | * |
||
23 | * @param $rows |
||
24 | * |
||
25 | * @throws FieldValidationException |
||
26 | */ |
||
27 | public function addRows($rows) |
||
49 | |||
50 | /** |
||
51 | * return the best inferred fields along with the best value casting according to the rows received so far. |
||
52 | * |
||
53 | * @return array field name => inferred field object |
||
54 | * |
||
55 | * @throws FieldValidationException |
||
56 | */ |
||
57 | public function infer() |
||
66 | |||
67 | /** |
||
68 | * returns all the input rows got so far with the best cast value for each field. |
||
69 | * |
||
70 | * @return array of arrays of field name => best cast value |
||
71 | */ |
||
72 | public function castRows() |
||
76 | |||
77 | protected $inputRows = []; |
||
78 | protected $castRows = []; |
||
79 | protected $fieldsPopularity = []; |
||
80 | protected $fieldsPopularityObjects = []; |
||
81 | protected $lenient; |
||
82 | |||
83 | /** |
||
84 | * infer field objects for the given row |
||
85 | * raises exception if fails to infer a field. |
||
86 | * |
||
87 | * @param $row array field name => value to infer by |
||
88 | * |
||
89 | * @return array field name => inferred field object |
||
90 | * |
||
91 | * @throws FieldValidationException |
||
92 | */ |
||
93 | protected function inferRow($row) |
||
102 | |||
103 | /** |
||
104 | * @return array |
||
105 | */ |
||
106 | protected function getFieldNames() |
||
112 | |||
113 | /** |
||
114 | * finds the best inferred fields for the given field name according to the popularity |
||
115 | * also updates the castRows array with the latest cast values. |
||
116 | * |
||
117 | * @param $fieldName |
||
118 | * @param $fieldTypesPopularity |
||
119 | * |
||
120 | * @return BaseField|null |
||
121 | */ |
||
122 | protected function inferField($fieldName, $fieldTypesPopularity) |
||
147 | } |
||
148 |