Skip to content

Commit 83e7093

Browse files
committed
Some bug fixes
1 parent f486020 commit 83e7093

10 files changed

Lines changed: 96 additions & 10 deletions

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
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.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace root4root\Reshaper;
43

54
require_once 'ReshaperData.php';
File renamed without changes.
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace root4root\Reshaper;
44

5+
use root4root\Reshaper\ReshaperData;
6+
use root4root\Reshaper\ProcessorInterface;
7+
58
class Processor_r implements ProcessorInterface
69
{
710
public $data = null;
@@ -19,7 +22,7 @@ public function requiredCol(array $rule)
1922
foreach ($rule['operations'] AS $key=>$operation) {
2023
$nextcol = $rule['columns'][$key+1];
2124

22-
if ($operation == '&') {
25+
if ($operation == '&' || $operation == '*') {
2326
$result = $result && $this->isEmpty($this->typecast($nextcol, $regexp));
2427
} else {
2528
$result = $result || $this->isEmpty($this->typecast($nextcol, $regexp));
@@ -33,23 +36,40 @@ public function processCol(array $rule)
3336
{
3437
$regexp = $rule['extra'];
3538
$colkey = $rule['columns'][0];
36-
3739
$result = $this->typecast($colkey, $regexp);
40+
41+
//$this->data->setResult($result);
42+
foreach ($rule['operations'] AS $key=>$operation) {
43+
$nextcol = $rule['columns'][$key+1];
44+
$result .= ' ' . $this->typecast($nextcol, $regexp);
45+
}
46+
3847
$this->data->setResult($result);
39-
48+
4049
return true;
4150
}
4251

4352
public function typecast($colkey, $regexp)
4453
{
54+
$result = '';
55+
4556
$rawcol = $this->data->getCol($colkey);
46-
preg_match($regexp, $rawcol, $result);
57+
58+
if (! preg_match($regexp, $rawcol, $matches)) {
59+
return $result;
60+
}
61+
62+
$matchesCount = count($matches);
4763

48-
if (empty($result) || $result[0] == '') {
49-
return '';
64+
if ($matchesCount > 1) {
65+
for ($i=1; $i<$mathesCount; $i++) {
66+
$result .= $matches[$i];
67+
}
5068
} else {
51-
return $result[0];
69+
$result = $matches[0];
5270
}
71+
72+
return $result;
5373
}
5474

5575
public function isEmpty($col = '')
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace root4root\Reshaper;
44

5+
use root4root\Reshaper\ReshaperData;
6+
use root4root\Reshaper\ProcessorInterface;
7+
58
class Processor_s implements ProcessorInterface
69
{
710
public $data = null;
@@ -21,7 +24,7 @@ public function requiredCol(array $rule)
2124
foreach ($rule['operations'] AS $key=>$operation) {
2225
$nextcol = $rule['columns'][$key+1];
2326

24-
if ($operation == '&') {
27+
if ($operation == '&' || $operation == '*') {
2528
$result = $result && $this->isEmpty($this->typecast($nextcol));
2629
} else {
2730
$result = $result || $this->isEmpty($this->typecast($nextcol));

0 commit comments

Comments
 (0)