-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d73680c
commit 346ff03
Showing
18 changed files
with
225 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Distantmagic\Resonance\Constraint; | ||
|
||
use Distantmagic\Resonance\Constraint; | ||
use Distantmagic\Resonance\ConstraintDefaultValue; | ||
use Distantmagic\Resonance\ConstraintPath; | ||
use Distantmagic\Resonance\ConstraintReason; | ||
use Distantmagic\Resonance\ConstraintResult; | ||
use Distantmagic\Resonance\ConstraintResultStatus; | ||
|
||
final readonly class FilenameConstraint extends Constraint | ||
{ | ||
public function __construct( | ||
?ConstraintDefaultValue $defaultValue = null, | ||
bool $isNullable = false, | ||
bool $isRequired = true, | ||
) { | ||
parent::__construct( | ||
defaultValue: $defaultValue, | ||
isNullable: $isNullable, | ||
isRequired: $isRequired, | ||
); | ||
} | ||
|
||
public function default(mixed $defaultValue): self | ||
{ | ||
return new self( | ||
defaultValue: new ConstraintDefaultValue($defaultValue), | ||
isNullable: $this->isNullable, | ||
isRequired: $this->isRequired, | ||
); | ||
} | ||
|
||
public function nullable(): self | ||
{ | ||
return new self( | ||
defaultValue: $this->defaultValue ?? new ConstraintDefaultValue(null), | ||
isNullable: true, | ||
isRequired: $this->isRequired, | ||
); | ||
} | ||
|
||
public function optional(): self | ||
{ | ||
return new self( | ||
defaultValue: $this->defaultValue, | ||
isNullable: $this->isNullable, | ||
isRequired: false, | ||
); | ||
} | ||
|
||
protected function doConvertToJsonSchema(): array | ||
{ | ||
return [ | ||
'type' => 'string', | ||
'minLength' => 1, | ||
]; | ||
} | ||
|
||
protected function doValidate(mixed $notValidatedData, ConstraintPath $path): ConstraintResult | ||
{ | ||
if (!is_string($notValidatedData) || empty($notValidatedData)) { | ||
return new ConstraintResult( | ||
castedData: $notValidatedData, | ||
path: $path, | ||
reason: ConstraintReason::InvalidDataType, | ||
status: ConstraintResultStatus::Invalid, | ||
); | ||
} | ||
|
||
if (!file_exists($notValidatedData)) { | ||
return new ConstraintResult( | ||
castedData: $notValidatedData, | ||
path: $path, | ||
reason: ConstraintReason::FileNotFound, | ||
status: ConstraintResultStatus::Invalid, | ||
); | ||
} | ||
|
||
if (!is_readable($notValidatedData)) { | ||
return new ConstraintResult( | ||
castedData: $notValidatedData, | ||
path: $path, | ||
reason: ConstraintReason::FileNotReadable, | ||
status: ConstraintResultStatus::Invalid, | ||
); | ||
} | ||
|
||
return new ConstraintResult( | ||
castedData: $notValidatedData, | ||
path: $path, | ||
reason: ConstraintReason::Ok, | ||
status: ConstraintResultStatus::Valid, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Distantmagic\Resonance\Constraint; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @coversNothing | ||
* | ||
* @internal | ||
*/ | ||
final class FilenameConstraintTest extends TestCase | ||
{ | ||
public function test_invalid(): void | ||
{ | ||
$constraint = new FilenameConstraint(); | ||
|
||
self::assertFalse($constraint->validate(__FILE__.'.not_exists')->status->isValid()); | ||
} | ||
|
||
public function test_is_converted_optionally_to_json_schema(): void | ||
{ | ||
$constraint = new FilenameConstraint(); | ||
|
||
self::assertEquals([ | ||
'type' => 'string', | ||
'minLength' => 1, | ||
], $constraint->optional()->toJsonSchema()); | ||
} | ||
|
||
public function test_is_converted_to_json_schema(): void | ||
{ | ||
$constraint = new FilenameConstraint(); | ||
|
||
self::assertEquals([ | ||
'type' => 'string', | ||
'minLength' => 1, | ||
], $constraint->toJsonSchema()); | ||
} | ||
|
||
public function test_nullable_is_converted_to_json_schema(): void | ||
{ | ||
$constraint = new FilenameConstraint(); | ||
|
||
self::assertEquals([ | ||
'type' => ['null', 'string'], | ||
'minLength' => 1, | ||
'default' => null, | ||
], $constraint->nullable()->toJsonSchema()); | ||
} | ||
|
||
public function test_validates(): void | ||
{ | ||
$constraint = new FilenameConstraint(); | ||
|
||
self::assertTrue($constraint->validate(__FILE__)->status->isValid()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.