Skip to content

Commit

Permalink
Merge branch 'release/v0.3.3' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Sep 12, 2022
2 parents 764aa1f + b7bf83e commit d295178
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.3.3 (2022-09-12)
* Added Spectrum Colour Processor and Constraints
* Added Exception management to validator

## v0.3.2 (2022-09-12)
* Moved Interval constraints back into place

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
"symplify/easy-coding-standard": "^11",

"decodelabs/phpstan-decodelabs": "^0.6",
"decodelabs/compass": "^0.1.1"
"decodelabs/compass": "^0.1.1",
"decodelabs/spectrum": "^0.2.1"
},
"suggest": {
"decodelabs/compass": "IP address validation"
"decodelabs/compass": "IP address validation support",
"decodelabs/spectrum": "Color validation support"
},
"autoload": {
"psr-4": {
Expand Down
66 changes: 66 additions & 0 deletions src/Lucid/Constraint/Color/MaxLightness.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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

declare(strict_types=1);

namespace DecodeLabs\Lucid\Constraint\Color;

use DecodeLabs\Lucid\Constraint;
use DecodeLabs\Lucid\ConstraintTrait;
use DecodeLabs\Lucid\Validate\Error;
use DecodeLabs\Spectrum\Color;
use Generator;

/**
* @implements Constraint<float, Color>
*/
class MaxLightness implements Constraint
{
/**
* @phpstan-use ConstraintTrait<float, Color>
*/
use ConstraintTrait;

public const OUTPUT_TYPES = [
'Spectrum:Color'
];

protected ?float $max = null;

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

public function setParameter(mixed $param): static
{
$this->max = (float)$param;
return $this;
}

public function getParameter(): mixed
{
return $this->max;
}

public function validate(mixed $value): Generator
{
if ($value === null) {
return false;
}

if ($value->getHslLightness() > $this->max) {
yield new Error(
$this,
$value,
'%type% value must not have lightness greater than %maxLightness%'
);
}

return true;
}
}
66 changes: 66 additions & 0 deletions src/Lucid/Constraint/Color/MaxSaturation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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

declare(strict_types=1);

namespace DecodeLabs\Lucid\Constraint\Color;

use DecodeLabs\Lucid\Constraint;
use DecodeLabs\Lucid\ConstraintTrait;
use DecodeLabs\Lucid\Validate\Error;
use DecodeLabs\Spectrum\Color;
use Generator;

/**
* @implements Constraint<float, Color>
*/
class MaxSaturation implements Constraint
{
/**
* @phpstan-use ConstraintTrait<float, Color>
*/
use ConstraintTrait;

public const OUTPUT_TYPES = [
'Spectrum:Color'
];

protected ?float $max = null;

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

public function setParameter(mixed $param): static
{
$this->max = (float)$param;
return $this;
}

public function getParameter(): mixed
{
return $this->max;
}

public function validate(mixed $value): Generator
{
if ($value === null) {
return false;
}

if ($value->getHslSaturation() > $this->max) {
yield new Error(
$this,
$value,
'%type% value must not have saturation greater than %maxSaturation%'
);
}

return true;
}
}
66 changes: 66 additions & 0 deletions src/Lucid/Constraint/Color/MinLightness.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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

declare(strict_types=1);

namespace DecodeLabs\Lucid\Constraint\Color;

use DecodeLabs\Lucid\Constraint;
use DecodeLabs\Lucid\ConstraintTrait;
use DecodeLabs\Lucid\Validate\Error;
use DecodeLabs\Spectrum\Color;
use Generator;

/**
* @implements Constraint<float, Color>
*/
class MinLightness implements Constraint
{
/**
* @phpstan-use ConstraintTrait<float, Color>
*/
use ConstraintTrait;

public const OUTPUT_TYPES = [
'Spectrum:Color'
];

protected ?float $min = null;

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

public function setParameter(mixed $param): static
{
$this->min = (float)$param;
return $this;
}

public function getParameter(): mixed
{
return $this->min;
}

public function validate(mixed $value): Generator
{
if ($value === null) {
return false;
}

if ($value->getHslLightness() < $this->min) {
yield new Error(
$this,
$value,
'%type% value must have lightness of at least %minLightness%'
);
}

return true;
}
}
66 changes: 66 additions & 0 deletions src/Lucid/Constraint/Color/MinSaturation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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

declare(strict_types=1);

namespace DecodeLabs\Lucid\Constraint\Color;

use DecodeLabs\Lucid\Constraint;
use DecodeLabs\Lucid\ConstraintTrait;
use DecodeLabs\Lucid\Validate\Error;
use DecodeLabs\Spectrum\Color;
use Generator;

/**
* @implements Constraint<float, Color>
*/
class MinSaturation implements Constraint
{
/**
* @phpstan-use ConstraintTrait<float, Color>
*/
use ConstraintTrait;

public const OUTPUT_TYPES = [
'Spectrum:Color'
];

protected ?float $min = null;

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

public function setParameter(mixed $param): static
{
$this->min = (float)$param;
return $this;
}

public function getParameter(): mixed
{
return $this->min;
}

public function validate(mixed $value): Generator
{
if ($value === null) {
return false;
}

if ($value->getHslSaturation() < $this->min) {
yield new Error(
$this,
$value,
'%type% value must have saturation of at least %minSaturation%'
);
}

return true;
}
}
62 changes: 62 additions & 0 deletions src/Lucid/Processor/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

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

declare(strict_types=1);

namespace DecodeLabs\Lucid\Processor;

use DecodeLabs\Exceptional;
use DecodeLabs\Lucid\Processor;
use DecodeLabs\Lucid\ProcessorTrait;
use DecodeLabs\Spectrum\Color as Spectrum;

/**
* @implements Processor<Spectrum>
*/
class Color implements Processor
{
/**
* @phpstan-use ProcessorTrait<Spectrum>
*/
use ProcessorTrait;

public function getOutputTypes(): array
{
return ['Spectrum:Color', 'DecodeLabs\\Spectrum\\Color'];
}

/**
* Convert prepared value to string or null
*/
public function coerce(mixed $value): ?Spectrum
{
if (!class_exists(Spectrum::class)) {
throw Exceptional::ComponentUnavailable(
'Color validation requires decodelabs-spectrum package'
);
}

if ($value === null) {
return null;
}

if (
is_string($value) ||
is_float($value) ||
is_array($value) ||
$value instanceof Spectrum
) {
return Spectrum::create($value);
}

throw Exceptional::UnexpectedValue(
'Could not coerce value to Spectrum Color',
null,
$value
);
}
}
Loading

0 comments on commit d295178

Please sign in to comment.