Skip to content

Commit

Permalink
Improved handling for empty values in Laravel (#9)
Browse files Browse the repository at this point in the history
* Improve handling on Laravel-validations

* Add PHP 8.1 to test matrix

* Update run-tests.yml
  • Loading branch information
olssonm authored Jul 19, 2021
1 parent a09bd8e commit 161150a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ $validator = Validator::make($request->all(), [
]);
```

Implicit validation

For the validator to run when the social security/organizational number is missing or an empty string (*note*, does not apply to `null`) you will need to implicitly state so with a required rule, i.e:

```php
'organization_number' => [
'required_with:company_name',
'entity:organization',
],
```

or

```php
'organization_number' => [
'required',
'entity',
],
```

## Attributes

### Person
Expand Down
2 changes: 1 addition & 1 deletion src/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Organization
/**
* Constructor
*
* @param string $orgNo
* @param mixed $orgNo
*/
public function __construct(string $orgNo)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Person
/**
* Constructor
*
* @param string $ssn
* @param mixed $ssn
* @param bool $allowCoordinationNumbers
*/
public function __construct(string $ssn, $allowCoordinationNumbers = true)
Expand Down
18 changes: 8 additions & 10 deletions src/SwedishEntityServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Olssonm\SwedishEntity;

use Illuminate\Support\ServiceProvider;
use Olssonm\SwedishEntity\Exceptions\DetectException;

class SwedishEntityServiceProvider extends ServiceProvider
{
Expand All @@ -17,19 +16,18 @@ public function boot()
$this->app['validator']->extend('entity', function ($attribute, $value, $parameters): bool {

$type = isset($parameters[0]) ? $parameters[0] : 'any';

$object = null;

if ($type === 'any') {
try {
try {
if ($type === 'any') {
$object = Entity::detect($value);
} catch (DetectException $exception) {
return false;
} elseif ($type === 'person') {
$object = new Person($value);
} elseif ($type === 'organization') {
$object = new Organization($value);
}
} elseif ($type === 'person') {
$object = new Person($value);
} elseif ($type === 'organization') {
$object = new Organization($value);
} catch (\Throwable $exception) {
return false;
}

return $object->valid();
Expand Down
34 changes: 32 additions & 2 deletions tests/SwedishEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use Olssonm\SwedishEntity\Exceptions\DetectException;
use Olssonm\SwedishEntity\Exceptions\OrganizationException;
use Olssonm\SwedishEntity\Exceptions\PersonException;
use Olssonm\SwedishEntity\Helpers\Cleaner;
use Personnummer\Personnummer;

class SwedishEntityTest extends \Orchestra\Testbench\TestCase
{
Expand Down Expand Up @@ -233,6 +231,28 @@ public function testLaravelValidator()
}
}

/** @test */
public function testLaravelValidatorImplicit()
{
if (class_exists(Validator::class)) {
$this->assertTrue($this->validateLaravel('', 'organization'));
$this->assertTrue($this->validateLaravel('', 'any'));
$this->assertTrue($this->validateLaravel('', 'person'));

$this->assertFalse($this->validateLaravel(null, 'organization'));
$this->assertFalse($this->validateLaravel(null, 'any'));
$this->assertFalse($this->validateLaravel(null, 'person'));

$this->assertFalse($this->validateLaravelImplicit('', 'organization'));
$this->assertFalse($this->validateLaravelImplicit('', 'any'));
$this->assertFalse($this->validateLaravelImplicit('', 'person'));

$this->assertFalse($this->validateLaravelImplicit(null, 'organization'));
$this->assertFalse($this->validateLaravelImplicit(null, 'any'));
$this->assertFalse($this->validateLaravelImplicit(null, 'person'));
}
}

/** @test */
public function testLaravelValidatorWithMessage()
{
Expand Down Expand Up @@ -286,6 +306,16 @@ private function validateLaravel($number, $type = null)
return $validator->passes();
}

private function validateLaravelImplicit($number, $type = null)
{
$data = ['number' => $number];
$validator = Validator::make($data, [
'number' => sprintf('required|entity:organization')
]);

return $validator->passes();
}

private function validateLaravelMessage($number, $type = null, $message = null)
{
$data = ['number' => $number];
Expand Down

0 comments on commit 161150a

Please sign in to comment.