Skip to content

Commit

Permalink
Try an update to Symfony 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
webeweb committed May 4, 2024
1 parent edfbf97 commit d00a128
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 78 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ jobs:
- php-version: "8.1"
symfony-version: "6.4.*"
symfony-label: ": Symfony 6.4.*"
- php-version: "8.2"
symfony-version: "7.0.*"
symfony-label: ": Symfony 7.0.*"

steps:
- name: "Checkout"
Expand Down
38 changes: 19 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
"doctrine/orm": "^2.5.13|^3.0",
"matomo/device-detector": "^3.0|^4.0|^5.0",
"psr/log": "^1.0|^2.0|^3.0",
"symfony/console": "^4.4|^5.0|^6.0",
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
"symfony/event-dispatcher": "^4.4|^5.0|^6.0",
"symfony/filesystem": "^4.4|^5.0|^6.0",
"symfony/form": "^4.4|^5.0|^6.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/http-foundation": "^4.4|^5.0|^6.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0",
"symfony/mailer": "^4.4|^5.0|^6.0",
"symfony/routing": "^4.4|^5.0|^6.0",
"symfony/security-core": "^4.4|^5.0|^6.0",
"symfony/serializer": "^4.4|^5.0|^6.0",
"symfony/translation": "^4.4|^5.0|^6.0",
"symfony/yaml": "^4.4|^5.0|^6.0",
"symfony/console": "^4.4|^5.0|^6.0|^7.0",
"symfony/dependency-injection": "^4.4|^5.0|^6.0|^7.0",
"symfony/event-dispatcher": "^4.4|^5.0|^6.0|^7.0",
"symfony/filesystem": "^4.4|^5.0|^6.0|^7.0",
"symfony/form": "^4.4|^5.0|^6.0|^7.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0|^7.0",
"symfony/http-foundation": "^4.4|^5.0|^6.0|^7.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0|^7.0",
"symfony/mailer": "^4.4|^5.0|^6.0|^7.0",
"symfony/routing": "^4.4|^5.0|^6.0|^7.0",
"symfony/security-core": "^4.4|^5.0|^6.0|^7.0",
"symfony/serializer": "^4.4|^5.0|^6.0|^7.0",
"symfony/translation": "^4.4|^5.0|^6.0|^7.0",
"symfony/yaml": "^4.4|^5.0|^6.0|^7.0",
"twig/twig": "^2.10|^3.0",
"webeweb/core-library": "^9.1"
},
Expand All @@ -51,12 +51,12 @@
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^7.0|^9.0",
"symfony/asset": "^4.4|^5.0|^6.0",
"symfony/browser-kit": "^4.4|^5.0|^6.0",
"symfony/asset": "^4.4|^5.0|^6.0|^7.0",
"symfony/browser-kit": "^4.4|^5.0|^6.0|^7.0",
"symfony/monolog-bundle": "^3.0",
"symfony/security-bundle": "^4.4|^5.0|^6.0",
"symfony/security-csrf": "^4.4|^5.0|^6.0",
"symfony/twig-bundle": "^4.4|^5.0|^6.0"
"symfony/security-bundle": "^4.4|^5.0|^6.0|^7.0",
"symfony/security-csrf": "^4.4|^5.0|^6.0|^7.0",
"symfony/twig-bundle": "^4.4|^5.0|^6.0|^7.0"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 21 additions & 0 deletions lib/common/Form/DataTransformer/AbstractDataTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the jquery-datatables-bundle package.
*
* (c) 2024 WEBEWEB
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WBW\Bundle\CommonBundle\Form\DataTransformer;

use Symfony\Component\HttpKernel\Kernel;

