Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97b096b

Browse files
committed
extract AbstractXliffVersion
1 parent f65f4c1 commit 97b096b

File tree

2 files changed

+94
-57
lines changed

2 files changed

+94
-57
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Loader\XliffVersion;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
16+
/**
17+
* Base Xliff version loader class.
18+
*
19+
* @author Berny Cantos <[email protected]>
20+
*/
21+
abstract class AbstractXliffVersion
22+
{
23+
/**
24+
* Get validation schema source for this version
25+
*
26+
* @return string
27+
*/
28+
abstract public function getSchema();
29+
30+
/**
31+
* Extract messages and metadata from DOMDocument into a MessageCatalogue
32+
*
33+
* @param \DOMDocument $dom Source to extract messages and metadata
34+
* @param MessageCatalogue $catalogue Catalogue where we'll collect messages and metadata
35+
* @param string $domain The domain
36+
*/
37+
abstract public function extract(\DOMDocument $dom, MessageCatalogue $catalogue, $domain);
38+
39+
/**
40+
* Internally changes the URI of a dependent xsd to be loaded locally
41+
*
42+
* @param string $schemaSource Current content of schema file
43+
* @param string $xmlUri External URI of XML to convert to local
44+
*
45+
* @return string
46+
*/
47+
protected function fixXmlLocation($schemaSource, $xmlUri)
48+
{
49+
$newPath = str_replace('\\', '/', __DIR__).'/../schema/dic/xliff-core/xml.xsd';
50+
$parts = explode('/', $newPath);
51+
if (0 === stripos($newPath, 'phar://')) {
52+
$tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
53+
if ($tmpfile) {
54+
copy($newPath, $tmpfile);
55+
$parts = explode('/', str_replace('\\', '/', $tmpfile));
56+
}
57+
}
58+
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
59+
$newPath = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
60+
61+
return str_replace($xmlUri, $newPath, $schemaSource);
62+
}
63+
64+
/**
65+
* Convert a UTF8 string to the specified encoding
66+
*
67+
* @param string $content String to decode
68+
* @param string $encoding Target encoding
69+
*
70+
* @throws \RuntimeException
71+
* @return string
72+
*/
73+
protected function utf8ToCharset($content, $encoding = null)
74+
{
75+
if (empty($encoding) || 'UTF-8' === $encoding) {
76+
return $content;
77+
}
78+
79+
if (function_exists('mb_convert_encoding')) {
80+
return mb_convert_encoding($content, $encoding, 'UTF-8');
81+
}
82+
83+
if (function_exists('iconv')) {
84+
return iconv('UTF-8', $encoding, $content);
85+
}
86+
87+
throw new \RuntimeException(
88+
'No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).'
89+
);
90+
}
91+
}

src/Symfony/Component/Translation/Loader/XliffVersion/XliffVersion12.php

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
namespace Symfony\Component\Translation\Loader\XliffVersion;
1313

14-
use DOMDocument;
1514
use Symfony\Component\Translation\MessageCatalogue;
1615

1716
/**
1817
* XliffVersion12 loads XLIFF files identified with version 1.2
1918
*
2019
* @author Berny Cantos <[email protected]>
2120
*/
22-
class XliffVersion12
21+
class XliffVersion12 extends AbstractXliffVersion
2322
{
2423
/**
2524
* Get validation schema source for this version
@@ -36,11 +35,11 @@ public function getSchema()
3635
/**
3736
* Extract messages and metadata from DOMDocument into a MessageCatalogue
3837
*
39-
* @param DOMDocument $dom Source to extract messages and metadata
38+
* @param \DOMDocument $dom Source to extract messages and metadata
4039
* @param MessageCatalogue $catalogue Catalogue where we'll collect messages and metadata
4140
* @param string $domain The domain
4241
*/
43-
public function extract(DOMDocument $dom, MessageCatalogue $catalogue, $domain)
42+
public function extract(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
4443
{
4544
$xml = simplexml_import_dom($dom);
4645
$encoding = strtoupper($dom->encoding);
@@ -81,57 +80,4 @@ public function extract(DOMDocument $dom, MessageCatalogue $catalogue, $domain)
8180
}
8281
}
8382
}
84-
85-
/**
86-
* Internally changes the URI of a dependent xsd to be loaded locally
87-
*
88-
* @param string $schemaSource Current content of schema file
89-
* @param string $xmlUri External URI of XML to convert to local
90-
*
91-
* @return string
92-
*/
93-
protected function fixXmlLocation($schemaSource, $xmlUri)
94-
{
95-
$newPath = str_replace('\\', '/', __DIR__).'/../schema/dic/xliff-core/xml.xsd';
96-
$parts = explode('/', $newPath);
97-
if (0 === stripos($newPath, 'phar://')) {
98-
$tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
99-
if ($tmpfile) {
100-
copy($newPath, $tmpfile);
101-
$parts = explode('/', str_replace('\\', '/', $tmpfile));
102-
}
103-
}
104-
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
105-
$newPath = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
106-
107-
return str_replace($xmlUri, $newPath, $schemaSource);
108-
}
109-
110-
/**
111-
* Convert a UTF8 string to the specified encoding
112-
*
113-
* @param string $content String to decode
114-
* @param string $encoding Target encoding
115-
*
116-
* @throws \RuntimeException
117-
* @return string
118-
*/
119-
protected function utf8ToCharset($content, $encoding = null)
120-
{
121-
if (empty($encoding) || 'UTF-8' === $encoding) {
122-
return $content;
123-
}
124-
125-
if (function_exists('mb_convert_encoding')) {
126-
return mb_convert_encoding($content, $encoding, 'UTF-8');
127-
}
128-
129-
if (function_exists('iconv')) {
130-
return iconv('UTF-8', $encoding, $content);
131-
}
132-
133-
throw new \RuntimeException(
134-
'No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).'
135-
);
136-
}
13783
}

0 commit comments

Comments
 (0)