Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from mybandmarket/factory
Add a color factory
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Spatie\Color; | ||
|
||
use ReflectionClass; | ||
use Spatie\Color\Exceptions\InvalidColorValue; | ||
|
||
class Factory | ||
{ | ||
const EXCLUDED_FILES = ['.', '..']; | ||
|
||
public static function fromString(string $string): Color | ||
{ | ||
$colorClasses = self::getColorClasses(); | ||
|
||
foreach ($colorClasses as $colorClass) { | ||
try { | ||
return call_user_func("$colorClass::fromString", $string); | ||
} catch (InvalidColorValue $e) { | ||
// Catch the exception but never throw it. | ||
} | ||
} | ||
|
||
throw InvalidColorValue::malformedColorString($string); | ||
} | ||
|
||
protected static function getColorClasses(): array | ||
{ | ||
return [ | ||
Hex::class, | ||
Rgb::class, | ||
Rgba::class, | ||
]; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace Spatie\Color\Test; | ||
|
||
use Spatie\Color\Hex; | ||
use Spatie\Color\Rgb; | ||
use Spatie\Color\Rgba; | ||
use Spatie\Color\Factory; | ||
use Spatie\Color\Exceptions\InvalidColorValue; | ||
|
||
class FactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_create_a_hex_color_from_a_string() | ||
{ | ||
$hex = Factory::fromString('#aabbcc'); | ||
|
||
$this->assertInstanceOf(Hex::class, $hex); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_create_a_rgb_color_from_a_string() | ||
{ | ||
$rgb = Factory::fromString('rgb(55,155,255)'); | ||
|
||
$this->assertInstanceOf(Rgb::class, $rgb); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_create_a_rgba_color_from_a_string() | ||
{ | ||
$rgba = Factory::fromString('rgba(55,155,255,0.5)'); | ||
|
||
$this->assertInstanceOf(Rgba::class, $rgba); | ||
} | ||
|
||
/** @test */ | ||
public function it_cant_create_a_color_from_malformed_string() | ||
{ | ||
$this->expectException(InvalidColorValue::class); | ||
|
||
Factory::fromString('abcd'); | ||
} | ||
} |