Skip to content

Commit

Permalink
Add methods to sanitize inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
z38 committed Mar 25, 2018
1 parent 0956b42 commit 01e4b06
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/Z38/SwissPayment/StructuredPostalAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ public function __construct($street, $buildingNo, $postCode, $town, $country = '
$this->country = Text::assertCountryCode($country);
}

/**
* Creates a new instance after sanitizing all inputs
*
* @param string|null $street Street name or null
* @param string|null $buildingNo Building number or null
* @param string $postCode Postal code
* @param string $town Town name
* @param string $country Country code (ISO 3166-1 alpha-2)
*
* @return StructuredPostalAddress
*/
public static function sanitize($street, $buildingNo, $postCode, $town, $country = 'CH')
{
return new self(
Text::sanitizeOptional($street, 70),
Text::sanitizeOptional($buildingNo, 16),
Text::sanitize($postCode, 16),
Text::sanitize($town, 35),
$country
);
}

/**
* {@inheritdoc}
*/
Expand Down
63 changes: 54 additions & 9 deletions src/Z38/SwissPayment/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,47 @@
use DOMDocument;
use InvalidArgumentException;

/**
* @internal
*/
class Text
{
const TEXT_CH = '/^[A-Za-z0-9 .,:\'\/()?+\-!"#%&*;<>÷=@_$£[\]{}\` ́~àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ]*$/u';
const TEXT_SWIFT = '/^[A-Za-z0-9 .,:\'\/()?+\-]*$/';
const TEXT_NON_CH = '/[^A-Za-z0-9 .,:\'\/()?+\-!"#%&*;<>÷=@_$£[\]{}\` ́~àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ]+/u';
const TEXT_NON_SWIFT = '/[^A-Za-z0-9 .,:\'\/()?+\-]+/';

/**
* Sanitizes and trims a string to conform to the Swiss character
* set.
*
* @param string|null $input
* @param int $maxLength
*
* @return string The sanitized string
*/
public static function sanitize($input, $maxLength)
{
$input = preg_replace('/\s+/', ' ', (string) $input);
$input = trim(preg_replace(self::TEXT_NON_CH, '', $input));

return function_exists('mb_substr') ? mb_substr($input, 0, $maxLength, 'UTF-8') : substr($input, 0, $maxLength);
}

/**
* Sanitizes and trims a string to conform to the Swiss character
* set.
*
* @param string|null $input
* @param int $maxLength
*
* @return string|null The sanitized string or null if it is empty.
*/
public static function sanitizeOptional($input, $maxLength)
{
$sanitized = self::sanitize($input, $maxLength);

return $sanitized !== '' ? $sanitized : null;
}

/**
* @internal
*/
public static function assertOptional($input, $maxLength)
{
if ($input === null) {
Expand All @@ -22,21 +55,30 @@ public static function assertOptional($input, $maxLength)
return self::assert($input, $maxLength);
}

/**
* @internal
*/
public static function assert($input, $maxLength)
{
return self::assertPattern($input, $maxLength, self::TEXT_CH);
return self::assertNotPattern($input, $maxLength, self::TEXT_NON_CH);
}

/**
* @internal
*/
public static function assertIdentifier($input)
{
$input = self::assertPattern($input, 35, self::TEXT_SWIFT);
$input = self::assertNotPattern($input, 35, self::TEXT_NON_SWIFT);
if ($input[0] === '/' || strpos($input, '//') !== false) {
throw new InvalidArgumentException('The identifier contains unallowed slashes.');
}

return $input;
}

/**
* @internal
*/
public static function assertCountryCode($input)
{
if (!preg_match('/^[A-Z]{2}$/', $input)) {
Expand All @@ -46,19 +88,22 @@ public static function assertCountryCode($input)
return $input;
}

protected static function assertPattern($input, $maxLength, $pattern)
protected static function assertNotPattern($input, $maxLength, $pattern)
{
$length = function_exists('mb_strlen') ? mb_strlen($input, 'UTF-8') : strlen($input);
if (!is_string($input) || $length === 0 || $length > $maxLength) {
throw new InvalidArgumentException(sprintf('The string can not be empty or longer than %d characters.', $maxLength));
}
if (!preg_match($pattern, $input)) {
if (preg_match($pattern, $input)) {
throw new InvalidArgumentException('The string contains invalid characters.');
}

return $input;
}

/**
* @internal
*/
public static function xml(DOMDocument $doc, $tag, $content)
{
$element = $doc->createElement($tag);
Expand Down
18 changes: 18 additions & 0 deletions src/Z38/SwissPayment/UnstructuredPostalAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public function __construct($addressLine1 = null, $addressLine2 = null, $country
$this->country = Text::assertCountryCode($country);
}

/**
* Creates a new instance after sanitizing all inputs
*
* @param string $addressLine1 Street name and house number
* @param string $addressLine2 Postcode and town
* @param string $country Country code (ISO 3166-1 alpha-2)
*
* @return UnstructuredPostalAddress
*/
public static function sanitize($addressLine1 = null, $addressLine2 = null, $country = 'CH')
{
return new self(
Text::sanitizeOptional($addressLine1, 70),
Text::sanitizeOptional($addressLine2, 70),
$country
);
}

/**
* {@inheritdoc}
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/Z38/SwissPayment/Tests/StructuredPostalAddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Z38\SwissPayment\Tests;

use Z38\SwissPayment\StructuredPostalAddress;

/**
* @coversDefaultClass \Z38\SwissPayment\StructuredPostalAddress
*/
class StructuredPostalAddressTest extends TestCase
{
/**
* @covers ::sanitize
*/
public function testSanitize()
{
$this->assertInstanceOf('Z38\SwissPayment\StructuredPostalAddress', StructuredPostalAddress::sanitize(
'Dorfstrasse',
'',
'Pfaffenschlag bei Waidhofen an der Thaya',
'3834',
'AT'
));
}
}
24 changes: 24 additions & 0 deletions tests/Z38/SwissPayment/Tests/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ public function testAssertCountryCodeUppercase()
Text::assertCountryCode('ch');
}

/**
* @dataProvider sanitizeSamples
*/
public function testSanitize($input, $expected)
{
$this->assertSame($expected, Text::sanitize($input, 3));
}

public function sanitizeSamples()
{
return [
["\t \t", ''],
['°¬◆😀', ''],
[' 中文A B中文C ', 'A B'],
["ä \nÇ \n \nz", 'ä Ç'],
['äääää', 'äää'],
];
}

public function testSanitizeOptional()
{
$this->assertSame(null, Text::sanitizeOptional(" \t ° ° \t", 100));
}

public function testXml()
{
$doc = new DOMDocument();
Expand Down
22 changes: 22 additions & 0 deletions tests/Z38/SwissPayment/Tests/UnstructuredPostalAddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Z38\SwissPayment\Tests;

use Z38\SwissPayment\UnstructuredPostalAddress;

/**
* @coversDefaultClass \Z38\SwissPayment\UnstructuredPostalAddress
*/
class UnstructuredPostalAddressTest extends TestCase
{
/**
* @covers ::sanitize
*/
public function testSanitize()
{
$this->assertInstanceOf('Z38\SwissPayment\UnstructuredPostalAddress', UnstructuredPostalAddress::sanitize(
"Dorf—Strasse 3\n\n",
"8000\tZürich"
));
}
}

0 comments on commit 01e4b06

Please sign in to comment.