forked from Sylius/AdminOrderCreationPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
|
||
sylius.behat.context.transform.lexical: | ||
public: true | ||
class: Tests\Sylius\AdminOrderCreationPlugin\Behat\Context\Transform\LexicalContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\AdminOrderCreationPlugin\Behat\Context\Transform; | ||
|
||
use Behat\Behat\Context\Context; | ||
|
||
final class LexicalContext implements Context | ||
{ | ||
/** | ||
* @Transform /^"(\-)?(?:€|£|¥|\$)([0-9,]+(\.[0-9]*)?)"$/ | ||
*/ | ||
public function getPriceFromString(string $sign, string $price): int | ||
{ | ||
$this->validatePriceString($price); | ||
|
||
$price = str_replace(',', '', $price); | ||
$price = (int) round((float) $price * 100, 2); | ||
|
||
if ('-' === $sign) { | ||
$price *= -1; | ||
} | ||
|
||
return $price; | ||
} | ||
|
||
/** | ||
* @Transform /^"((?:\d+\.)?\d+)%"$/ | ||
*/ | ||
public function getPercentageFromString(string $percentage): float | ||
{ | ||
return (float) $percentage / 100; | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
private function validatePriceString(string $price): void | ||
{ | ||
if (!preg_match('/^.*\.\d{2}$/', $price)) { | ||
throw new \InvalidArgumentException('The price string should have exactly 2 decimal digits.'); | ||
} | ||
|
||
if (!preg_match('/^\d{1,3}(,\d{3})*\.\d{2}$/', $price)) { | ||
throw new \InvalidArgumentException('Thousands and larger numbers should be separated by commas.'); | ||
} | ||
} | ||
} |