-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/v0.3.3' into main
- Loading branch information
Showing
8 changed files
with
371 additions
and
8 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
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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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 | ||
); | ||
} | ||
} |
Oops, something went wrong.