These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Halapi; |
||
4 | |||
5 | use Halapi\Factory\RelationFactory; |
||
6 | use Halapi\Subscriber\JsonEventSubscriber; |
||
7 | use JMS\Serializer\EventDispatcher\EventDispatcherInterface; |
||
8 | use JMS\Serializer\SerializerBuilder; |
||
9 | |||
10 | /** |
||
11 | * Class HALAPIBuilder |
||
12 | * @author Romain Richard |
||
13 | */ |
||
14 | class HALAPIBuilder |
||
15 | { |
||
16 | /** |
||
17 | * @var SerializerBuilder |
||
18 | */ |
||
19 | private $serializerBuilder; |
||
20 | |||
21 | /** |
||
22 | * HALAPIBuilder constructor. |
||
23 | * |
||
24 | * @param RelationFactory $relationFactory |
||
25 | * @param SerializerBuilder|null $serializerBuilder |
||
26 | */ |
||
27 | public function __construct( |
||
28 | RelationFactory $relationFactory, |
||
29 | SerializerBuilder $serializerBuilder = null |
||
30 | ) { |
||
31 | $this->relationFactory = $relationFactory; |
||
0 ignored issues
–
show
|
|||
32 | $this->serializerBuilder = $serializerBuilder ?: SerializerBuilder::create(); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return \JMS\Serializer\Serializer |
||
37 | */ |
||
38 | public function getSerializer() |
||
39 | { |
||
40 | $this->serializerBuilder |
||
41 | ->addDefaultListeners() |
||
42 | ->configureListeners(function(EventDispatcherInterface $dispatcher) { |
||
43 | $dispatcher->addSubscriber(new JsonEventSubscriber($this->relationFactory)); |
||
44 | }) |
||
45 | ; |
||
46 | |||
47 | return $this->serializerBuilder->build(); |
||
48 | } |
||
49 | } |
||
50 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: