Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Model/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ class Field implements FieldTypes
/** @var FieldValidator */
protected $FieldValidator;

/** @var array<callable> */
protected $customValidators = [];

/** @var array<string> */
private $errors = [];

public function __construct()
{
$this->FieldValidator = new FieldValidator();
$this->initValidators();
}

public function getValue()
Expand All @@ -61,6 +65,11 @@ public function getName()
return $this->name;
}

public function addValidator(callable $callback)
{
$this->customValidators[] = $callback;
}

public function setValue($value)
{
$this->checkMaximum($value);
Expand All @@ -70,6 +79,9 @@ public function setValue($value)
if (!is_null($value)) {
$this->checkValue($value);
}
if (!empty($this->customValidators)) {
$this->checkCustomValidators($value);
}
if (!empty($this->errors)) {
throw new InvalidArgumentException(print_r($this->errors, true));
}
Expand Down Expand Up @@ -136,4 +148,27 @@ public function checkValue($value)
);
}
}

protected function initValidators() {}

private function checkCustomValidators($value)
{
foreach ($this->customValidators as $validator) {
$result = $validator($value, $this);

if (!$result instanceof FieldValidationResultInterface) {
throw new InvalidArgumentException(
'Custom validator must return an instance of FieldValidationResultInterface.'
);
}

if (!$result->isValid()) {
$this->errors[] = sprintf(
'Validation failed for field %s: %s',
$this->name,
(string) $result->getMessage()
);
}
}
}
}
30 changes: 30 additions & 0 deletions src/Model/Fields/FieldValidationResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tpay\OpenApi\Model\Fields;

class FieldValidationResult implements FieldValidationResultInterface
{
/** @var bool */
private $valid;

/** @var null|string */
private $message;

public function __construct($valid, $message = null)
{
$this->valid = $valid;
$this->message = $message;
}

/** @return bool */
public function isValid()
{
return $this->valid;
}

/** @return null|string */
public function getMessage()
{
return $this->message;
}
}
12 changes: 12 additions & 0 deletions src/Model/Fields/FieldValidationResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tpay\OpenApi\Model\Fields;

interface FieldValidationResultInterface
{
/** @return bool */
public function isValid();

/** @return null|string */
public function getMessage();
}
4 changes: 2 additions & 2 deletions src/Model/Fields/FieldValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FieldValidator implements FieldTypes
*/
public function isTooLong($maxLength, $value)
{
return (bool) (strlen($value) > $maxLength);
return (bool) (strlen((string) $value) > $maxLength);
}

/**
Expand All @@ -23,7 +23,7 @@ public function isTooLong($maxLength, $value)
*/
public function isTooShort($minLength, $value)
{
return (bool) (strlen($value) < $minLength);
return (bool) (strlen((string) $value) < $minLength);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Model/Fields/Payer/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tpay\OpenApi\Model\Fields\Payer;

use Tpay\OpenApi\Model\Fields\Field;
use Tpay\OpenApi\Model\Fields\FieldValidationResult;

/**
* @method getValue(): string
Expand All @@ -13,5 +14,15 @@ class IP extends Field
protected $type = self::STRING;
protected $minLength = 3;
protected $maxLength = 255;
protected $pattern = '^([0-9]{1,3}\.){3}[0-9]{1,3}$';

protected function initValidators()
{
$this->addValidator(function ($value) {
if (false === filter_var($value, FILTER_VALIDATE_IP)) {
return new FieldValidationResult(false, 'Invalid IP address.');
}

return new FieldValidationResult(true);
});
}
}
58 changes: 58 additions & 0 deletions tests/Model/Fields/Payer/IpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Model\Fields\Payer;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Tpay\OpenApi\Model\Fields\Payer\IP;

class IpTest extends TestCase
{
public function testIpv4()
{
$ip = new Ip();
$ip->setValue('127.0.0.1');

$this->assertEquals('127.0.0.1', $ip->getValue());
}

public function testIpv6()
{
$ip = new Ip();
$ip->setValue('2001:db8::1');

$this->assertEquals('2001:db8::1', $ip->getValue());
}

public function testInvalidIp()
{
$ip = new Ip();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Validation failed for field Tpay\OpenApi\Model\Fields\Payer\IP: Invalid IP address.');

$ip->setValue('TEST123');
}

public function testNull()
{
$ip = new Ip();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Value of field Tpay\OpenApi\Model\Fields\Payer\IP is too short. Min required 3');
$this->expectExceptionMessage('Validation failed for field Tpay\OpenApi\Model\Fields\Payer\IP: Invalid IP address.');

$ip->setValue(null);
}

public function testWrongType()
{
$ip = new Ip();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Value type of field Tpay\OpenApi\Model\Fields\Payer\IP is invalid. Should be string type');
$this->expectExceptionMessage('Validation failed for field Tpay\OpenApi\Model\Fields\Payer\IP: Invalid IP address.');

$ip->setValue(123212312321);
}
}
5 changes: 5 additions & 0 deletions tests/Model/ModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use Tpay\OpenApi\Model\Fields\FieldValidationResult;

class ModelsTest extends TestCase
{
Expand Down Expand Up @@ -42,6 +43,10 @@ public static function dataModel()
);
$className = str_replace('/', '\\', $className);

if (FieldValidationResult::class === $className) {
continue;
}

yield $className => [$className];
}
}
Expand Down