1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager; |
13
|
|
|
|
14
|
|
|
use Sulu\Component\DocumentManager\Exception\InvalidLocaleException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class responsible for encoding properties to PHPCR nodes. |
18
|
|
|
*/ |
19
|
|
|
class PropertyEncoder |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var NamespaceRegistry |
23
|
|
|
*/ |
24
|
|
|
private $namespaceRegistry; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param NamespaceRegistry $namespaceRegistry |
28
|
|
|
*/ |
29
|
|
|
public function __construct(NamespaceRegistry $namespaceRegistry) |
30
|
|
|
{ |
31
|
|
|
$this->namespaceRegistry = $namespaceRegistry; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function encode($encoding, $name, $locale) |
35
|
|
|
{ |
36
|
|
|
switch ($encoding) { |
37
|
|
|
case 'system_localized': |
38
|
|
|
return $this->localizedSystemName($name, $locale); |
39
|
|
|
case 'system': |
40
|
|
|
return $this->systemName($name); |
41
|
|
|
case 'content_localized': |
42
|
|
|
return $this->localizedContentName($name, $locale); |
43
|
|
|
case 'content': |
44
|
|
|
return $this->contentName($name); |
45
|
|
|
default: |
46
|
|
|
throw new \InvalidArgumentException(sprintf( |
47
|
|
|
'Invalid encoding "%s"', $encoding |
48
|
|
|
)); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $name |
54
|
|
|
* @param string $locale |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function localizedSystemName($name, $locale) |
59
|
|
|
{ |
60
|
|
|
if ($locale === null) { |
61
|
|
|
throw new InvalidLocaleException($locale); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->formatLocalizedName('system_localized', $name, $locale); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function systemName($name) |
73
|
|
|
{ |
74
|
|
|
return $this->formatName('system', $name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
* @param string $locale |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function localizedContentName($name, $locale) |
84
|
|
|
{ |
85
|
|
|
if ($locale === null) { |
86
|
|
|
throw new InvalidLocaleException($locale); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->formatLocalizedName('content_localized', $name, $locale); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function contentName($name) |
98
|
|
|
{ |
99
|
|
|
return $this->formatName('content', $name); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
private function formatName($role, $name) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$prefix = $this->namespaceRegistry->getPrefix($role); |
105
|
|
|
|
106
|
|
|
if (!$prefix) { |
107
|
|
|
return $name; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return sprintf( |
111
|
|
|
'%s:%s', |
112
|
|
|
$prefix, |
113
|
|
|
$name |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
private function formatLocalizedName($role, $name, $locale) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$prefix = $this->namespaceRegistry->getPrefix($role); |
120
|
|
|
|
121
|
|
|
if (!$prefix) { |
122
|
|
|
return sprintf('%s-%s', $locale, $name); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return sprintf( |
126
|
|
|
'%s:%s-%s', |
127
|
|
|
$prefix, |
128
|
|
|
$locale, |
129
|
|
|
$name |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.