|
1 | | -# reshaper |
| 1 | +### What this all about? |
| 2 | +Imagine that you have an array and at the same time you are trying to create a new one with the custom field from it, including mixing. This might be very helpful if you are working with some data sources, and want to fit the data under a specific format. Let’s say, you have a price list and you need to update your database in accordance with it. To make this possible you need to reshape data to fit database schema. Hence, the name. |
| 3 | + |
| 4 | +### Quick examle |
| 5 | +```php |
| 6 | +$fields = ['(A)i','(B)s', '(B+C)s']; |
| 7 | +$required = ['(A)i', '(G|H|I)r(/^(?:\s*)[1-9,\+]+(?:\s*)$/)']; |
| 8 | +$data = [1, 'PARTNUMBER', 'Part.333', 'Description', 'foo1', 'foo2', '+', 0, 0]; |
| 9 | + |
| 10 | +$obj = new Configurator(); |
| 11 | +$reshaper = new Reshaper($obj->createConfig($fields, $required)); |
| 12 | + |
| 13 | +$result = $reshaper->parseRow($data); |
| 14 | + |
| 15 | +if ($result !== false) { |
| 16 | + print_r($result->getResult()); |
| 17 | +} |
| 18 | +/* |
| 19 | +* Array |
| 20 | +* ( |
| 21 | +* [0] => 1 |
| 22 | +* [1] => PARTNUMBER |
| 23 | +* [2] => PARTNUMBER Part.333 |
| 24 | +* ) |
| 25 | +*/ |
| 26 | + |
| 27 | +``` |
| 28 | +### Explanation |
| 29 | +There are two arrays for configuration. First one is answering the question «What fields are expected to be created with the output array?» Second formulates rules for validation. |
| 30 | + |
| 31 | +##### Fields array syntax ($fields in example) |
| 32 | +###### (column)type(extra) |
| 33 | +Columns can be separated by a special chars: +, -, *, /. |
| 34 | + |
| 35 | +Types from package: |
| 36 | +* f - float |
| 37 | +* i - integer |
| 38 | +* r - regular expression |
| 39 | +* s - string |
| 40 | + |
| 41 | +Extra: some additional configuration, required for ‘r’ processor. Can be used with ‘i’ and ‘f’ by optional. |
| 42 | + |
| 43 | +Each type handles with certain processor, with its own validation and filed rules. For example, processor 's' will concatenate fields, regardless of separator (B+C) or (B*C). Processor 'i' (integer) and 'f' will calculate the result depending on the math sign. |
| 44 | + |
| 45 | +(A)i(30) - get value from column, convert it to integer and increase by 30%. Same operation with the float type. |
| 46 | + |
| 47 | +You can specify columns by number, starting from 1 (first). Example: '(2+3)s' == '(B+C)s' |
| 48 | + |
| 49 | +##### Required array syntax ($required in example) |
| 50 | +###### (column)type(extra) |
| 51 | + |
| 52 | +Columns separators: |,& (or +,* respectevly). |
| 53 | + |
| 54 | +Types: f,i,r,s from package. |
| 55 | + |
| 56 | +(A|B)i - A **or** B must be > 0 |
| 57 | + |
| 58 | +(A&B)s - A **and** B is not empty strings |
| 59 | + |
| 60 | +(G|H)r(regexp) - A **or** B must satisfy regular expression. |
| 61 | + |
| 62 | +**You should specify required array or its equivalents to field list. This can cause a problem with performance.** |
| 63 | + |
| 64 | +### Your own types |
| 65 | +You can define your own data types or override defaults from package. Create class Processor_type, which implements the Processor Interface. |
0 commit comments