Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

EzRichText::hashToFieldValue()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 13

Duplication

Lines 4
Ratio 15.38 %

Importance

Changes 0
Metric Value
dl 4
loc 26
c 0
b 0
f 0
rs 8.5806
cc 4
eloc 13
nc 4
nop 2
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
6
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface;
7
8
/// @todo unify $this->resolver and $this->referenceResolver
9
class EzRichText extends AbstractFieldHandler implements FieldValueImporterInterface
10
{
11
    protected $resolver;
12
13
    public function __construct(PrefixBasedResolverInterface $resolver)
14
    {
15
        $this->resolver = $resolver;
16
    }
17
18
    /**
19
     * Replaces any references in an xml string to be used as the input data for an ezrichtext field.
20
     *
21
     * @param string|array $fieldValue The definition of teh field value, structured in the yml file. Either a string, or an array with key 'content'
22
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
23
     * @return string
24
     *
25
     * @todo replace objects and location refs in ezcontent:// and ezlocation:// links
26
     */
27
    public function hashToFieldValue($fieldValue, array $context = array())
28
    {
29
        if (is_string($fieldValue)) {
30
            $xmlText = $fieldValue;
31
        } else {
32
            $xmlText = $fieldValue['content'];
33
        }
34
35
        // Check if there are any references in the xml text and replace them.
36
37
        // 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
38
        $regexp = substr($this->resolver->getRegexp(), 1, -1);
39
        // NB: here we assume that all regexp resolvers give us a regexp with a very specific format...
40
        $regexp = '/\[' . preg_replace(array('/^\^/'), array('', ''), $regexp) . '[^]]+\]/';
41
42
        $count = preg_match_all($regexp, $xmlText, $matches);
43
        // $matches[0][] will have the matched full string eg.: [reference:example_reference]
44
        if ($count) {
45 View Code Duplication
            foreach ($matches[0] as $referenceIdentifier) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
46
                $reference = $this->resolver->getReferenceValue(substr($referenceIdentifier, 1, -1));
47
                $xmlText = str_replace($referenceIdentifier, $reference, $xmlText);
48
            }
49
        }
50
51
        return $xmlText;
52
    }
53
}
54