Skip to content

Commit

Permalink
Support for padding in a table cell. fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
OlisaevAG authored and OlisaevAG committed Nov 19, 2024
1 parent 3e182f0 commit ab9e012
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
23 changes: 14 additions & 9 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,24 +805,24 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
break;

case 'padding':
$valueTop = $valueRight = $valueBottom = $valueLeft = null;
$cValue = preg_replace('# +#', ' ', trim($value));
$paddingArr = explode(' ', $cValue);
$valueTop = $valueRight = $valueBottom = $valueLeft = null;
$cValue = preg_replace('# +#', ' ', trim($value));
$paddingArr = explode(' ', $cValue);
$countParams = count($paddingArr);
if ($countParams == 1) {
$valueTop = $valueRight = $valueBottom = $valueLeft = $paddingArr[0];
} elseif ($countParams == 2) {
$valueTop = $valueBottom = $paddingArr[0];
$valueTop = $valueBottom = $paddingArr[0];
$valueRight = $valueLeft = $paddingArr[1];
} elseif ($countParams == 3) {
$valueTop = $paddingArr[0];
$valueRight = $valueLeft = $paddingArr[1];
$valueTop = $paddingArr[0];
$valueRight = $valueLeft = $paddingArr[1];
$valueBottom = $paddingArr[2];
} elseif ($countParams == 4) {
$valueTop = $paddingArr[0];
$valueRight = $paddingArr[1];
$valueTop = $paddingArr[0];
$valueRight = $paddingArr[1];
$valueBottom = $paddingArr[2];
$valueLeft = $paddingArr[3];
$valueLeft = $paddingArr[3];
}
if ($valueTop !== null) {
$styles['paddingTop'] = Converter::cssToTwip($valueTop);
Expand All @@ -836,18 +836,23 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
if ($valueLeft !== null) {
$styles['paddingLeft'] = Converter::cssToTwip($valueLeft);
}

break;
case 'padding-top':
$styles['paddingTop'] = Converter::cssToTwip($value);

break;
case 'padding-right':
$styles['paddingRight'] = Converter::cssToTwip($value);

break;
case 'padding-bottom':
$styles['paddingBottom'] = Converter::cssToTwip($value);

break;
case 'padding-left':
$styles['paddingLeft'] = Converter::cssToTwip($value);

break;

case 'border-color':
Expand Down
24 changes: 15 additions & 9 deletions src/PhpWord/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,60 +377,66 @@ public function getNoWrap(): bool
return $this->noWrap;
}


/**
* Get style padding-top.
*
* @return mixed
*/
public function getPaddingTop()
{
return $this->paddingTop;
}

/**
* Set style padding-top.
* @param int $value
*
* @return $this
*/
public function setPaddingTop(int $value): Cell
public function setPaddingTop(int $value): self
{
$this->paddingTop = $value;

return $this;
}

/**
* Get style padding-bottom.
*
* @return mixed
*/
public function getPaddingBottom()
{
return $this->paddingBottom;
}

/**
* Set style padding-bottom.
* @param int $value
*
* @return $this
*/
public function setPaddingBottom(int $value): Cell
public function setPaddingBottom(int $value): self
{
$this->paddingBottom = $value;

return $this;
}

/**
* Get style padding-left.
*
* @return mixed
*/
public function getPaddingLeft()
{
return $this->paddingLeft;
}

/**
* Set style padding-left.
* @param int $value
*
* @return $this
*/
public function setPaddingLeft(int $value): Cell
public function setPaddingLeft(int $value): self
{
$this->paddingLeft = $value;

Expand All @@ -439,6 +445,7 @@ public function setPaddingLeft(int $value): Cell

/**
* Get style padding-right.
*
* @return mixed
*/
public function getPaddingRight()
Expand All @@ -448,11 +455,10 @@ public function getPaddingRight()

/**
* Set style padding-right.
* @param int $value
*
* @return $this
*/
public function setPaddingRight(int $value): Cell
public function setPaddingRight(int $value): self
{
$this->paddingRight = $value;

Expand Down
10 changes: 5 additions & 5 deletions src/PhpWord/Writer/Word2007/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ public function write(): void
$paddingBottom = $style->getPaddingBottom();
$paddingRight = $style->getPaddingRight();

if($paddingTop !== null || $paddingLeft !== null || $paddingBottom !== null || $paddingRight !== null){
if ($paddingTop !== null || $paddingLeft !== null || $paddingBottom !== null || $paddingRight !== null) {
$xmlWriter->startElement('w:tcMar');
if($paddingTop !== null) {
if ($paddingTop !== null) {
$xmlWriter->startElement('w:top');
$xmlWriter->writeAttribute('w:w', $paddingTop);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:top
}
if($paddingLeft !== null) {
if ($paddingLeft !== null) {
$xmlWriter->startElement('w:start');
$xmlWriter->writeAttribute('w:w', $paddingLeft);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:start
}
if($paddingBottom !== null) {
if ($paddingBottom !== null) {
$xmlWriter->startElement('w:bottom');
$xmlWriter->writeAttribute('w:w', $paddingBottom);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:bottom
}
if($paddingRight !== null){
if ($paddingRight !== null) {
$xmlWriter->startElement('w:end');
$xmlWriter->writeAttribute('w:w', $paddingRight);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
Expand Down

0 comments on commit ab9e012

Please sign in to comment.