This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #4 from Jeckel-Lab/value-object
Value object
Showing
20 changed files
with
1,662 additions
and
172 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,69 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 20/11/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Type; | ||
use JeckelLab\AdvancedTypes\ValueObject\Color; | ||
|
||
/** | ||
* Class ColorType | ||
* @package JeckelLab\AdvancedTypes\DBAL\Types | ||
*/ | ||
class ColorType extends Type | ||
{ | ||
protected const COLOR_TYPE = 'color'; | ||
|
||
/** | ||
* @param array $fieldDeclaration | ||
* @param AbstractPlatform $platform | ||
* @return string | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string | ||
{ | ||
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return self::COLOR_TYPE; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param AbstractPlatform $platform | ||
* @return Color|null | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* @SuppressWarnings(PHPMD.StaticAccess) | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform): ?Color | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
return Color::byHex($value); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param AbstractPlatform $platform | ||
* @return string|null | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string | ||
{ | ||
if ($value instanceof Color) { | ||
return $value->getHex(); | ||
} | ||
return null; | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 18/11/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Type; | ||
use JeckelLab\AdvancedTypes\ValueObject\TimeDuration; | ||
|
||
/** | ||
* Class TimeDurationType | ||
* @package JeckelLab\AdvancedTypes\DBAL\Types | ||
*/ | ||
class TimeDurationType extends Type | ||
{ | ||
protected const TIME_DURATION_TYPE = 'time_duration'; | ||
|
||
/** | ||
* @param array $fieldDeclaration | ||
* @param AbstractPlatform $platform | ||
* @return string | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string | ||
{ | ||
return $platform->getIntegerTypeDeclarationSQL($fieldDeclaration); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return self::TIME_DURATION_TYPE; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param AbstractPlatform $platform | ||
* @return TimeDuration|null | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* @SuppressWarnings(PHPMD.StaticAccess) | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform): ?TimeDuration | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
return new TimeDuration((int) $value); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param AbstractPlatform $platform | ||
* @return int|null | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int | ||
{ | ||
if ($value instanceof TimeDuration) { | ||
return $value->getValue(); | ||
} | ||
return null; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 19/12/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes\DependencyInjection; | ||
|
||
use Exception; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
|
||
/** | ||
* Class AdvancedTypesExtension | ||
*/ | ||
class AdvancedTypesExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* @throws Exception | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container): void | ||
{ | ||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.yml'); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getAlias(): string | ||
{ | ||
return 'jeckellab_advanced_types'; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 13/11/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes; | ||
|
||
use JeckelLab\AdvancedTypes\DependencyInjection\AdvancedTypesExtension; | ||
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
/** | ||
* Class JeckelLabCommandDispatcherBundle | ||
*/ | ||
class JeckelLabAdvancedTypesBundle extends Bundle | ||
{ | ||
/** | ||
* @return AdvancedTypesExtension|ExtensionInterface|null | ||
*/ | ||
public function getContainerExtension() | ||
{ | ||
return new AdvancedTypesExtension(); | ||
} | ||
} |
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,8 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
JeckelLab\AdvancedTypes\Twig\DurationExtension: | ||
autowire: true | ||
tags: [ 'twig.runtime', 'twig.extension' ] |
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 | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 13/12/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
/** | ||
* Class DurationExtension | ||
* @package App\Twig | ||
*/ | ||
class DurationExtension extends AbstractExtension | ||
{ | ||
/** @var int */ | ||
protected $dayDuration = 86400; // 60*60*24 ==> 1 day | ||
|
||
/** | ||
* DurationExtension constructor. | ||
* @param int|null $dayDuration | ||
*/ | ||
public function __construct($dayDuration = null) | ||
{ | ||
if (null !== $dayDuration & is_numeric($dayDuration)) { | ||
$this->dayDuration = (int) $dayDuration; | ||
} | ||
} | ||
|
||
/** | ||
* @return iterable | ||
*/ | ||
public function getFilters(): iterable | ||
{ | ||
return [ | ||
new TwigFilter('duration', [$this, 'formatDuration']) | ||
]; | ||
} | ||
|
||
/** | ||
* @param int $duration | ||
* @return string | ||
*/ | ||
public function formatDuration(int $duration): string | ||
{ | ||
$minutes = floor($duration / 60) % 60; | ||
$hours = floor(($duration % $this->dayDuration) / 3600) ; | ||
$days = floor($duration / $this->dayDuration); | ||
|
||
switch (true) { | ||
case $days > 0: | ||
return sprintf('%dd %d:%02d', $days, $hours, $minutes); | ||
case ($hours > 0 || $minutes > 0): | ||
return sprintf('%d:%02d', $hours, $minutes); | ||
default: | ||
return '<1mn'; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 20/11/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes\ValueObject; | ||
|
||
use JeckelLab\AdvancedTypes\ValueObject\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Class Color | ||
* @package ValueObject | ||
*/ | ||
class Color | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $color; | ||
|
||
/** | ||
* Color constructor. | ||
* @param string $color | ||
*/ | ||
protected function __construct(string $color) | ||
{ | ||
$this->color = $color; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getHex(): string | ||
{ | ||
return $this->color; | ||
} | ||
|
||
/** | ||
* @param string $color | ||
* @return bool | ||
*/ | ||
public static function isValidHex(string $color): bool | ||
{ | ||
// Remove leading # if present | ||
if (strpos($color, '#') === 0) { | ||
$color = substr($color, 1); | ||
} | ||
return (ctype_xdigit($color) && strlen($color) === 6); | ||
} | ||
|
||
/** | ||
* @param string $color | ||
* @return Color | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function byHex(string $color): Color | ||
{ | ||
if (! self::isValidHex($color)) { | ||
throw new InvalidArgumentException(sprintf('Color %s is not a valid hex color', $color)); | ||
} | ||
return new self($color); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return $this->color; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 20/11/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes\ValueObject\Exception; | ||
|
||
/** | ||
* Class InvalidArgumentException | ||
* @package ValueObject\Exception | ||
*/ | ||
class InvalidArgumentException extends \InvalidArgumentException implements ValueObjectExceptionInterface | ||
{ | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/ValueObject/Exception/ValueObjectExceptionInterface.php
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,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 20/11/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes\ValueObject\Exception; | ||
|
||
use Throwable; | ||
|
||
/** | ||
* Interface ValueObjectExceptionInterface | ||
* @package ValueObject\Exception | ||
*/ | ||
interface ValueObjectExceptionInterface extends Throwable | ||
{ | ||
|
||
} |
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,110 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @author Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at : 18/11/2019 | ||
*/ | ||
|
||
namespace JeckelLab\AdvancedTypes\ValueObject; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* Class TimeDuration | ||
*/ | ||
class TimeDuration | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
protected $duration; | ||
|
||
/** | ||
* TimeDuration constructor. | ||
* @param int $duration | ||
*/ | ||
public function __construct(int $duration) | ||
{ | ||
$this->duration = $duration; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getValue(): int | ||
{ | ||
return $this->duration; | ||
} | ||
|
||
/** | ||
* @param string|null $format | ||
* @return string | ||
* @throw RuntimeException | ||
*/ | ||
public function format(?string $format = null): string | ||
{ | ||
if (null === $format) { | ||
$format = $this->getOptimalFormat(); | ||
} | ||
$pattern = ['/%h/', '/%m/', '/%s/', '/%M/', '/%S/']; | ||
$hours = floor($this->duration / 3600); | ||
$minutes = floor(($this->duration % 3600) / 60); | ||
$seconds = $this->duration % 60; | ||
|
||
$values = [ | ||
$hours, | ||
$minutes, | ||
$seconds, | ||
sprintf('%02d', $minutes), | ||
sprintf('%02d', $seconds) | ||
]; | ||
|
||
$result = preg_replace($pattern, $values, $format); | ||
if (is_string($result)) { | ||
return $result; | ||
} | ||
throw new RuntimeException('Invalid format for time duration '.$format); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getOptimalFormat(): string | ||
{ | ||
if ($this->duration > 3600) { | ||
return '%h:%M:%S'; | ||
} | ||
if ($this->duration > 60) { | ||
return '%m:%S'; | ||
} | ||
return '%s'; | ||
} | ||
|
||
/** | ||
* @param int $duration | ||
* @return $this | ||
*/ | ||
public function add(int $duration): self | ||
{ | ||
$this->duration += $duration; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param TimeDuration $duration | ||
* @return $this | ||
*/ | ||
public function addDuration(TimeDuration $duration): self | ||
{ | ||
$this->add($duration->getValue()); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return $this->format(); | ||
} | ||
} |