Code Duplication    Length = 45-45 lines in 2 locations

Core/ComplexField/EzRichText.php 1 location

@@ 8-52 (lines=45) @@
5
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface;
6
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface;
7
8
class EzRichText extends AbstractComplexField implements ComplexFieldInterface
9
{
10
    protected $resolver;
11
12
    public function __construct(PrefixBasedResolverInterface $resolver)
13
    {
14
        $this->resolver = $resolver;
15
    }
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
    public function createValue($fieldValue, array $context = array())
27
    {
28
        if (is_string($fieldValue)) {
29
            $xmlText = $fieldValue;
30
        } else {
31
            $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
        $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
        $regexp = '/\['.preg_replace(array('/^\^/'), array('', ''), $regexp) . '[^]]+\]/';
40
41
        $count = preg_match_all($regexp, $xmlText, $matches);
42
        // $matches[0][] will have the matched full string eg.: [reference:example_reference]
43
        if ($count) {
44
            foreach ($matches[0] as $referenceIdentifier) {
45
                $reference = $this->resolver->getReferenceValue(substr($referenceIdentifier, 1, -1));
46
                $xmlText = str_replace($referenceIdentifier, $reference, $xmlText);
47
            }
48
        }
49
50
        return $xmlText;
51
    }
52
}
53

Core/ComplexField/EzXmlText.php 1 location

@@ 8-52 (lines=45) @@
5
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface;
6
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface;
7
8
class EzXmlText extends AbstractComplexField implements ComplexFieldInterface
9
{
10
    protected $resolver;
11
12
    public function __construct(PrefixBasedResolverInterface $resolver)
13
    {
14
        $this->resolver = $resolver;
15
    }
16
17
    /**
18
     * Replace any references in an xml string to be used as the input data for an ezxmltext 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 eznode and ezobject links
25
     */
26
    public function createValue($fieldValue, array $context = array())
27
    {
28
        if (is_string($fieldValue)) {
29
            $xmlText = $fieldValue;
30
        } else {
31
            $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
        $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
        $regexp = '/\['.preg_replace(array('/^\^/'), array('', ''), $regexp) . '[^]]+\]/';
40
41
        $count = preg_match_all($regexp, $xmlText, $matches);
42
        // $matches[0][] will have the matched full string eg.: [reference:example_reference]
43
        if ($count) {
44
            foreach ($matches[0] as $referenceIdentifier) {
45
                $reference = $this->resolver->getReferenceValue(substr($referenceIdentifier, 1, -1));
46
                $xmlText = str_replace($referenceIdentifier, $reference, $xmlText);
47
            }
48
        }
49
50
        return $xmlText;
51
    }
52
}
53