Skip to content

Commit

Permalink
chore: Convert some parts of Date validator to php8 syntax and add so…
Browse files Browse the repository at this point in the history
…me tests to check userland retrocompatibility.

Signed-off-by: codisart <[email protected]>
  • Loading branch information
codisart committed Feb 21, 2024
1 parent 5c3fc8c commit 78d2570
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 15 deletions.
3 changes: 0 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,6 @@
<PossiblyInvalidArgument>
<code>$options</code>
</PossiblyInvalidArgument>
<PossiblyUndefinedVariable>
<code>$temp</code>
</PossiblyUndefinedVariable>
</file>
<file src="src/DateStep.php">
<ArgumentTypeCoercion>
Expand Down
26 changes: 14 additions & 12 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __construct($options = [])
$options = iterator_to_array($options);
} elseif (! is_array($options)) {
$options = func_get_args();
$temp = [];
$temp['format'] = array_shift($options);
$options = $temp;
}
Expand Down Expand Up @@ -152,22 +153,23 @@ protected function convertToDateTime($param, $addErrors = true)
return DateTime::createFromImmutable($param);
}

$type = gettype($param);
switch ($type) {
case 'string':
return $this->convertString($param, $addErrors);
case 'integer':
return $this->convertInteger($param);
case 'double':
return $this->convertDouble($param);
case 'array':
return $this->convertArray($param, $addErrors);
}
return match (gettype($param)) {
'string' => $this->convertString($param, $addErrors),
'integer' => $this->convertInteger($param),
'double' => $this->convertDouble($param),
'array' => $this->convertArray($param),
default => $this->addInvalidTypeError($addErrors),
};
}

/**
* @return false
*/
private function addInvalidTypeError(bool $addErrors)
{
if ($addErrors) {
$this->error(self::INVALID);
}

return false;
}

Expand Down
19 changes: 19 additions & 0 deletions test/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTime;
use DateTimeImmutable;
use Laminas\Validator\Date;
use LaminasTest\Validator\TestAsset\CustomDate;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -195,4 +196,22 @@ public function testConstructorWithFormatParameter(): void

self::assertSame($format, $validator->getFormat());
}

public function testAddErrorsParam(): void
{
$customDateValidator = new Date();
/** @psalm-suppress InvalidArgument */
self::assertFalse($customDateValidator->isValid(new stdClass()));
self::assertArrayHasKey('dateInvalid', $customDateValidator->getMessages());
}

public function testExtensionDateValidator(): void
{
$customDateValidator = new CustomDate();
self::assertTrue($customDateValidator->isValid(16757802.07));

/** @psalm-suppress InvalidArgument */
self::assertFalse($customDateValidator->isValid(new stdClass()));
self::assertArrayNotHasKey('dateInvalid', $customDateValidator->getMessages());
}
}
55 changes: 55 additions & 0 deletions test/TestAsset/CustomDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Validator\TestAsset;

use DateTime;
use DateTimeInterface;
use Laminas\Validator\Date;

final class CustomDate extends Date
{
/** @var string */
protected $format = self::FORMAT_DEFAULT;

/** @var bool */
protected $strict = false;

/**
* @param string|numeric|array|DateTimeInterface $value
* @return bool
*/
public function isValid($value)
{
return parent::isValid($value);
}

/**
* @param string|numeric|array|DateTimeInterface $param
* @param bool $addErrors
* @return bool|DateTime
*/
protected function convertToDateTime($param, $addErrors = true)
{
return parent::convertToDateTime($param, false);
}

/**
* @param integer $value
* @return false|DateTime
*/
protected function convertInteger($value)
{
return parent::convertInteger($value);
}

/**
* @param double $value
* @return false|DateTime
*/
protected function convertDouble($value)
{
return parent::convertDouble($value * 100);
}
}

0 comments on commit 78d2570

Please sign in to comment.