\PhpOffice\Common\Text -> \PhpOffice\PhpWord\Shared\Text
This commit is contained in:
parent
cf808cb3fc
commit
c52686c243
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
|
||||
/**
|
||||
* Bookmark element
|
||||
|
|
@ -45,7 +45,7 @@ class Bookmark extends AbstractElement
|
|||
*/
|
||||
public function __construct($name = '')
|
||||
{
|
||||
$this->name = CommonText::toUTF8($name);
|
||||
$this->name = SharedText::toUTF8($name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
|
||||
/**
|
||||
* Check box element
|
||||
|
|
@ -55,7 +55,7 @@ class CheckBox extends Text
|
|||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = CommonText::toUTF8($name);
|
||||
$this->name = SharedText::toUTF8($name);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
|
||||
|
|
@ -79,8 +79,8 @@ class Link extends AbstractElement
|
|||
*/
|
||||
public function __construct($source, $text = null, $fontStyle = null, $paragraphStyle = null, $internal = false)
|
||||
{
|
||||
$this->source = CommonText::toUTF8($source);
|
||||
$this->text = is_null($text) ? $this->source : CommonText::toUTF8($text);
|
||||
$this->source = SharedText::toUTF8($source);
|
||||
$this->text = is_null($text) ? $this->source : SharedText::toUTF8($text);
|
||||
$this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
|
||||
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
|
||||
$this->internal = $internal;
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
|
||||
|
||||
/**
|
||||
|
|
@ -57,7 +57,7 @@ class ListItem extends AbstractElement
|
|||
*/
|
||||
public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
|
||||
{
|
||||
$this->textObject = new Text(CommonText::toUTF8($text), $fontStyle, $paragraphStyle);
|
||||
$this->textObject = new Text(SharedText::toUTF8($text), $fontStyle, $paragraphStyle);
|
||||
$this->depth = $depth;
|
||||
|
||||
// Version >= 0.10.0 will pass numbering style name. Older version will use old method
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ class PreserveText extends AbstractElement
|
|||
$this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
|
||||
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
|
||||
|
||||
$this->text = CommonText::toUTF8($text);
|
||||
$this->text = SharedText::toUTF8($text);
|
||||
$matches = preg_split('/({.*?})/', $this->text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
||||
if (isset($matches[0])) {
|
||||
$this->text = $matches;
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
|
||||
|
|
@ -136,7 +136,7 @@ class Text extends AbstractElement
|
|||
*/
|
||||
public function setText($text)
|
||||
{
|
||||
$this->text = CommonText::toUTF8($text);
|
||||
$this->text = SharedText::toUTF8($text);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
|
||||
/**
|
||||
|
|
@ -62,7 +62,7 @@ class Title extends AbstractElement
|
|||
public function __construct($text, $depth = 1)
|
||||
{
|
||||
if (is_string($text)) {
|
||||
$this->text = CommonText::toUTF8($text);
|
||||
$this->text = SharedText::toUTF8($text);
|
||||
} elseif ($text instanceof TextRun) {
|
||||
$this->text = $text;
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,235 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of PHPWord - A pure PHP library for reading and writing
|
||||
* word processing documents.
|
||||
*
|
||||
* PHPWord is free software distributed under the terms of the GNU Lesser
|
||||
* General Public License version 3 as published by the Free Software Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please read the LICENSE
|
||||
* file that was distributed with this source code. For the full list of
|
||||
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
||||
*
|
||||
* @see https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2010-2018 PHPWord contributors
|
||||
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Shared;
|
||||
|
||||
/**
|
||||
* Text
|
||||
*/
|
||||
class Text
|
||||
{
|
||||
/**
|
||||
* Control characters array
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private static $controlCharacters = array();
|
||||
|
||||
/**
|
||||
* Build control characters array
|
||||
*/
|
||||
private static function buildControlCharacters()
|
||||
{
|
||||
for ($i = 0; $i <= 19; ++$i) {
|
||||
if ($i != 9 && $i != 10 && $i != 13) {
|
||||
$find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
|
||||
$replace = chr($i);
|
||||
self::$controlCharacters[$find] = $replace;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from PHP control character to OpenXML escaped control character
|
||||
*
|
||||
* Excel 2007 team:
|
||||
* ----------------
|
||||
* That's correct, control characters are stored directly in the shared-strings table.
|
||||
* We do encode characters that cannot be represented in XML using the following escape sequence:
|
||||
* _xHHHH_ where H represents a hexadecimal character in the character's value...
|
||||
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
|
||||
* element or in the shared string <t> element.
|
||||
*
|
||||
* @param string $value Value to escape
|
||||
* @return string
|
||||
*/
|
||||
public static function controlCharacterPHP2OOXML($value = '')
|
||||
{
|
||||
if (empty(self::$controlCharacters)) {
|
||||
self::buildControlCharacters();
|
||||
}
|
||||
|
||||
return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a number formatted for being integrated in xml files
|
||||
* @param float $number
|
||||
* @param integer $decimals
|
||||
* @return string
|
||||
*/
|
||||
public static function numberFormat($number, $decimals)
|
||||
{
|
||||
return number_format($number, $decimals, '.', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $dec
|
||||
* @link http://stackoverflow.com/a/7153133/2235790
|
||||
* @author velcrow
|
||||
* @return string
|
||||
*/
|
||||
public static function chr($dec)
|
||||
{
|
||||
if ($dec<=0x7F) {
|
||||
return chr($dec);
|
||||
}
|
||||
if ($dec<=0x7FF) {
|
||||
return chr(($dec>>6)+192).chr(($dec&63)+128);
|
||||
}
|
||||
if ($dec<=0xFFFF) {
|
||||
return chr(($dec>>12)+224).chr((($dec>>6)&63)+128).chr(($dec&63)+128);
|
||||
}
|
||||
if ($dec<=0x1FFFFF) {
|
||||
return chr(($dec>>18)+240).chr((($dec>>12)&63)+128).chr((($dec>>6)&63)+128).chr(($dec&63)+128);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from OpenXML escaped control character to PHP control character
|
||||
*
|
||||
* @param string $value Value to unescape
|
||||
* @return string
|
||||
*/
|
||||
public static function controlCharacterOOXML2PHP($value = '')
|
||||
{
|
||||
if (empty(self::$controlCharacters)) {
|
||||
self::buildControlCharacters();
|
||||
}
|
||||
|
||||
return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string contains UTF-8 data
|
||||
*
|
||||
* @param string $value
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isUTF8($value = '')
|
||||
{
|
||||
return is_string($value) && ($value === '' || preg_match('/^./su', $value) == 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return UTF8 encoded value
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function toUTF8($value = '')
|
||||
{
|
||||
if (!is_null($value) && !self::isUTF8($value)) {
|
||||
$value = utf8_encode($value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns unicode from UTF8 text
|
||||
*
|
||||
* The function is splitted to reduce cyclomatic complexity
|
||||
*
|
||||
* @param string $text UTF8 text
|
||||
* @return string Unicode text
|
||||
* @since 0.11.0
|
||||
*/
|
||||
public static function toUnicode($text)
|
||||
{
|
||||
return self::unicodeToEntities(self::utf8ToUnicode($text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns unicode array from UTF8 text
|
||||
*
|
||||
* @param string $text UTF8 text
|
||||
* @return array
|
||||
* @since 0.11.0
|
||||
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
|
||||
*/
|
||||
public static function utf8ToUnicode($text)
|
||||
{
|
||||
$unicode = array();
|
||||
$values = array();
|
||||
$lookingFor = 1;
|
||||
|
||||
// Gets unicode for each character
|
||||
for ($i = 0; $i < strlen($text); $i++) {
|
||||
$thisValue = ord($text[$i]);
|
||||
if ($thisValue < 128) {
|
||||
$unicode[] = $thisValue;
|
||||
} else {
|
||||
if (count($values) == 0) {
|
||||
$lookingFor = $thisValue < 224 ? 2 : 3;
|
||||
}
|
||||
$values[] = $thisValue;
|
||||
if (count($values) == $lookingFor) {
|
||||
if ($lookingFor == 3) {
|
||||
$number = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
|
||||
} else {
|
||||
$number = (($values[0] % 32) * 64) + ($values[1] % 64);
|
||||
}
|
||||
$unicode[] = $number;
|
||||
$values = array();
|
||||
$lookingFor = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $unicode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns entites from unicode array
|
||||
*
|
||||
* @param array $unicode
|
||||
* @return string
|
||||
* @since 0.11.0
|
||||
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
|
||||
*/
|
||||
private static function unicodeToEntities($unicode)
|
||||
{
|
||||
$entities = '';
|
||||
|
||||
foreach ($unicode as $value) {
|
||||
if ($value != 65279) {
|
||||
$entities .= $value > 127 ? '\uc0{\u' . $value . '}' : chr($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $entities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return name without underscore for < 0.10.0 variable name compatibility
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function removeUnderscorePrefix($value)
|
||||
{
|
||||
if (!is_null($value)) {
|
||||
if (substr($value, 0, 1) == '_') {
|
||||
$value = substr($value, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Style;
|
||||
|
||||
use PhpOffice\Common\Text;
|
||||
use PhpOffice\PhpWord\Shared\Text;
|
||||
|
||||
/**
|
||||
* Abstract style class
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Style;
|
||||
|
||||
use PhpOffice\Common\Text;
|
||||
use PhpOffice\PhpWord\Exception\InvalidStyleException;
|
||||
use PhpOffice\PhpWord\Shared\Text;
|
||||
use PhpOffice\PhpWord\SimpleType\Jc;
|
||||
use PhpOffice\PhpWord\SimpleType\TextAlignment;
|
||||
|
||||
|
|
|
|||
|
|
@ -17,13 +17,13 @@
|
|||
|
||||
namespace PhpOffice\PhpWord;
|
||||
|
||||
use PhpOffice\Common\Text;
|
||||
use PhpOffice\Common\XMLWriter;
|
||||
use PhpOffice\PhpWord\Escaper\RegExp;
|
||||
use PhpOffice\PhpWord\Escaper\Xml;
|
||||
use PhpOffice\PhpWord\Exception\CopyFileException;
|
||||
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
||||
use PhpOffice\PhpWord\Exception\Exception;
|
||||
use PhpOffice\PhpWord\Shared\Text;
|
||||
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||
|
||||
class TemplateProcessor
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\PhpWord\Element\AbstractElement as Element;
|
||||
use PhpOffice\PhpWord\Escaper\Rtf;
|
||||
use PhpOffice\PhpWord\Settings;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
||||
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
||||
|
|
@ -126,7 +126,7 @@ abstract class AbstractElement extends HTMLAbstractElement
|
|||
return $this->escaper->escape($text);
|
||||
}
|
||||
|
||||
return CommonText::toUnicode($text); // todo: replace with `return $text;` later.
|
||||
return SharedText::toUnicode($text); // todo: replace with `return $text;` later.
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
|
||||
|
||||
use PhpOffice\Common\Text as CommonText;
|
||||
use PhpOffice\Common\XMLWriter;
|
||||
use PhpOffice\PhpWord\Element\AbstractElement as Element;
|
||||
use PhpOffice\PhpWord\Settings;
|
||||
use PhpOffice\PhpWord\Shared\Text as SharedText;
|
||||
|
||||
/**
|
||||
* Abstract element writer
|
||||
|
|
@ -207,7 +207,7 @@ abstract class AbstractElement
|
|||
*/
|
||||
protected function getText($text)
|
||||
{
|
||||
return CommonText::controlCharacterPHP2OOXML($text);
|
||||
return SharedText::controlCharacterPHP2OOXML($text);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of PHPWord - A pure PHP library for reading and writing
|
||||
* word processing documents.
|
||||
*
|
||||
* PHPWord is free software distributed under the terms of the GNU Lesser
|
||||
* General Public License version 3 as published by the Free Software Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please read the LICENSE
|
||||
* file that was distributed with this source code. For the full list of
|
||||
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
||||
*
|
||||
* @see https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2010-2018 PHPWord contributors
|
||||
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Shared;
|
||||
|
||||
/**
|
||||
* Test class for Text
|
||||
*
|
||||
* @coversDefaultClass PhpOffice\PhpWord\Shared\Text
|
||||
*/
|
||||
class TextTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
*/
|
||||
public function testControlCharacters()
|
||||
{
|
||||
$this->assertEquals('', Text::controlCharacterPHP2OOXML());
|
||||
$this->assertEquals('aeiou', Text::controlCharacterPHP2OOXML('aeiou'));
|
||||
$this->assertEquals('àéîöù', Text::controlCharacterPHP2OOXML('àéîöù'));
|
||||
|
||||
$value = rand(0, 8);
|
||||
$this->assertEquals('_x'.sprintf('%04s', strtoupper(dechex($value))).'_', Text::controlCharacterPHP2OOXML(chr($value)));
|
||||
|
||||
$this->assertEquals('', Text::controlCharacterOOXML2PHP(''));
|
||||
$this->assertEquals(chr(0x08), Text::controlCharacterOOXML2PHP('_x0008_'));
|
||||
}
|
||||
|
||||
public function testNumberFormat()
|
||||
{
|
||||
$this->assertEquals('2.1', Text::numberFormat('2.06', 1));
|
||||
$this->assertEquals('2.1', Text::numberFormat('2.12', 1));
|
||||
$this->assertEquals('1234.0', Text::numberFormat(1234, 1));
|
||||
}
|
||||
|
||||
public function testChr()
|
||||
{
|
||||
$this->assertEquals('A', Text::chr(65));
|
||||
$this->assertEquals('A', Text::chr(0x41));
|
||||
$this->assertEquals('é', Text::chr(233));
|
||||
$this->assertEquals('é', Text::chr(0xE9));
|
||||
$this->assertEquals('⼳', Text::chr(12083));
|
||||
$this->assertEquals('⼳', Text::chr(0x2F33));
|
||||
$this->assertEquals('🌃', Text::chr(127747));
|
||||
$this->assertEquals('🌃', Text::chr(0x1F303));
|
||||
$this->assertEquals('', Text::chr(2097152));
|
||||
}
|
||||
/**
|
||||
* Is UTF8
|
||||
*/
|
||||
public function testIsUTF8()
|
||||
{
|
||||
$this->assertTrue(Text::isUTF8(''));
|
||||
$this->assertTrue(Text::isUTF8('éééé'));
|
||||
$this->assertFalse(Text::isUTF8(utf8_decode('éééé')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test unicode conversion
|
||||
*/
|
||||
public function testToUnicode()
|
||||
{
|
||||
$this->assertEquals('a', Text::toUnicode('a'));
|
||||
$this->assertEquals('\uc0{\u8364}', Text::toUnicode('€'));
|
||||
$this->assertEquals('\uc0{\u233}', Text::toUnicode('é'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove underscore prefix
|
||||
*/
|
||||
public function testRemoveUnderscorePrefix()
|
||||
{
|
||||
$this->assertEquals('item', Text::removeUnderscorePrefix('_item'));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue