Skip to content

Commit

Permalink
Merge pull request #26 from ingenerator/include-warden-symfony-validator
Browse files Browse the repository at this point in the history
Include warden-validator-symfony as part of the core package
  • Loading branch information
craig410 authored Sep 25, 2024
2 parents 381679f + 8ffd0a5 commit 367ee72
Show file tree
Hide file tree
Showing 26 changed files with 322 additions and 48 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ jobs:
fail-fast: false
matrix:
php_version:
- '8.0'
- '8.1'
- '8.2'
- '8.3'
dependencies:
- 'default'
include:
- php_version: '8.1'
dependencies: 'lowest'
- php_version: '8.2'
dependencies: 'lowest'
- php_version: '8.3'
dependencies: 'lowest'

steps:
- name: Setup PHP
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
### Unreleased

### v2.0.0 (2024-09-25)

* [BREAKING] Remove @Assert docblock annotations from request objects. Migrated to using attributes
* Include a validator with this core package (using symfony) with attribute tagging on request objects
* Drop support for PHP 8.0
* Support PHP 8.3

### v1.2.2 (2022-10-31)

* Fix deprecation on password ->isCorrect() when hash is null - e.g. if a user's password has never been initialised.
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ This isn't in packagist yet : you'll need to add our package repository to your

`$> composer require ingenerator/warden-core`

# Validation

A default validation interface is included, along with an implementation using symfony validator and a factory to create a validator.

## Using attribute based mapping

The warden-core package defines validation mapping by default with attributes on the
various entity and request objects.

## Using alternate mapping

If you want to use an alternate validation mapping method (e.g. yaml files / xml files) you will
need to define the appropriate mappings based on the constraints specified in the warden class
annotations, and populate your validation builder appropriately.

# Contributing

Contributions are welcome but please contact us before you start work on anything to check your
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
}
],
"require": {
"php": "~8.0.0 || ~8.1.0 || ~8.2.0"
"egulias/email-validator": "^4.0",
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"symfony/validator": "^6.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5.5"
Expand All @@ -36,6 +38,7 @@
}
},
"config": {
"preferred-install": "dist"
"preferred-install": "dist",
"sort-packages": true
}
}
6 changes: 2 additions & 4 deletions src/Interactor/ActivateAccountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

use Symfony\Component\Validator\Constraints as Assert;

/** @var Assert $annotations keep me to stop phpstorm deleting the import */

