diff --git a/README.md b/README.md index 57e99d1..1342746 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Organization.php b/src/Organization.php index af3d55d..e5be52d 100644 --- a/src/Organization.php +++ b/src/Organization.php @@ -30,7 +30,7 @@ class Organization /** * Constructor * - * @param string $orgNo + * @param mixed $orgNo */ public function __construct(string $orgNo) { diff --git a/src/Person.php b/src/Person.php index cbe9cae..0c4dfb5 100644 --- a/src/Person.php +++ b/src/Person.php @@ -40,7 +40,7 @@ class Person /** * Constructor * - * @param string $ssn + * @param mixed $ssn * @param bool $allowCoordinationNumbers */ public function __construct(string $ssn, $allowCoordinationNumbers = true) diff --git a/src/SwedishEntityServiceProvider.php b/src/SwedishEntityServiceProvider.php index 11cd190..37646cf 100644 --- a/src/SwedishEntityServiceProvider.php +++ b/src/SwedishEntityServiceProvider.php @@ -3,7 +3,6 @@ namespace Olssonm\SwedishEntity; use Illuminate\Support\ServiceProvider; -use Olssonm\SwedishEntity\Exceptions\DetectException; class SwedishEntityServiceProvider extends ServiceProvider { @@ -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(); diff --git a/tests/SwedishEntityTest.php b/tests/SwedishEntityTest.php index 4447433..ce2353d 100644 --- a/tests/SwedishEntityTest.php +++ b/tests/SwedishEntityTest.php @@ -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 { @@ -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() { @@ -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];