-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update docs/reference/installation.rst Co-Authored-By: Grégoire Paris <[email protected]> Update src/Bridge/Symfony/DependencyInjection/Configuration.php Co-Authored-By: Grégoire Paris <[email protected]> Update src/Bridge/Symfony/DependencyInjection/Configuration.php Co-Authored-By: Grégoire Paris <[email protected]> Update src/Bridge/Symfony/DependencyInjection/SonataFormExtension.php Co-Authored-By: Grégoire Paris <[email protected]> Update src/Bridge/Symfony/DependencyInjection/SonataFormExtension.php Co-Authored-By: Grégoire Paris <[email protected]> Update src/Serializer/BaseSerializerHandler.php Co-Authored-By: Javier Spagnoletti <[email protected]> Update src/Bridge/Symfony/DependencyInjection/SonataFormExtension.php Co-Authored-By: Javier Spagnoletti <[email protected]>
- Loading branch information
Showing
7 changed files
with
177 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
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,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\Form\Serializer; | ||
|
||
use JMS\Serializer\Context; | ||
use JMS\Serializer\GraphNavigator; | ||
use JMS\Serializer\VisitorInterface; | ||
use Sonata\Doctrine\Model\ManagerInterface; | ||
|
||
/** | ||
* @author Sylvain Deloux <[email protected]> | ||
*/ | ||
abstract class BaseSerializerHandler implements SerializerHandlerInterface | ||
{ | ||
/** | ||
* @var ManagerInterface | ||
*/ | ||
protected $manager; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected static $formats = []; | ||
|
||
public function __construct(ManagerInterface $manager) | ||
{ | ||
$this->manager = $manager; | ||
} | ||
|
||
final public static function setFormats(array $formats): void | ||
{ | ||
static::$formats = $formats; | ||
} | ||
|
||
final public static function addFormat(string $format): void | ||
{ | ||
static::$formats[] = $format; | ||
} | ||
|
||
public static function getSubscribingMethods(): array | ||
{ | ||
$type = static::getType(); | ||
$methods = []; | ||
|
||
foreach (static::$formats as $format) { | ||
$methods[] = [ | ||
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, | ||
'format' => $format, | ||
'type' => $type, | ||
'method' => 'serializeObjectToId', | ||
]; | ||
|
||
$methods[] = [ | ||
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, | ||
'format' => $format, | ||
'type' => $type, | ||
'method' => 'deserializeObjectFromId', | ||
]; | ||
} | ||
|
||
return $methods; | ||
} | ||
|
||
/** | ||
* Serialize data object to id. | ||
*/ | ||
public function serializeObjectToId(VisitorInterface $visitor, object $data, array $type, Context $context): ?int | ||
{ | ||
$className = $this->manager->getClass(); | ||
|
||
if ($data instanceof $className) { | ||
return $visitor->visitInteger($data->getId(), $type, $context); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Deserialize object from its id. | ||
*/ | ||
public function deserializeObjectFromId(VisitorInterface $visitor, int $data, array $type): ?object | ||
{ | ||
return $this->manager->findOneBy(['id' => $data]); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\Form\Serializer; | ||
|
||
use JMS\Serializer\Handler\SubscribingHandlerInterface; | ||
|
||
/** | ||
* @author Sylvain Deloux <[email protected]> | ||
*/ | ||
interface SerializerHandlerInterface extends SubscribingHandlerInterface | ||
{ | ||
public static function getType(): string; | ||
} |
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