|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\UrlBundle\Form\DataTransformer; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
10
|
|
|
use Zicht\Bundle\UrlBundle\Aliasing\Aliasing; |
|
11
|
|
|
use Zicht\Bundle\UrlBundle\Aliasing\Mapper\UrlMapperInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Provides a Template Method pattern for implementing different mapping types |
|
15
|
|
|
* |
|
16
|
|
|
* @package Zicht\Bundle\UrlBundle\Form\DataTransformer |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractAliasingTransformer implements DataTransformerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Aliasing */ |
|
21
|
|
|
protected $aliasing; |
|
22
|
|
|
/** @var int */ |
|
23
|
|
|
protected $mode; |
|
24
|
|
|
/** Mode bit for transform method */ |
|
25
|
|
|
const MODE_TO_PUBLIC = 1; |
|
26
|
|
|
/** Mode bit for reverseTransform method */ |
|
27
|
|
|
const MODE_TO_INTERNAL = 2; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* AliasToInternalUrlTransformer constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Aliasing $aliasing |
|
33
|
|
|
* @param int $mode |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Aliasing $aliasing, $mode = self::MODE_TO_PUBLIC|self::MODE_TO_INTERNAL) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->aliasing = $aliasing; |
|
38
|
|
|
$this->mode = $mode; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Transforms a string containing internal urls to string to public urls. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $data |
|
45
|
|
|
* @return null|string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function transform($data) |
|
48
|
|
|
{ |
|
49
|
|
|
if (self::MODE_TO_PUBLIC === (self::MODE_TO_PUBLIC & $this->mode)) { |
|
50
|
|
|
return $this->map($data, UrlMapperInterface::MODE_INTERNAL_TO_PUBLIC); |
|
51
|
|
|
} else { |
|
52
|
|
|
return $data; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Tranforms a string containing public urls to string with internal urls. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $data |
|
60
|
|
|
* @return null|string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function reverseTransform($data) |
|
63
|
|
|
{ |
|
64
|
|
|
if (self::MODE_TO_INTERNAL === (self::MODE_TO_INTERNAL & $this->mode)) { |
|
65
|
|
|
return $this->map($data, UrlMapperInterface::MODE_PUBLIC_TO_INTERNAL); |
|
66
|
|
|
} else { |
|
67
|
|
|
return $data; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Wraps the doMap to defend for the 'null'-value case |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $data |
|
75
|
|
|
* @param string $mode |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
final public function map($data, $mode) |
|
79
|
|
|
{ |
|
80
|
|
|
if (null === $data) { |
|
|
|
|
|
|
81
|
|
|
return $data; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->doMap($data, $mode); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Implement the actual mapping with the specified mode. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $data |
|
91
|
|
|
* @param string $mode |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
abstract protected function doMap($data, $mode); |
|
95
|
|
|
} |
|
96
|
|
|
|