| @@ 9-54 (lines=46) @@ | ||
| 6 | use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher; |
|
| 7 | use Kaliop\eZMigrationBundle\Core\Matcher\LocationMatcher; |
|
| 8 | ||
| 9 | class EzRichText extends AbstractComplexField implements ComplexFieldInterface |
|
| 10 | { |
|
| 11 | protected $contentMatcher; |
|
| 12 | protected $locationMatcher; |
|
| 13 | ||
| 14 | public function __construct(ContentMatcher $contentMatcher, LocationMatcher $locationMatcher) |
|
| 15 | { |
|
| 16 | $this->contentMatcher = $contentMatcher; |
|
| 17 | $this->locationMatcher = $locationMatcher; |
|
| 18 | } |
|
| 19 | ||
| 20 | /** |
|
| 21 | * Replace any references in an xml string to be used as the input data for an ezrichtext field. |
|
| 22 | * |
|
| 23 | * @param string|array $fieldValue The definition of teh field value, structured in the yml file. Either a string, or an array with key 'content' |
|
| 24 | * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration |
|
| 25 | * @return string |
|
| 26 | * |
|
| 27 | * @todo replace objects and location refs in ezcontent:// and ezlocation:// links |
|
| 28 | */ |
|
| 29 | public function createValue($fieldValue, array $context = array()) |
|
| 30 | { |
|
| 31 | if (is_string($fieldValue)) { |
|
| 32 | $xmlText = $fieldValue; |
|
| 33 | } else { |
|
| 34 | $xmlText = $fieldValue['content']; |
|
| 35 | } |
|
| 36 | ||
| 37 | /// @todo this regexp belongs to the resolver... |
|
| 38 | ||
| 39 | //Check if there are any references in the xml text and replace them. |
|
| 40 | // $result[0][] will have the matched full string eg.: [reference:example_reference] |
|
| 41 | // $result[1][] will have the reference id eg.: example_reference |
|
| 42 | $count = preg_match_all('|\[(reference:[^\]\[]*)\]|', $xmlText, $result); |
|
| 43 | ||
| 44 | if ($count !== false && count($result) > 1) { |
|
| 45 | foreach ($result[1] as $index => $referenceIdentifier) { |
|
| 46 | $reference = $this->referenceResolver->getReferenceValue($referenceIdentifier); |
|
| 47 | ||
| 48 | $xmlText = str_replace($result[0][$index], $reference, $xmlText); |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||
| 52 | return $xmlText; |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| @@ 9-54 (lines=46) @@ | ||
| 6 | use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher; |
|
| 7 | use Kaliop\eZMigrationBundle\Core\Matcher\LocationMatcher; |
|
| 8 | ||
| 9 | class EzXmlText extends AbstractComplexField implements ComplexFieldInterface |
|
| 10 | { |
|
| 11 | protected $contentMatcher; |
|
| 12 | protected $locationMatcher; |
|
| 13 | ||
| 14 | public function __construct(ContentMatcher $contentMatcher, LocationMatcher $locationMatcher) |
|
| 15 | { |
|
| 16 | $this->contentMatcher = $contentMatcher; |
|
| 17 | $this->locationMatcher = $locationMatcher; |
|
| 18 | } |
|
| 19 | ||
| 20 | /** |
|
| 21 | * Replace any references in an xml string to be used as the input data for an ezxmltext field. |
|
| 22 | * |
|
| 23 | * @param string|array $fieldValue The definition of teh field value, structured in the yml file. Either a string, or an array with key 'content' |
|
| 24 | * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration |
|
| 25 | * @return string |
|
| 26 | * |
|
| 27 | * @todo replace objects and location refs in eznode and ezobject links |
|
| 28 | */ |
|
| 29 | public function createValue($fieldValue, array $context = array()) |
|
| 30 | { |
|
| 31 | if (is_string($fieldValue)) { |
|
| 32 | $xmlText = $fieldValue; |
|
| 33 | } else { |
|
| 34 | $xmlText = $fieldValue['content']; |
|
| 35 | } |
|
| 36 | ||
| 37 | /// @todo this regexp belongs to the resolver... |
|
| 38 | ||
| 39 | //Check if there are any references in the xml text and replace them. |
|
| 40 | // $result[0][] will have the matched full string eg.: [reference:example_reference] |
|
| 41 | // $result[1][] will have the reference id eg.: reference:example_reference |
|
| 42 | $count = preg_match_all('|\[(reference:[^\]\[]*)\]|', $xmlText, $result); |
|
| 43 | ||
| 44 | if ($count !== false && count($result) > 1) { |
|
| 45 | foreach ($result[1] as $index => $referenceIdentifier) { |
|
| 46 | $reference = $this->referenceResolver->getReferenceValue($referenceIdentifier); |
|
| 47 | ||
| 48 | $xmlText = str_replace($result[0][$index], $reference, $xmlText); |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||
| 52 | return $xmlText; |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||