Skip to content

Commit d25dc96

Browse files
committed
ODT Writer: Link writing
1 parent 8ace1c1 commit d25dc96

File tree

10 files changed

+92
-39
lines changed

10 files changed

+92
-39
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This release marked heavy refactorings on internal code structure with the creat
3737
- DOCX Writer: Change `docProps/app.xml` `Application` to `PHPWord` - @ivanlanin
3838
- DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin
3939
- ODT Writer: Basic image writing - @ivanlanin
40+
- ODT Writer: Link writing - @ivanlanin
4041

4142
### Bugfixes
4243

@@ -55,6 +56,8 @@ This release marked heavy refactorings on internal code structure with the creat
5556
- `Footnote::addFootnoteLinkElement` replaced by `Media::addElement`
5657
- `Footnote::getFootnoteLinkElements` replaced by `Media::getElements`
5758
- All current methods on `Media`
59+
- `Element\Link::getLinkSrc` replaced by `Element\Link::getTarget`
60+
- `Element\Link::getLinkName` replaced by `Element\Link::getText`
5861

5962
### Miscellaneous
6063

docs/intro.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Writers
7777
+---------------------------+----------------------+--------+-------+-------+--------+-------+
7878
| | Title || | |||
7979
+---------------------------+----------------------+--------+-------+-------+--------+-------+
80-
| | Link || | |||
80+
| | Link || | |||
8181
+---------------------------+----------------------+--------+-------+-------+--------+-------+
8282
| | Preserve Text || | | | |
8383
+---------------------------+----------------------+--------+-------+-------+--------+-------+

docs/src/documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Below are the supported features for each file formats.
8888
| **Element Type** | Text ||||||
8989
| | Text Run ||||||
9090
| | Title || | |||
91-
| | Link || | |||
91+
| | Link || | |||
9292
| | Preserve Text || | | | |
9393
| | Text Break ||||||
9494
| | Page Break || | | | |

src/PhpWord/Element/Link.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
class Link extends AbstractElement
1919
{
2020
/**
21-
* Link source
21+
* Link target
2222
*
2323
* @var string
2424
*/
25-
private $source;
25+
private $target;
2626

2727
/**
28-
* Link name
28+
* Link text
2929
*
3030
* @var string
3131
*/
32-
private $name;
32+
private $text;
3333

3434
/**
3535
* Font style
@@ -54,34 +54,34 @@ class Link extends AbstractElement
5454
* @param mixed $fontStyle
5555
* @param mixed $paragraphStyle
5656
*/
57-
public function __construct($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null)
57+
public function __construct($target, $text = null, $fontStyle = null, $paragraphStyle = null)
5858
{
59-
$this->source = $linkSrc;
60-
$this->name = $linkName;
59+
$this->target = $target;
60+
$this->text = is_null($text) ? $target : $text;
6161
$this->fontStyle = $this->setStyle(new Font('text'), $fontStyle);
6262
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
6363

6464
return $this;
6565
}
6666

6767
/**
68-
* Get Link source
68+
* Get link target
6969
*
7070
* @return string
7171
*/
72-
public function getLinkSrc()
72+
public function getTarget()
7373
{
74-
return $this->source;
74+
return $this->target;
7575
}
7676

7777
/**
78-
* Get Link name
78+
* Get link text
7979
*
8080
* @return string
8181
*/
82-
public function getLinkName()
82+
public function getText()
8383
{
84-
return $this->name;
84+
return $this->text;
8585
}
8686

8787
/**
@@ -103,4 +103,26 @@ public function getParagraphStyle()
103103
{
104104
return $this->paragraphStyle;
105105
}
106+
107+
/**
108+
* Get Link source
109+
*
110+
* @return string
111+
* @deprecated 0.10.0
112+
*/
113+
public function getLinkSrc()
114+
{
115+
return $this->getTarget();
116+
}
117+
118+
/**
119+
* Get Link name
120+
*
121+
* @return string
122+
* @deprecated 0.10.0
123+
*/
124+
public function getLinkName()
125+
{
126+
return $this->getText();
127+
}
106128
}

