Skip to content

PHP7 Update - Renamed String class to SharedString #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

matrix:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"phploc/phploc": "2.*",
"dompdf/dompdf":"0.6.*",
"tecnick.com/tcpdf": "6.*",
"mpdf/mpdf": "5.*",
"mpdf/mpdf": "dev-development",
"zendframework/zend-validator": "2.5.*"
},
"suggest": {
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Element/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Style;

/**
Expand Down Expand Up @@ -47,7 +47,7 @@ class Bookmark extends AbstractElement
public function __construct($name)
{

$this->name = String::toUTF8($name);
$this->name = SharedString::toUTF8($name);
return $this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Element/CheckBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;

/**
* Check box element
Expand Down Expand Up @@ -56,7 +56,7 @@ public function __construct($name = null, $text = null, $fontStyle = null, $para
*/
public function setName($name)
{
$this->name = String::toUTF8($name);
$this->name = SharedString::toUTF8($name);

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/PhpWord/Element/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;

Expand Down Expand Up @@ -78,8 +78,8 @@ class Link extends AbstractElement
*/
public function __construct($source, $text = null, $fontStyle = null, $paragraphStyle = null, $internal = false)
{
$this->source = String::toUTF8($source);
$this->text = is_null($text) ? $this->source : String::toUTF8($text);
$this->source = SharedString::toUTF8($source);
$this->text = is_null($text) ? $this->source : SharedString::toUTF8($text);
$this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
$this->internal = $internal;
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Element/ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ class ListItem extends AbstractElement
*/
public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
{
$this->textObject = new Text(String::toUTF8($text), $fontStyle, $paragraphStyle);
$this->textObject = new Text(SharedString::toUTF8($text), $fontStyle, $paragraphStyle);
$this->depth = $depth;

// Version >= 0.10.0 will pass numbering style name. Older version will use old method
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Element/PreserveText.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;

Expand Down Expand Up @@ -61,7 +61,7 @@ public function __construct($text = null, $fontStyle = null, $paragraphStyle = n
$this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);

$this->text = String::toUTF8($text);
$this->text = SharedString::toUTF8($text);
$matches = preg_split('/({.*?})/', $this->text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
if (isset($matches[0])) {
$this->text = $matches;
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Element/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;

Expand Down Expand Up @@ -136,7 +136,7 @@ public function getParagraphStyle()
*/
public function setText($text)
{
$this->text = String::toUTF8($text);
$this->text = SharedString::toUTF8($text);

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Element/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Style;

/**
Expand Down Expand Up @@ -61,7 +61,7 @@ class Title extends AbstractElement
*/
public function __construct($text, $depth = 1)
{
$this->text = String::toUTF8($text);
$this->text = SharedString::toUTF8($text);
$this->depth = $depth;
if (array_key_exists("Heading_{$this->depth}", Style::getStyles())) {
$this->style = "Heading{$this->depth}";
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Shared/PCLZip/pclzip.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class PclZip
// Note that no real action is taken, if the archive does not exist it is not
// created. Use create() for that.
// --------------------------------------------------------------------------------
function PclZip($p_zipname)
function __construct($p_zipname)
{

// ----- Tests the zlib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Common string functions
*/
class String
class SharedString
{
/**
* Control characters array
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Style/AbstractStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Style;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;

/**
* Abstract style class
Expand Down Expand Up @@ -161,7 +161,7 @@ public function setStyleValue($key, $value)
if (isset($this->aliases[$key])) {
$key = $this->aliases[$key];
}
$method = 'set' . String::removeUnderscorePrefix($key);
$method = 'set' . SharedString::removeUnderscorePrefix($key);
if (method_exists($this, $method)) {
$this->$method($value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace PhpOffice\PhpWord\Style;

use PhpOffice\PhpWord\Exception\InvalidStyleException;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\SimpleType\Jc;

/**
Expand Down Expand Up @@ -167,7 +167,7 @@ class Paragraph extends Border
*/
public function setStyleValue($key, $value)
{
$key = String::removeUnderscorePrefix($key);
$key = SharedString::removeUnderscorePrefix($key);
if ('indent' == $key || 'hanging' == $key) {
$value = $value * 720;
} elseif ('spacing' == $key) {
Expand Down
41 changes: 39 additions & 2 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use PhpOffice\PhpWord\Exception\CopyFileException;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Shared\ZipArchive;

class TemplateProcessor
Expand Down Expand Up @@ -134,6 +134,43 @@ public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslO

$this->tempDocumentMainPart = $xmlTransformed;
}

/**
* Clean all template parts of hidden XML within variables/placeholders.
*
* @return void
*/
public function fixBrokenVariables()
{
$this->tempDocumentMainPart = $this->fixBrokenVariablesForPart($this->tempDocumentMainPart);

foreach ($this->tempDocumentHeaders as &$headerXML) {
$headerXML = $this->fixBrokenVariablesForPart($headerXML);
}

foreach ($this->tempDocumentFooters as &$footerXML) {
$headerXML = $this->fixBrokenVariablesForPart($footerXML);
}
}

/**
* Fix dirty hidden XML within variables in document part.
*
* @param string $documentPartXML said document part
*
* @return string
*/
protected function fixBrokenVariablesForPart(string $documentPartXML)
{
return
preg_replace_callback(
'|\$([^{]*)(\{.*})|U',
function ($match) {
return '$'.strip_tags($match[2]);
},
$documentPartXML
);
}

/**
* @param mixed $macro
Expand All @@ -148,7 +185,7 @@ public function setValue($macro, $replace, $limit = self::MAXIMUM_REPLACEMENTS_D
$macro = '${' . $macro . '}';
}

if (!String::isUTF8($replace)) {
if (!SharedString::isUTF8($replace)) {
$replace = utf8_encode($replace);
}

Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Writer/RTF/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace PhpOffice\PhpWord\Writer\RTF\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font as FontStyle;
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
Expand Down Expand Up @@ -112,7 +112,7 @@ protected function writeOpening()
*/
protected function writeText($text)
{
return String::toUnicode($text);
return SharedString::toUnicode($text);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Writer/Word2007/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;

use PhpOffice\PhpWord\Element\AbstractElement as Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Shared\SharedString;
use PhpOffice\PhpWord\Shared\XMLWriter;

/**
Expand Down Expand Up @@ -167,6 +167,6 @@ private function writeTextStyle($styleType)
*/
protected function getText($text)
{
return String::controlCharacterPHP2OOXML($text);
return SharedString::controlCharacterPHP2OOXML($text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,51 @@
* @coversDefaultClass \PhpOffice\PhpWord\Shared\String
* @runTestsInSeparateProcesses
*/
class StringTest extends \PHPUnit_Framework_TestCase
class SharedStringTest extends \PHPUnit_Framework_TestCase
{
/**
* Is UTF8
*/
public function testIsUTF8()
{
$this->assertTrue(String::isUTF8(''));
$this->assertTrue(String::isUTF8('éééé'));
$this->assertFalse(String::isUTF8(utf8_decode('éééé')));
$this->assertTrue(SharedString::isUTF8(''));
$this->assertTrue(SharedString::isUTF8('éééé'));
$this->assertFalse(SharedString::isUTF8(utf8_decode('éééé')));
}

/**
* OOXML to PHP control character
*/
public function testControlCharacterOOXML2PHP()
{
$this->assertEquals('', String::controlCharacterOOXML2PHP(''));
$this->assertEquals(chr(0x08), String::controlCharacterOOXML2PHP('_x0008_'));
$this->assertEquals('', SharedString::controlCharacterOOXML2PHP(''));
$this->assertEquals(chr(0x08), SharedString::controlCharacterOOXML2PHP('_x0008_'));
}

/**
* PHP to OOXML control character
*/
public function testControlCharacterPHP2OOXML()
{
$this->assertEquals('', String::controlCharacterPHP2OOXML(''));
$this->assertEquals('_x0008_', String::controlCharacterPHP2OOXML(chr(0x08)));
$this->assertEquals('', SharedString::controlCharacterPHP2OOXML(''));
$this->assertEquals('_x0008_', SharedString::controlCharacterPHP2OOXML(chr(0x08)));
}

/**
* Test unicode conversion
*/
public function testToUnicode()
{
$this->assertEquals('a', String::toUnicode('a'));
$this->assertEquals('\uc0{\u8364}', String::toUnicode('€'));
$this->assertEquals('\uc0{\u233}', String::toUnicode('é'));
$this->assertEquals('a', SharedString::toUnicode('a'));
$this->assertEquals('\uc0{\u8364}', SharedString::toUnicode('€'));
$this->assertEquals('\uc0{\u233}', SharedString::toUnicode('é'));
}

/**
* Test remove underscore prefix
*/
public function testRemoveUnderscorePrefix()
{
$this->assertEquals('item', String::removeUnderscorePrefix('_item'));
$this->assertEquals('item', SharedString::removeUnderscorePrefix('_item'));
}
}