Skip to content

Commit

Permalink
Merge branch 'release/v0.2.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Sep 8, 2022
2 parents 3abadce + 0b4c9a7 commit 1ef6016
Show file tree
Hide file tree
Showing 25 changed files with 942 additions and 86 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.2.0 (2022-09-08)
* Reorganised to contain main Lucid interfaces
* Updated key handling in MultiContextProvider
* Added Sanitizer loader interfaces

## v0.1.1 (2022-09-08)
* Removed force methods

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ For example:
```php
namespace My\Library;

use DecodeLabs\Lucid\Sanitizer\SingleContextProvider;
use DecodeLabs\Lucid\Sanitizer\SingleContextProviderTrait;
use DecodeLabs\Lucid\Provider\SingleContext;
use DecodeLabs\Lucid\Provider\SingleContextTrait;

class MyClass implements SingleContextProvider {
class MyClass implements SingleContext {

use SingleContextProviderTrait;
use SingleContextTrait;

protected mixed $value;

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": {
"php": "^8.0",

"decodelabs/coercion": "^0.2.2",
"decodelabs/exceptional": "^0.4"
},
"require-dev": {
Expand All @@ -20,8 +21,7 @@
"php-parallel-lint/php-parallel-lint": "^1.3",
"symplify/easy-coding-standard": "^11",

"decodelabs/phpstan-decodelabs": "^0.6",
"decodelabs/lucid": "^0.1|^0.2"
"decodelabs/phpstan-decodelabs": "^0.6"
},
"suggest": {},
"autoload": {
Expand All @@ -31,7 +31,7 @@
},
"extra": {
"branch-alias": {
"dev-develop": "0.1.x-dev"
"dev-develop": "0.2.x-dev"
}
},
"config": {
Expand Down
59 changes: 59 additions & 0 deletions src/Constraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* @package Lucid
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Lucid;

use DecodeLabs\Lucid\Validate\Error;
use Generator;

/**
* @template TParam
* @template TValue
*/
interface Constraint
{
/**
* @phpstan-param Processor<TValue> $processor
*/
public function __construct(Processor $processor);

/**
* @return array<string>|null
*/
public static function getProcessorOutputTypes(): ?array;

public function getName(): string;
public function getWeight(): int;

/**
* @phpstan-return Processor<TValue>
*/
public function getProcessor(): Processor;

/**
* @phpstan-param TParam $param
* @return $this
*/
public function setParameter(mixed $param): static;
public function getParameter(): mixed;

public function prepareValue(mixed $value): mixed;

/**
* @phpstan-param TValue $value
* @phpstan-return TValue|null
*/
public function alterValue(mixed $value): mixed;

/**
* @phpstan-param TValue|null $value
* @phpstan-return Generator<int, Error|null, mixed, bool>
*/
public function validate(mixed $value): Generator;
}
40 changes: 40 additions & 0 deletions src/Constraint/Processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @package Lucid
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Lucid\Constraint;

use DecodeLabs\Lucid\Constraint;
use DecodeLabs\Lucid\ConstraintTrait;

/**
* This constraint is used to wrap the main Processor
* to be passed to Error objects
*
* @template TValue
* @implements Constraint<TValue, TValue>
*/
class Processor implements Constraint
{
/**
* @phpstan-use ConstraintTrait<TValue, TValue>
*/
use ConstraintTrait;

public const OUTPUT_TYPES = [];

public function getName(): string
{
return $this->processor->getName();
}

public function getWeight(): int
{
return 1;
}
}
96 changes: 96 additions & 0 deletions src/ConstraintTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* @package Lucid
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Lucid;

use Generator;
use ReflectionClass;

/**
* @template TParam
* @template TValue
*/
trait ConstraintTrait
{
/**
* @phpstan-var Processor<TValue>
*/
protected Processor $processor;

/**
* @phpstan-param Processor<TValue> $processor
*/
public function __construct(Processor $processor)
{
$this->processor = $processor;
}

public static function getProcessorOutputTypes(): ?array
{
if (
defined('static::OUTPUT_TYPES') &&
/** @phpstan-ignore-next-line */
is_array(static::OUTPUT_TYPES)
) {
/** @phpstan-ignore-next-line */
return static::OUTPUT_TYPES;
}

return null;
}

public function getName(): string
{
return (new ReflectionClass($this))
->getShortName();
}

public function getWeight(): int
{
return 10;
}

public function getProcessor(): Processor
{
return $this->processor;
}

/**
* @phpstan-param TParam $param
* @return $this
*/
public function setParameter(mixed $param): static
{
return $this;
}

public function getParameter(): mixed
{
return null;
}

public function prepareValue(mixed $value): mixed
{
return $value;
}

public function alterValue(mixed $value): mixed
{
return $value;
}

/**
* @phpstan-param TValue $value
*/
public function validate(mixed $value): Generator
{
yield null;
return true;
}
}
94 changes: 94 additions & 0 deletions src/Processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* @package Lucid
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Lucid;

use DecodeLabs\Lucid\Validate\Error;
use Generator;

/**
* @template TOutput
*/
interface Processor
{
public function __construct(Sanitizer $sanitizer);

/**
* @return array<string>
*/
public function getOutputTypes(): array;

public function getName(): string;
public function getSanitizer(): Sanitizer;

/**
* Does this processor require a list of values?
*/
public function isMultiValue(): bool;

/**
* Prepare value before coercion
*/
public function prepareValue(mixed $value): mixed;

/**
* Apply value processing before validation
*
* @phpstan-param TOutput $value
* @phpstan-return TOutput|null
*/
public function alterValue(mixed $value): mixed;

/**
* Coerce input to output type or null
*
* @phpstan-return TOutput|null
*/
public function coerce(mixed $value): mixed;


/**
* Test validity of constraint
*
* @return $this
*/
public function test(
string $constraint,
mixed $param
): static;


/**
* @return array<string, mixed>
*/
public function getDefaultConstraints(): array;


/**
* @phpstan-return array<string, Constraint<mixed, TOutput>>
*/
public function prepareConstraints(): array;


/**
* Test constraints and yield errors in sequence
*
* @phpstan-param TOutput|null $value
* @return Generator<Error|null>
*/
public function validate(mixed $value): Generator;

/**
* Test type and yield errors in sequence
*
* @phpstan-param TOutput|null $value
* @return Generator<Error|null>
*/
public function validateType(mixed $value): Generator;
}
14 changes: 14 additions & 0 deletions src/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @package Lucid
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Lucid;

interface Provider
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@

declare(strict_types=1);

namespace DecodeLabs\Lucid\Sanitizer;
namespace DecodeLabs\Lucid\Provider;

use Closure;
use DecodeLabs\Lucid\Provider;
use DecodeLabs\Lucid\Sanitizer;
use DecodeLabs\Lucid\Validate\Result;

/**
* @template TValue
*/
interface DirectContextProvider
interface DirectContext extends Provider
{
/**
* @template TInput
Expand Down Expand Up @@ -52,10 +50,5 @@ public function is(
array|Closure|null $setup = null
): bool;

/**
* @template TInput
* @phpstan-param TInput $value
* @phpstan-return Sanitizer<TValue>
*/
public function sanitize(mixed $value): Sanitizer;
}
Loading

0 comments on commit 1ef6016

Please sign in to comment.