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
23 changes: 23 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Tests
on: [ pull_request ]

jobs:
unit:
name: PHPUnit
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.4
coverage: none

- name: Download dependencies
uses: ramsey/composer-install@v2

- name: PHPUnit (unit tests)
run: ./vendor/bin/phpunit
25 changes: 4 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,15 @@ Via Composer
$ composer require jongotlin/identity-number-bundle
```

```php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new JGI\IdentityNumberValidatorBundle\IdentityNumberValidatorBundle(),
}
}
}
```

## Usage
```php
use JGI\IdentityNumberValidatorBundle\Validator\Constraints as IdentityNumberAssert;

/**
* @IdentityNumberAssert\IdentityNumber(allowCoordinationNumber=true)
*/
private $identityNumber;
#[IdentityNumberAssert\IdentityNumber(allowCoordinationNumber=true)]
private ?string $identityNumber;

/**
* @IdentityNumberAssert\OrganizationNumber(allowPersonalIdNumber=true, allowCoordinationNumber=true)
*/
private $organizationNumber;
#[IdentityNumberAssert\OrganizationNumber(allowPersonalIdNumber=true, allowCoordinationNumber=true)]
private ?string $organizationNumber;
```

Available options are
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
}
},
"require": {
"php": "^7.0|^8.0",
"symfony/framework-bundle": "^2.7|^3.0|^4.0|^5.0|^6.0",
"byrokrat/id": "^1.0"
"php": "^8.2",
"symfony/framework-bundle": "^7.0|^8.0",
"byrokrat/id": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/validator": "^6.0"
"phpunit/phpunit": "^12.5",
"symfony/validator": "^8.0"
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<testsuites>
<testsuite name="Project Test Suite">
<directory>Tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/IdentityNumberValidatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;

class IdentityNumberValidatorExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $config, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
45 changes: 18 additions & 27 deletions src/Validator/Constraints/IdentityNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,26 @@

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
#[\Attribute]
class IdentityNumber extends Constraint
{
/**
* @var string
*/
public $message = 'This value is not a valid identity number.';

/**
* @var string
*/
public $messageWithCoordinationNumber = 'This value is not a valid identity number or coordination number.';

/**
* @var bool
*/
public $allowCoordinationNumber = false;

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

/**
* @return string
*/
public function validatedBy()
public string $message = 'This value is not a valid identity number.';

public string $messageWithCoordinationNumber = 'This value is not a valid identity number or coordination number.';

public bool $allowCoordinationNumber = false;

public bool $strict = false;

public function __construct(?string $message = null, ?string $messageWithCoordinationNumber = null, ?bool $allowCoordinationNumber = null, ?bool $strict = null)
{
$this->message = $message ?? $this->message;
$this->messageWithCoordinationNumber = $messageWithCoordinationNumber ?? $this->messageWithCoordinationNumber;
$this->allowCoordinationNumber = $allowCoordinationNumber ?? $this->allowCoordinationNumber;
$this->strict = $strict ?? $this->strict;
}

public function validatedBy(): string
{
return 'jgi.validator.identity_number';
}
Expand Down
35 changes: 10 additions & 25 deletions src/Validator/Constraints/IdentityNumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,22 @@
use byrokrat\id\PersonalIdFactory;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class IdentityNumberValidator extends ConstraintValidator
{
/**
* @var PersonalIdFactory
*/
protected $personalIdFactory;

/**
* @var CoordinationIdFactory
*/
protected $coordinationIdFactory;

/**
* @param PersonalIdFactory $personalIdFactory
* @param CoordinationIdFactory $coordinationIdFactory
*/
public function __construct(PersonalIdFactory $personalIdFactory, CoordinationIdFactory $coordinationIdFactory)
{
$this->personalIdFactory = $personalIdFactory;
$this->coordinationIdFactory = $coordinationIdFactory;
public function __construct(
private readonly PersonalIdFactory $personalIdFactory,
private readonly CoordinationIdFactory $coordinationIdFactory
) {
}

/**
* @param mixed $value
* @param Constraint|IdentityNumber $constraint
*
* @return void
*/
public function validate($value, Constraint $constraint)
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof IdentityNumber) {
throw new UnexpectedTypeException($constraint, IdentityNumber::class);
}

if (null === $value || '' === $value) {
return;
}
Expand Down
45 changes: 18 additions & 27 deletions src/Validator/Constraints/OrganizationNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,26 @@

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
#[\Attribute]
class OrganizationNumber extends Constraint
{
/**
* @var string
*/
public $message = 'This value is not a valid organization number.';

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

/**
* @var bool
*/
public $allowPersonalIdNumber = false;

/**
* @var bool
*/
public $allowCoordinationNumber = false;

/**
* @return string
*/
public function validatedBy()
public string $message = 'This value is not a valid organization number.';

public bool $strict = false;

public bool $allowPersonalIdNumber = false;

public bool $allowCoordinationNumber = false;

public function __construct(?string $message = null, ?bool $strict = null, ?bool $allowPersonalIdNumber = null, ?bool $allowCoordinationNumber = null)
{
$this->message = $message ?? $this->message;
$this->strict = $strict ?? $this->strict;
$this->allowPersonalIdNumber = $allowPersonalIdNumber ?? $this->allowPersonalIdNumber;
$this->allowCoordinationNumber = $allowCoordinationNumber ?? $this->allowCoordinationNumber;
}

public function validatedBy(): string
{
return 'jgi.validator.organization_number';
}
Expand Down
43 changes: 10 additions & 33 deletions src/Validator/Constraints/OrganizationNumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,23 @@
use byrokrat\id\PersonalIdFactory;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class OrganizationNumberValidator extends ConstraintValidator
{
/**
* @var OrganizationIdFactory
*/
protected $organizationIdFactory;

/**
* @var PersonalIdFactory|null
*/
protected $personalIdFactory;

/**
* @var CoordinationIdFactory|null
*/
protected $coordinationIdFactory;

/**
* @param OrganizationIdFactory $factory
*/
public function __construct(
OrganizationIdFactory $organizationIdFactory,
PersonalIdFactory $personalIdFactory = null,
CoordinationIdFactory $coordinationIdFactory = null
)
{
$this->organizationIdFactory = $organizationIdFactory;
$this->personalIdFactory = $personalIdFactory;
$this->coordinationIdFactory = $coordinationIdFactory;
private readonly OrganizationIdFactory $organizationIdFactory,
private readonly ?PersonalIdFactory $personalIdFactory = null,
private readonly ?CoordinationIdFactory $coordinationIdFactory = null
) {
}

/**
* @param mixed $value
* @param Constraint|OrganizationNumber $constraint
*
* @return void
*/
public function validate($value, Constraint $constraint)
public function validate($value, Constraint $constraint): void
{
if (!$constraint instanceof OrganizationNumber) {
throw new UnexpectedTypeException($constraint, OrganizationNumber::class);
}

if (null === $value || '' === $value) {
return;
}
Expand Down
Loading
Loading