You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
72 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Parser\DocxParser;
|
|
|
|
use App\Parser\DocxParser\Traits\Helper;
|
|
use PhpOffice\PhpWord\Style;
|
|
use PhpOffice\PhpWord\Element\Title as WordTitle;
|
|
|
|
class Title
|
|
{
|
|
|
|
use Helper;
|
|
|
|
public function handle($element)
|
|
{
|
|
if (! $element instanceof WordTitle) {
|
|
return;
|
|
}
|
|
|
|
$title = $element->getText();
|
|
if (! is_string($title)) {
|
|
$handler = $this->getHandler($title);
|
|
|
|
return $handler->handle($title);
|
|
}
|
|
|
|
//dd($element->getText(),get_class_methods($element),$element->getDepth());
|
|
|
|
$style = $this->getTitleStyle($element);
|
|
$headings = [
|
|
'Title' => 'h1',
|
|
'Subtitle' => 'h2',
|
|
'Heading1' => 'h1',
|
|
'Heading2' => 'h2',
|
|
'Heading3' => 'h3',
|
|
'Heading4' => 'h4',
|
|
'Heading5' => 'h5',
|
|
];
|
|
$fontStyle = $style[ 'font' ]->getStyleValues();
|
|
$inlineStyle = $this->getInlineStyles(array_merge($fontStyle[ 'style' ], $fontStyle[ 'basic' ]));
|
|
$heading = array_key_exists($style[ 'heading' ], $headings) ? $headings[ $style[ 'heading' ] ] : 'h5';
|
|
|
|
return [
|
|
'content' => [
|
|
'content' => '<'.$heading.(($inlineStyle) ? ' style="'.$inlineStyle.'"' : '').'>'.$element->getText().'</'.$heading.'>',
|
|
'type' => 'title',
|
|
],
|
|
'type' => 'title',
|
|
'depth' => null,
|
|
'styleDepth' => $this->getStyleListDepth($element->getStyle()),
|
|
'styleName' => $element->getStyle(),
|
|
'index' => $element->getElementIndex(),
|
|
'children' => []
|
|
];
|
|
|
|
}
|
|
|
|
|
|
private function getTitleStyle($element)
|
|
{
|
|
if (strpos($element->getStyle(), 'Heading') !== false) {
|
|
$font = Style::getStyle(str_replace('Heading', 'Heading_', $element->getStyle()));
|
|
} else {
|
|
$font = Style::getStyle($element->getStyle());
|
|
}
|
|
|
|
return [
|
|
'font' => $font,
|
|
'heading' => $element->getStyle()
|
|
];
|
|
}
|
|
}
|