// TODO: Remove when dropping support for Symfony 6.4
if (Kernel::VERSION_ID < 70000) {
class_alias("WBW\Bundle\CommonBundle\Form\DataTransformer\Symfony6DataTransformer", "WBW\Bundle\CommonBundle\Form\DataTransformer\AbstractDataTransformer");
} else {
class_alias("WBW\Bundle\CommonBundle\Form\DataTransformer\Symfony7DataTransformer", "WBW\Bundle\CommonBundle\Form\DataTransformer\AbstractDataTransformer");
}
85 changes: 46 additions & 39 deletions lib/common/Form/DataTransformer/AbstractDateTimeDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
* @package WBW\Bundle\CommonBundle\Form\DataTransformer
* @abstract
*/
abstract class AbstractDateTimeDataTransformer implements DataTransformerInterface {
abstract class AbstractDateTimeDataTransformer extends AbstractDataTransformer implements DataTransformerInterface {


/**
* Format.
Expand All @@ -52,6 +53,50 @@ protected function __construct(string $format, ?string $timeZone = null) {
$this->setTimezone($timeZone);
}

/**
* Decode a value.
*
* @param mixed|null $value The value.
* @return mixed|null Returns the decoded value.
* @throws Throwable Throws an exception if an error occurs.
*/
protected function decode($value) {

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

$zone = $this->newDateTimeZone();

$date = DateTime::createFromFormat($this->getFormat(), $value, $zone);
if (false === $date) {
return null;
}

return $date;
}

/**
* Encode a value.
*
* @param mixed|null $value The value.
* @return mixed|null Returns the encoded value.
* @throws Throwable Throws an exception if an error occurs.
*/
protected function encode($value) {

if (false === ($value instanceof DateTime)) {
return null;
}

$zone = $this->newDateTimeZone();
if (null !== $zone) {
$value->setTimezone($zone);
}

return $value->format($this->getFormat());
}

/**
* Get the format.
*
Expand Down Expand Up @@ -85,26 +130,6 @@ protected function newDateTimeZone(): ?DateTimeZone {
return new DateTimeZone($this->getTimezone());
}

/**
* {@inheritDoc}
* @throws Throwable Throws an exception if an error occurs.
*/
public function reverseTransform($value) {

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

$zone = $this->newDateTimeZone();

$date = DateTime::createFromFormat($this->getFormat(), $value, $zone);
if (false === $date) {
return null;
}

return $date;
}

/**
* Set the format.
*
Expand All @@ -126,22 +151,4 @@ public function setTimezone(?string $timezone): AbstractDateTimeDataTransformer
$this->timezone = $timezone;
return $this;
}

/**
* {@inheritDoc}
* @throws Throwable Throws an exception if an error occurs.
*/
public function transform($value) {

if (false === ($value instanceof DateTime)) {
return null;
}

$zone = $this->newDateTimeZone();
if (null !== $zone) {
$value->setTimezone($zone);
}

return $value->format($this->getFormat());
}
}
29 changes: 18 additions & 11 deletions lib/common/Form/DataTransformer/AbstractEntityDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @package WBW\Bundle\CommonBundle\Form\DataTransformer
* @abstract
*/
abstract class AbstractEntityDataTransformer implements DataTransformerInterface {
abstract class AbstractEntityDataTransformer extends AbstractDataTransformer implements DataTransformerInterface {

use EntityManagerTrait;

Expand All @@ -38,16 +38,12 @@ public function __construct(EntityManagerInterface $em) {
}

/**
* Get the entity class.
* Decode a value.
*
* @return string Returns the entity class.
*/
abstract protected function getEntityClass(): string;

/**
* {@inheritDoc}
* @param mixed|null $value The value.
* @return mixed|null Returns the decoded value
*/
public function reverseTransform($value) {
protected function decode($value) {

if ($value <= 0) {
return null;
Expand All @@ -59,14 +55,25 @@ public function reverseTransform($value) {
}

/**
* {@inheritDoc}
* Encode a value.
*
* @param mixed|null $value The value.
* @return mixed|null Returns the encoded value.
*/
public function transform($value) {
protected function encode($value) {

if (null === $value || false === method_exists($value, "getId")) {
return -1;
}

return $value->getId();
}

/**
* Get the entity class.
*
* @return string Returns the entity class.
*/
abstract protected function getEntityClass(): string;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ abstract class AbstractTimestampDataTransformer extends AbstractDateTimeDataTran
/**
* {@inheritDoc}
*/
public function reverseTransform($value) {
protected function decode($value) {

/** @var DateTime|null $date */
$date = parent::reverseTransform($value);
$date = parent::decode($value);
if (null === $date) {
return null;
}
Expand All @@ -41,14 +41,14 @@ public function reverseTransform($value) {
/**
* {@inheritDoc}
*/
public function transform($value) {
protected function encode($value) {

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

$date = new DateTime("@$value");

return parent::transform($date);
return parent::encode($date);
}
}
64 changes: 64 additions & 0 deletions lib/common/Form/DataTransformer/Symfony6DataTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the jquery-datatables-bundle package.
*
* (c) 2024 WEBEWEB
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WBW\Bundle\CommonBundle\Form\DataTransformer;

use Throwable;

/**
* Symfony 6.x data transformer.
*
* @author webeweb <https://github.com/webeweb>
* @package WBW\Bundle\CommonBundle\Form\DataTransformer
* @abstract
*/
abstract class Symfony6DataTransformer {

/**
* Decode a value.
*
* @param mixed|null $value The value.
* @return mixed|null Returns the decoded value.
* @throws Throwable Throws an exception if an error occurs.
*/
abstract protected function decode($value);

/**
* Encode a value.
*
* @param mixed|null $value The value.
* @return mixed|null Returns the encoded value.
* @throws Throwable Throws an exception if an error occurs.
*/
abstract protected function encode($value);

/**
* Reverse a transformed value.
*
* @param mixed|null $value The value.
* @return mixed|null Returns the original value.
* @throws Throwable Throws an exception if an error occurs.
*/
public function reverseTransform($value) {
return $this->decode($value);
}

/**
* Transform a value.
*
* @param mixed|null $value The value.
* @return mixed|null Returns the transformed value.
* @throws Throwable Throws an exception if an error occurs.
*/
public function transform($value) {
return $this->encode($value);
}
}
Loading

0 comments on commit d00a128

Please sign in to comment.