|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\ComplexField; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface; |
|
7
|
|
|
|
|
8
|
|
View Code Duplication |
class EzRichText extends AbstractComplexField implements ComplexFieldInterface |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
protected $resolver; |
|
11
|
|
|
|
|
12
|
27 |
|
public function __construct(PrefixBasedResolverInterface $resolver) |
|
13
|
|
|
{ |
|
14
|
27 |
|
$this->resolver = $resolver; |
|
15
|
27 |
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Replace any references in an xml string to be used as the input data for an ezrichtext field. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string|array $fieldValue The definition of teh field value, structured in the yml file. Either a string, or an array with key 'content' |
|
21
|
|
|
* @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration |
|
22
|
|
|
* @return string |
|
23
|
|
|
* |
|
24
|
|
|
* @todo replace objects and location refs in ezcontent:// and ezlocation:// links |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function createValue($fieldValue, array $context = array()) |
|
27
|
|
|
{ |
|
28
|
1 |
|
if (is_string($fieldValue)) { |
|
29
|
|
|
$xmlText = $fieldValue; |
|
30
|
|
|
} else { |
|
31
|
1 |
|
$xmlText = $fieldValue['content']; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Check if there are any references in the xml text and replace them. |
|
35
|
|
|
|
|
36
|
|
|
// we need to alter the regexp we get from the resolver, as it will be used to match parts of text, not the whole string |
|
37
|
1 |
|
$regexp = substr($this->resolver->getRegexp(), 1, -1); |
|
38
|
|
|
// NB: here we assume that all regexp resolvers give us a regexp with a very specific format... |
|
39
|
1 |
|
$regexp = '/\['.preg_replace(array('/^\^/'), array('', ''), $regexp) . '[^]]+\]/'; |
|
40
|
|
|
|
|
41
|
1 |
|
$count = preg_match_all($regexp, $xmlText, $matches); |
|
42
|
|
|
// $matches[0][] will have the matched full string eg.: [reference:example_reference] |
|
43
|
1 |
|
if ($count) { |
|
44
|
1 |
|
foreach ($matches[0] as $referenceIdentifier) { |
|
45
|
1 |
|
$reference = $this->resolver->getReferenceValue(substr($referenceIdentifier, 1, -1)); |
|
46
|
1 |
|
$xmlText = str_replace($referenceIdentifier, $reference, $xmlText); |
|
47
|
1 |
|
} |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return $xmlText; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
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.