1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\GraphNavigator\Factory;
6
7use JMS\Serializer\Accessor\AccessorStrategyInterface;
8use JMS\Serializer\Accessor\DefaultAccessorStrategy;
9use JMS\Serializer\EventDispatcher\EventDispatcher;
10use JMS\Serializer\EventDispatcher\EventDispatcherInterface;
11use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
12use JMS\Serializer\GraphNavigator\SerializationGraphNavigator;
13use JMS\Serializer\GraphNavigatorInterface;
14use JMS\Serializer\Handler\HandlerRegistryInterface;
15use Metadata\MetadataFactoryInterface;
16
17final class SerializationGraphNavigatorFactory implements GraphNavigatorFactoryInterface
18{
19    /**
20     * @var MetadataFactoryInterface
21     */
22    private $metadataFactory;
23    /**
24     * @var HandlerRegistryInterface
25     */
26    private $handlerRegistry;
27    /**
28     * @var AccessorStrategyInterface
29     */
30    private $accessor;
31    /**
32     * @var EventDispatcherInterface
33     */
34    private $dispatcher;
35    /**
36     * @var ExpressionEvaluatorInterface
37     */
38    private $expressionEvaluator;
39
40    public function __construct(
41        MetadataFactoryInterface $metadataFactory,
42        HandlerRegistryInterface $handlerRegistry,
43        ?AccessorStrategyInterface $accessor = null,
44        ?EventDispatcherInterface $dispatcher = null,
45        ?ExpressionEvaluatorInterface $expressionEvaluator = null
46    ) {
47        $this->metadataFactory = $metadataFactory;
48        $this->handlerRegistry = $handlerRegistry;
49        $this->accessor = $accessor ?: new DefaultAccessorStrategy();
50        $this->dispatcher = $dispatcher ?: new EventDispatcher();
51        $this->expressionEvaluator = $expressionEvaluator;
52    }
53
54    public function getGraphNavigator(): GraphNavigatorInterface
55    {
56        return new SerializationGraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->accessor, $this->dispatcher, $this->expressionEvaluator);
57    }
58}
59