Skip to content

Commit

Permalink
Moved code to PHP 8.1. Added PHPStan level 8. Replaced PHPUnit with Pest
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmiu committed Nov 17, 2023
1 parent 8bd3f8d commit 4f08d96
Show file tree
Hide file tree
Showing 114 changed files with 3,027 additions and 3,542 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
build:
strategy:
matrix:
php: ['7.4', '8.0', '8.1', '8.2']
php: ['8.1', '8.2', '8.3']
include:
- php: '8.0'
- php: '8.1'
send-to-scrutinizer: 'yes'

runs-on: ubuntu-latest
Expand Down Expand Up @@ -47,4 +47,5 @@ jobs:
- name: Run test suite
run: |
mkdir -p build/logs
vendor/bin/phpunit -c tests/phpunit.xml
composer run stan
composer run test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ php-cs-fixer.phar
phpcbf.phar
phpcs.phar
phpmd.phar
.phpunit.result.cache
.php_cs.cache
84 changes: 44 additions & 40 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
{
"name": "siriusphp/validation",
"description": "Data validation library. Validate arrays, array objects, domain models etc using a simple API. Easily add your own validators on top of the already dozens built-in validation rules",
"type": "library",
"license": "MIT",
"keywords": [
"form",
"validation",
"sanitization",
"security",
"modeling"
],
"authors": [
{
"name": "Adrian Miu",
"email": "adrian@adrianmiu.ro"
}
],
"require": {
"php": "^7.2|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5"
},
"autoload": {
"psr-4": {
"Sirius\\Validation\\": "src/"
}
},
"scripts": {
"cs": [
"php phpcs.phar --standard=PSR2 ./src"
"name": "siriusphp/validation",
"description": "Data validation library. Validate arrays, array objects, domain models etc using a simple API. Easily add your own validators on top of the already dozens built-in validation rules",
"type": "library",
"license": "MIT",
"keywords": [
"form",
"validation",
"sanitization",
"security",
"modeling"
],
"md": [
"php phpmd.phar ./src xml phpmd.xml"
"authors": [
{
"name": "Adrian Miu",
"email": "adrian@adrianmiu.ro"
}
],
"cbf": [
"php phpcbf.phar ./src --standard=PSR2 -w"
],
"csfix": [
"php php-cs-fixer.phar fix ./src --rules=@PSR2"
]
}
"require": {
"php": ">=8.0"
},
"autoload": {
"psr-4": {
"Sirius\\Validation\\": "src/"
}
},
"scripts": {
"stan": [
"php vendor/bin/phpstan analyse"
],
"csfix": [
"tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --standard=PSR-2 src"
],
"test": [
"php vendor/bin/pest"
]
},
"require-dev": {
"pestphp/pest": "*",
"phpstan/phpstan": "^1.10",
"pestphp/pest-plugin-drift": "^2.5"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
checkGenericClassInNonGenericObjectType: false
paths:
- src
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
17 changes: 10 additions & 7 deletions src/DataWrapper/ArrayWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
namespace Sirius\Validation\DataWrapper;

use Sirius\Validation\Util\Arr;
use Sirius\Validation\DataWrapper\WrapperInterface;

class ArrayWrapper implements WrapperInterface
{

/**
* @var array
* @var array<string,mixed>
*/
protected $data = [];
protected array $data = [];

/**
* @param array|\ArrayObject|object $data
* @param array<string,mixed>|\ArrayObject|object $data
*
* @throws \InvalidArgumentException
*/
public function __construct($data = [])
public function __construct(mixed $data)
{
if (is_object($data)) {
if ($data instanceof \ArrayObject) {
Expand All @@ -28,19 +27,23 @@ public function __construct($data = [])
$data = $data->toArray();
}
}
if (! is_array($data)) {
if (!is_array($data)) {
throw new \InvalidArgumentException('Data passed to validator is not an array or an ArrayObject');
}
$this->data = $data;
}

public function getItemValue(string $item)
public function getItemValue(string $item): mixed
{
return Arr::getByPath($this->data, $item);
}

/**
* @return array<string,mixed>
*/
public function getItemsBySelector(string $selector): array
{
return Arr::getBySelector($this->data, $selector);
}

}
8 changes: 2 additions & 6 deletions src/DataWrapper/WrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ interface WrapperInterface
/**
* Get value from the data container using the path
*
* @param string $item
*
* @return mixed
*/
public function getItemValue(string $item);

/**
* Get items by selector
*
* @param string $selector
*
* @return array
* @return array<string,mixed>
*/
public function getItemsBySelector(string $selector);
public function getItemsBySelector(string $selector): array;
}
35 changes: 24 additions & 11 deletions src/ErrorMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,41 @@

class ErrorMessage
{
protected $template = 'Invalid';
protected $variables = [];
protected string $template = 'Invalid';

public function __construct($template = '', $variables = [])
/**
* @var array<string, mixed>
*/
protected array $variables = [];

/**
* @param array<string, mixed> $variables
*/
public function __construct(string $template = '', array $variables = [])
{
$this->setTemplate($template)
->setVariables($variables);
->setVariables($variables);
}

public function setTemplate($template)
public function setTemplate(string $template): self
{
$template = trim((string) $template);
$template = trim($template);
if ($template) {
$this->template = (string) $template;
$this->template = $template;
}

return $this;
}

public function getTemplate()
public function getTemplate(): string
{
return $this->template;
}

public function setVariables($variables = [])
/**
* @param array<string, mixed> $variables
*/
public function setVariables(array $variables = []): self
{
foreach ($variables as $k => $v) {
$this->variables[$k] = $v;
Expand All @@ -38,7 +48,10 @@ public function setVariables($variables = [])
return $this;
}

public function getVariables()
/**
* @return array<string, mixed>
*/
public function getVariables(): array
{
return $this->variables;
}
Expand All @@ -48,7 +61,7 @@ public function __toString()
$result = $this->template;
foreach ($this->variables as $k => $v) {
if (strpos($result, "{{$k}}") !== false) {
$result = str_replace("{{$k}}", (string) $v, (string) $result);
$result = str_replace("{{$k}}", (string)$v, (string)$result);
}
}

Expand Down
Loading

0 comments on commit 4f08d96

Please sign in to comment.