class ActivateAccountRequest extends AbstractRequest implements TokenSignedRequest
{
/**
* @Assert\NotBlank
* @Assert\Regex("/^\w+/")
* @var string
*/
#[Assert\NotBlank]
#[Assert\Regex('/^\w+$/')]
protected $user_id;

/**
Expand Down
10 changes: 4 additions & 6 deletions src/Interactor/ChangeEmailRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@

use Symfony\Component\Validator\Constraints as Assert;

/** @var Assert $annotations keep me to stop phpstorm deleting the import */

class ChangeEmailRequest extends AbstractRequest implements TokenSignedRequest
{

/**
* @Assert\NotBlank
* @Assert\Regex("/^\w+/")
* @var string
*/
#[Assert\NotBlank]
#[Assert\Regex('/^\w+$/')]
protected $user_id;

/**
* @Assert\NotBlank
* @Assert\Email(mode = "strict")
* @var string
*/
#[Assert\NotBlank]
#[Assert\Email(mode: 'strict')]
protected $email;

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Interactor/ChangePasswordRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@

use Symfony\Component\Validator\Constraints as Assert;

/** @var Assert $annotations keep me to stop phpstorm deleting the import */
class ChangePasswordRequest extends AbstractRequest
{

/**
* @Assert\NotBlank
* @var string
*/
#[Assert\NotBlank]
protected $current_password;

/**
* @Assert\Length(min = 8)
* @Assert\NotBlank
* @var string
*/
#[Assert\Length(min: 8)]
#[Assert\NotBlank]
protected $new_password;

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Interactor/EmailVerificationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Ingenerator\Warden\Core\Support\UrlProvider;
use Symfony\Component\Validator\Constraints as Assert;

/** @var Assert $annotations keep me to stop phpstorm deleting the import */
class EmailVerificationRequest extends AbstractRequest
{

Expand All @@ -26,10 +25,10 @@ class EmailVerificationRequest extends AbstractRequest
protected $action;

/**
* @Assert\NotBlank
* @Assert\Email(mode = "strict")
* @var string
*/
#[Assert\NotBlank]
#[Assert\Email(mode: 'strict')]
protected $email;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Interactor/LoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
*/

namespace Ingenerator\Warden\Core\Interactor;
use Symfony\Component\Validator\Constraints as Assert;

use Symfony\Component\Validator\Constraints as Assert;

class LoginRequest extends AbstractRequest
{

/**
* @Assert\NotBlank
* @Assert\Email(mode = "loose")
* @var string
*/
#[Assert\NotBlank]
#[Assert\Email(mode: 'loose')]
protected $email;

/**
Expand Down
10 changes: 4 additions & 6 deletions src/Interactor/PasswordResetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@
namespace Ingenerator\Warden\Core\Interactor;
use Symfony\Component\Validator\Constraints as Assert;

/** @var Assert $annotations keep me to stop phpstorm deleting the import */

class PasswordResetRequest extends AbstractRequest implements TokenSignedRequest
{

/**
* @Assert\NotBlank
* @Assert\Regex("/^\w+/")
* @var string
*/
#[Assert\NotBlank]
#[Assert\Regex('/^\w+$/')]
protected $user_id;

/**
* @Assert\Length(min = 8)
* @Assert\NotBlank
* @var string
*/
#[Assert\Length(min: 8)]
#[Assert\NotBlank]
protected $new_password;

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Interactor/UserRegistrationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@

namespace Ingenerator\Warden\Core\Interactor;


use Ingenerator\Warden\Core\Entity\User;
use Symfony\Component\Validator\Constraints as Assert;

/** @var Assert $annotations keep me to stop phpstorm deleting the import */

class UserRegistrationRequest extends AbstractRequest implements TokenSignedRequest
{
/**
* @Assert\NotBlank
* @Assert\Email(mode = "strict")
* @var string
*/
#[Assert\NotBlank]
#[Assert\Email(mode: 'strict')]
protected $email;

/**
Expand All @@ -27,10 +24,10 @@ class UserRegistrationRequest extends AbstractRequest implements TokenSignedRequ
protected $email_confirmation_token;

/**
* @Assert\Length(min = 8)
* @Assert\NotBlank
* @var string
*/
#[Assert\Length(min: 8)]
#[Assert\NotBlank]
protected $password;

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Validator/SymfonyValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @author Andrew Coulton <andrew@ingenerator.com>
* @licence proprietary
*/

namespace Ingenerator\Warden\Core\Validator;

use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class SymfonyValidator implements Validator
{

public function __construct(protected ValidatorInterface $validator)
{
}

public function validate($object): array
{
$errors = $this->validator->validate($object);
$error_messages = [];
foreach ($errors as $error) {
/** @var ConstraintViolationInterface $error */
$error_messages[$error->getPropertyPath()] = $error->getMessage();
}

return $error_messages;
}
}
20 changes: 20 additions & 0 deletions src/Validator/SymfonyValidatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @author Craig Gosman <craig@ingenerator.com>
* @licence proprietary
*/

namespace Ingenerator\Warden\Core\Validator;

use Symfony\Component\Validator\Validation;

class SymfonyValidatorFactory
{
public static function factory(): SymfonyValidator
{
$builder = Validation::createValidatorBuilder();
$builder->enableAttributeMapping();

return new SymfonyValidator($builder->getValidator());
}
}
2 changes: 1 addition & 1 deletion test/integration/Repository/ArrayUserRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use Ingenerator\Warden\Core\Repository\ArrayUserRepository;

class ArrayUserRepositoryTest extends UserRepositoryTest
class ArrayUserRepositoryTest extends UserRepositoryTestCase
{
/**
* @var \ArrayObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Ingenerator\Warden\Core\Repository\UserRepository;
use PHPUnit\Framework\TestCase;

abstract class UserRepositoryTest extends TestCase
abstract class UserRepositoryTestCase extends TestCase
{
/**
* @var Configuration
Expand Down
Loading

0 comments on commit 367ee72

Please sign in to comment.