Skip to content

Commit

Permalink
Merge pull request #76 from szymach/2.0
Browse files Browse the repository at this point in the history
Cast date/time/datetime values to DateTimeImmutable
  • Loading branch information
rn0 authored Mar 12, 2019
2 parents 4753a82 + 6620356 commit 39fcb82
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ matrix:
- php: 7.1
env:
- COMPOSER_FLAGS='--prefer-lowest'
- php: 7.2

sudo: false
- php: 7.3

cache:
directories:
Expand Down
8 changes: 4 additions & 4 deletions Form/Type/ResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ResourceType extends AbstractType
final class ResourceType extends AbstractType
{
/**
* @var MapBuilder
*/
protected $mapBuilder;
private $mapBuilder;

/**
* @var string
*/
protected $resourceClass;
private $resourceClass;

public function __construct(MapBuilder $mapBuilder, string $resourceClass)
{
Expand All @@ -49,7 +49,7 @@ public function configureOptions(OptionsResolver $resolver)
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (false === $this->mapBuilder->hasResource($options['resource_key'])) {
throw new ResourceFormTypeException(sprintf('"%s" is not a valid resource key', $options['resource_key']));
throw new ResourceFormTypeException("\"{$options['resource_key']}\" is not a valid resource key");
}

$resource = $this->mapBuilder->getResource($options['resource_key']);
Expand Down
24 changes: 21 additions & 3 deletions Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace FSi\Bundle\ResourceRepositoryBundle\Model;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;

class Resource implements ResourceValue
{
Expand Down Expand Up @@ -82,7 +84,7 @@ public function getTextValue()

public function setDateValue($dateValue)
{
$this->dateValue = $dateValue;
$this->dateValue = $this->toDateTimeImmutable($dateValue);
}

public function getDateValue()
Expand All @@ -92,7 +94,7 @@ public function getDateValue()

public function setDatetimeValue($datetimeValue)
{
$this->datetimeValue = $datetimeValue;
$this->datetimeValue = $this->toDateTimeImmutable($datetimeValue);
}

public function getDatetimeValue()
Expand All @@ -102,7 +104,7 @@ public function getDatetimeValue()

public function setTimeValue($timeValue)
{
$this->timeValue = $timeValue;
$this->timeValue = $this->toDateTimeImmutable($timeValue);
}

public function getTimeValue()
Expand Down Expand Up @@ -139,4 +141,20 @@ public function getBoolValue()
{
return $this->boolValue;
}

/**
* Symfony date/time/datetime forms do not allow for default DateTimeImmutable
* value until version 4.2, so the values need to be casted manually.
*
* @param DateTimeInterface|null $value
* @return DateTimeImmutable|null
*/
private function toDateTimeImmutable(?DateTimeInterface $value): ?DateTimeImmutable
{
if (true === $value instanceof DateTime) {
$value = DateTimeImmutable::createFromMutable($value);
}

return $value;
}
}
2 changes: 1 addition & 1 deletion Model/ResourceValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace FSi\Bundle\ResourceRepositoryBundle\Model;

Interface ResourceValue
interface ResourceValue
{
/**
* @param string $key
Expand Down
6 changes: 3 additions & 3 deletions Repository/MapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function recursiveParseRawMap(?array $rawMap = [], ?string $parentPath
{
$map = [];

if (!is_array($rawMap)) {
if (false === is_array($rawMap)) {
return $map;
}

Expand Down Expand Up @@ -133,7 +133,7 @@ protected function createResource(array $configuration, string $path): ResourceI
{
$type = $configuration['type'];

if (!array_key_exists($type, $this->resourceTypes)) {
if (false === array_key_exists($type, $this->resourceTypes)) {
throw new ConfigurationException(sprintf(
'"%s" is not a valid resource type. Try one from: %s',
$type,
Expand Down Expand Up @@ -183,7 +183,7 @@ protected function validateConfiguration(array $configuration, string $path): vo
));
}

if (!array_key_exists('type', $configuration)) {
if (false === array_key_exists('type', $configuration)) {
throw new ConfigurationException(sprintf(
'Missing "type" declaration in "%s" element configuration',
$path
Expand Down
11 changes: 3 additions & 8 deletions Twig/ResourceRepositoryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,18 @@
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class ResourceRepositoryExtension extends AbstractExtension
final class ResourceRepositoryExtension extends AbstractExtension
{
/**
* @var Repository
*/
protected $repository;
private $repository;

public function __construct(Repository $repository)
{
$this->repository = $repository;
}

public function getName()
{
return 'fsi_resource_repository';
}

public function getFunctions()
{
return [
Expand All @@ -40,7 +35,7 @@ public function getFunctions()
}),
new TwigFunction('get_resource', function(string $key, $default = null) {
$value = $this->repository->get($key);
return is_null($value) ? $default : $value;
return null !== $value ? $value : $default;
})
];
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"fsi/doctrine-extensions-bundle": "^1.0|^2.0",
"fsi/form-extensions-bundle": "^2.0",
"phpunit/phpunit": "^7.1",
"phpspec/phpspec": "^4.3",
"phpspec/phpspec": "^5.0",
"phpspec/prophecy": "^1.7",
"sebastian/exporter": "^3.1.0"
},
Expand Down

0 comments on commit 39fcb82

Please sign in to comment.