src/PhpWord/Writer/HTML/Element/Link.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,9 @@ class Link extends Element
2323
*/
2424
public function write()
2525
{
26-
$url = $this->element->getLinkSrc();
27-
$text = $this->element->getLinkName();
28-
if ($text == '') {
29-
$text = $url;
30-
}
31-
$html = '';
32-
if (!$this->withoutP) {
33-
$html .= "<p>" . PHP_EOL;
34-
}
35-
$html .= "<a href=\"{$url}\">{$text}</a>" . PHP_EOL;
26+
$html = "<a href=\"{$this->element->getTarget()}\">{$this->element->getText()}</a>" . PHP_EOL;
3627
if (!$this->withoutP) {
37-
$html .= "</p>" . PHP_EOL;
28+
$html = '<p>' . $html . '</p>' . PHP_EOL;
3829
}
3930

4031
return $html;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Writer\ODText\Element;
11+
12+
/**
13+
* Text element writer
14+
*
15+
* @since 0.10.0
16+
*/
17+
class Link extends Element
18+
{
19+
/**
20+
* Write element
21+
*/
22+
public function write()
23+
{
24+
if (!$this->withoutP) {
25+
$this->xmlWriter->startElement('text:p'); // text:p
26+
}
27+
28+
$this->xmlWriter->startElement('text:a');
29+
$this->xmlWriter->writeAttribute('xlink:type', 'simple');
30+
$this->xmlWriter->writeAttribute('xlink:href', $this->element->getTarget());
31+
$this->xmlWriter->writeRaw($this->element->getText());
32+
$this->xmlWriter->endElement(); // text:a
33+
34+
if (!$this->withoutP) {
35+
$this->xmlWriter->endElement(); // text:p
36+
}
37+
}
38+
}

src/PhpWord/Writer/ODText/Element/TextRun.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public function write()
2929
if (count($elements) > 0) {
3030
foreach ($elements as $element) {
3131
if ($element instanceof TextElement) {
32-
$elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $element, true);
32+
$elementWriter = new Text($this->xmlWriter, $this->parentWriter, $element, true);
33+
$elementWriter->write();
34+
} elseif ($element instanceof Link) {
35+
$elementWriter = new Link($this->xmlWriter, $this->parentWriter, $element, true);
3336
$elementWriter->write();
3437
}
3538
}

src/PhpWord/Writer/Word2007/Element/Link.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class Link extends Element
2525
public function write()
2626
{
2727
$rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0);
28-
$linkName = $this->element->getLinkName();
29-
if (is_null($linkName)) {
30-
$linkName = $this->element->getLinkSrc();
31-
}
3228
$fStyle = $this->element->getFontStyle();
3329
$pStyle = $this->element->getParagraphStyle();
3430

@@ -50,7 +46,7 @@ public function write()
5046
$styleWriter->write();
5147
$this->xmlWriter->startElement('w:t');
5248
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
53-
$this->xmlWriter->writeRaw($linkName);
49+
$this->xmlWriter->writeRaw($this->element->getText());
5450
$this->xmlWriter->endElement(); // w:t
5551
$this->xmlWriter->endElement(); // w:r
5652
$this->xmlWriter->endElement(); // w:hyperlink

tests/PhpWord/Tests/Element/LinkTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public function testConstructDefault()
2828
$oLink = new Link('http://www.google.com');
2929

3030
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink);
31-
$this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com');
32-
$this->assertEquals($oLink->getLinkName(), null);
31+
$this->assertEquals($oLink->getTarget(), 'http://www.google.com');
32+
$this->assertEquals($oLink->getText(), $oLink->getTarget());
3333
$this->assertEquals($oLink->getFontStyle(), null);
3434
$this->assertEquals($oLink->getParagraphStyle(), null);
3535
}
@@ -47,8 +47,8 @@ public function testConstructWithParamsArray()
4747
);
4848

4949
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink);
50-
$this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com');
51-
$this->assertEquals($oLink->getLinkName(), 'Search Engine');
50+
$this->assertEquals($oLink->getTarget(), 'http://www.google.com');
51+
$this->assertEquals($oLink->getText(), 'Search Engine');
5252
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oLink->getFontStyle());
5353
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oLink->getParagraphStyle());
5454
}

tests/PhpWord/Tests/Element/TextRunTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testAddLink()
9090

9191
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
9292
$this->assertCount(1, $oTextRun->getElements());
93-
$this->assertEquals($element->getLinkSrc(), 'http://www.google.fr');
93+
$this->assertEquals($element->getTarget(), 'http://www.google.fr');
9494
}
9595

9696
/**
@@ -103,8 +103,8 @@ public function testAddLinkWithName()
103103

104104
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
105105
$this->assertCount(1, $oTextRun->getElements());
106-
$this->assertEquals($element->getLinkSrc(), 'http://www.google.fr');
107-
$this->assertEquals($element->getLinkName(), 'ééé');
106+
$this->assertEquals($element->getTarget(), 'http://www.google.fr');
107+
$this->assertEquals($element->getText(), 'ééé');
108108
}
109109

110110
/**

0 commit comments

Comments
 (0)