|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2016, HelloFresh GmbH. |
|
4
|
|
|
* All rights reserved. |
|
5
|
|
|
* |
|
6
|
|
|
* This source code is licensed under the MIT license found in the |
|
7
|
|
|
* LICENSE file in the root directory of this source tree. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace HelloFresh\FeatureToggle\Serializer; |
|
11
|
|
|
|
|
12
|
|
|
use Collections\MapInterface; |
|
13
|
|
|
use HelloFresh\FeatureToggle\OperatorCondition; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Hand written serializer to serialize an OperatorCondition to a php array. |
|
17
|
|
|
*/ |
|
18
|
|
|
class OperatorConditionSerializer |
|
19
|
|
|
{ |
|
20
|
|
|
private $operatorSerializer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param OperatorSerializer $operatorSerializer |
|
24
|
|
|
*/ |
|
25
|
8 |
|
public function __construct(OperatorSerializer $operatorSerializer) |
|
26
|
|
|
{ |
|
27
|
8 |
|
$this->operatorSerializer = $operatorSerializer; |
|
28
|
8 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param OperatorCondition $condition |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function serialize(OperatorCondition $condition) |
|
36
|
|
|
{ |
|
37
|
|
|
return array( |
|
38
|
1 |
|
'name' => 'operator-condition', |
|
39
|
1 |
|
'key' => $condition->getKey(), |
|
40
|
2 |
|
'operator' => $this->operatorSerializer->serialize($condition->getOperator()), |
|
41
|
1 |
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param MapInterface $condition |
|
46
|
|
|
* |
|
47
|
|
|
* @return OperatorCondition |
|
48
|
|
|
*/ |
|
49
|
7 |
|
public function deserialize(MapInterface $condition) |
|
50
|
|
|
{ |
|
51
|
7 |
|
$name = $condition->get('name'); |
|
52
|
|
|
|
|
53
|
3 |
|
if ($name !== 'operator-condition') { |
|
54
|
1 |
|
throw new \RuntimeException(sprintf('Unable to deserialize operator with name "%s".', $name)); |
|
55
|
|
|
} |
|
56
|
2 |
|
$operator = $this->operatorSerializer->deserialize($condition->get('operator')); |
|
57
|
|
|
|
|
58
|
2 |
|
return new OperatorCondition($condition->get('key'), $operator); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|