Skip to content

Commit

Permalink
exec method refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Apr 28, 2022
1 parent 2f472c7 commit 1e82fe1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ $input = [
];

$schemator = SchematorFactory::create();
$output = $schemator->exec($schema, $input);
$output = $schemator->exec($input, $schema);

print_r($output);
/* Array
Expand Down Expand Up @@ -158,7 +158,7 @@ $input = [
];

$schemator = SchematorFactory::create();
$output = $schemator->exec($schema, $input);
$output = $schemator->exec($input, $schema);

print_r($output);
/*
Expand Down Expand Up @@ -203,7 +203,7 @@ $input = [
'numbers' => [-1, 10, 5, 22, -10, 0, 35, 7, 8, 9, 0],
];

$output = $schemator->exec([
$output = $schemator->exec($input, [
'positive' => [
'numbers',
['filter', [['>', 0]]],
Expand All @@ -220,7 +220,7 @@ $output = $schemator->exec([
['filter', [['<', 22]]],
['sort'],
],
], $input);
]);

print_r($output);
/*
Expand Down Expand Up @@ -255,7 +255,7 @@ Array
)
*/

$output = $schemator->exec([
$output = $schemator->exec($input, [
'number_types' => ['numbers', [
'replace',
[
Expand All @@ -265,7 +265,7 @@ $output = $schemator->exec([
['1-8', 'between', 1, 8],
]
]]
], $input);
]);
print_r($output);
/*
Expand Down Expand Up @@ -312,7 +312,7 @@ $schema = [
'street_names' => ['streets', ['startsWith', 'T'], ['implode', ', ']],
];
$output = $schemator->exec($schema, $input);
$output = $schemator->exec($input, $schema);
print_r($output);
/*
Expand Down
4 changes: 2 additions & 2 deletions src/Schemator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public function __construct(string $pathDelimiter = '.')

/**
* Converts input data with using schema
* @param array $schema schema for converting
* @param array $source input data to convert
* @param array $schema schema for converting
* @return array converted data
* @throws SchematorException
*/
public function exec(array $schema, array $source): array
public function exec(array $source, array $schema): array
{
$result = [];

Expand Down
72 changes: 36 additions & 36 deletions tests/unit/SchematorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testDefaultDelimiter()
'msk_path' => 'country.capitals.msk',
];

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_id' => 'id',
'city_name' => 'name',
'city_street_names' => 'streets.name',
Expand All @@ -65,7 +65,7 @@ public function testDefaultDelimiter()
'raw' => '',
'country_data.country_id' => 'country.id',
'country_data.country_name' => 'country.name',
], $input);
]);

$this->assertEquals(100, $data['city_id']);
$this->assertEquals('Novgorod', $data['city_name']);
Expand All @@ -86,9 +86,9 @@ public function testDefaultDelimiter()
], $data['country_data']);

try {
$schemator->exec([
$schemator->exec($input, [
'city_street_names' => ['streets.name', ['join', ', ']]
], $input);
]);
$this->assertTrue(false);
} catch(SchematorException $e) {
$this->assertEquals(SchematorException::FILTER_NOT_FOUND, $e->getCode());
Expand All @@ -112,24 +112,24 @@ public function testDefaultDelimiter()
return $schemator->getValue($rootSource, $source);
});

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_street_names' => ['streets.name', ['implode', ', ']]
], $input);
]);
$this->assertEquals('Tverskaya, Leninskiy, Tarusskaya', $data['city_street_names']);

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_street_names' => ['streets.name', ['implode', ', '], ['explode', ', ']]
], $input);
]);
$this->assertEquals(['Tverskaya', 'Leninskiy', 'Tarusskaya'], $data['city_street_names']);

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_street_names' => ['streets.name', ['startsWith', 'T'], ['implode', ', ']]
], $input);
]);
$this->assertEquals('Tverskaya, Tarusskaya', $data['city_street_names']);

$data = $schemator->exec([
$data = $schemator->exec($input, [
'msk' => ['msk_path', ['path']]
], $input);
]);
$this->assertEquals('Moscow', $data['msk']);
}

Expand Down Expand Up @@ -172,7 +172,7 @@ public function testSpecificDelimiter()
'msk_path' => 'country/capitals/msk',
];

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_id' => 'id',
'city_name' => 'name',
'city_street_names' => 'streets/name',
Expand All @@ -189,7 +189,7 @@ public function testSpecificDelimiter()
'raw' => '',
'country_data/country_id' => 'country/id',
'country_data/country_name' => 'country/name',
], $input);
]);

$this->assertEquals(100, $data['city_id']);
$this->assertEquals('Novgorod', $data['city_name']);
Expand All @@ -210,9 +210,9 @@ public function testSpecificDelimiter()
], $data['country_data']);

try {
$schemator->exec([
$schemator->exec($input, [
'city_street_names' => ['streets/name', ['join', ', ']]
], $input);
]);
$this->assertTrue(false);
} catch(SchematorException $e) {
$this->assertEquals(SchematorException::FILTER_NOT_FOUND, $e->getCode());
Expand All @@ -236,24 +236,24 @@ public function testSpecificDelimiter()
return $schemator->getValue($rootSource, $source);
});

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_street_names' => ['streets/name', ['implode', ', ']]
], $input);
]);
$this->assertEquals('Tverskaya, Leninskiy, Tarusskaya', $data['city_street_names']);

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_street_names' => ['streets/name', ['implode', ', '], ['explode', ', ']]
], $input);
]);
$this->assertEquals(['Tverskaya', 'Leninskiy', 'Tarusskaya'], $data['city_street_names']);

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_street_names' => ['streets/name', ['startsWith', 'T'], ['implode', ', ']]
], $input);
]);
$this->assertEquals('Tverskaya, Tarusskaya', $data['city_street_names']);

$data = $schemator->exec([
$data = $schemator->exec($input, [
'msk' => ['msk_path', ['path']]
], $input);
]);
$this->assertEquals('Moscow', $data['msk']);
}

Expand Down Expand Up @@ -302,7 +302,7 @@ public function testFactory()
'msk_path' => 'country.capitals.msk',
];

$data = $schemator->exec([
$data = $schemator->exec($input, [
'city_street_names.first' => ['streets.name', ['implode', ', ']],
'city_street_names.second' => ['streets.name', ['implode', ', '], ['explode', ', ']],
'city_street_names.third' => ['streets.name', ['startsWith', 'T'], ['implode', ', ']],
Expand All @@ -312,7 +312,7 @@ public function testFactory()
}]],
'msk' => ['msk_path', ['path']],
'city_street_houses' => ['streets.houses', ['flatten']],
], $input);
]);
$this->assertEquals('Tverskaya, Leninskiy, Tarusskaya', $data['city_street_names']['first']);
$this->assertEquals(['Tverskaya', 'Leninskiy', 'Tarusskaya'], $data['city_street_names']['second']);
$this->assertEquals('Tverskaya, Tarusskaya', $data['city_street_names']['third']);
Expand All @@ -333,7 +333,7 @@ public function testReplaceAndFilter()

$schemator = SchematorFactory::create();

$data = $schemator->exec([
$data = $schemator->exec($input, [
'number_types' => ['numbers', [
'replace',
[
Expand All @@ -343,13 +343,13 @@ public function testReplaceAndFilter()
['1-8', 'between', 1, 8],
]
]]
], $input);
]);

$this->assertEquals([
'<0', '>9', '1-8', '>9', '<0', '=0', '>9', '1-8', '1-8', null, '=0',
], $data['number_types']);

$data = $schemator->exec([
$data = $schemator->exec($input, [
'positive' => [
'numbers',
['filter', [['>', 0]]],
Expand All @@ -366,7 +366,7 @@ public function testReplaceAndFilter()
['filter', [['<', 22]]],
['sort'],
],
], $input);
]);

$this->assertEquals([5, 7, 8, 9, 10, 22, 35], $data['positive']);
$this->assertEquals([-10, -1], $data['negative']);
Expand All @@ -390,38 +390,38 @@ public function testFormat()
}, 'Y-m-d']]
];

$output = $schemator->exec($schema, $input);
$output = $schemator->exec($input, $schema);
$this->assertEquals('2022-04-28', $output['date']);

$schema = [
'date' => ['date', ['date', 'Y-m-d']]
];
$output = $schemator->exec($schema, $input);
$output = $schemator->exec($input, $schema);
$this->assertEquals('2022-04-28', $output['date']);

$schema = [
'date' => ['date', ['date', 'Y-m-d H:i']]
];
$output = $schemator->exec($schema, $input);
$output = $schemator->exec($input, $schema);
$this->assertEquals('2022-04-28 19:01', $output['date']);

$schema = [
'date' => ['date', ['date', 'Y-m-d H:i', 3]]
];
$output = $schemator->exec($schema, $input);
$output = $schemator->exec($input, $schema);
$this->assertEquals('2022-04-28 19:01', $output['date']);

$schema = [
'date' => ['date', ['date', 'Y-m-d H:i', 0]]
];
$output = $schemator->exec($schema, $input);
$output = $schemator->exec($input, $schema);
$this->assertEquals('2022-04-28 16:01', $output['date']);

$schema = [
'date' => ['date', ['date', ['Y-m-d H:i'], 0]]
];
try {
$schemator->exec($schema, $input);
$schemator->exec($input, $schema);
$this->assertTrue(false);
} catch(SchematorException $e) {
$this->assertEquals(SchematorException::FILTER_ERROR, $e->getCode());
Expand Down

0 comments on commit 1e82fe1

Please sign in to comment.