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.
74 lines
2.6 KiB
74 lines
2.6 KiB
<?php
|
|
|
|
namespace App\Parser\DocxParser;
|
|
|
|
use App\Parser\DocxParser\Traits\Helper;
|
|
use Exception;
|
|
|
|
class TextRun
|
|
{
|
|
|
|
use Helper;
|
|
|
|
public function handle($textRun)
|
|
{
|
|
$result = [];
|
|
$textRunElements = $this->getElements($textRun);
|
|
if (count($textRunElements)) {
|
|
foreach ($textRunElements as $index => $element) {
|
|
|
|
try {
|
|
$handler = $this->getHandler($element);
|
|
$data = $handler->handle($element);
|
|
if ($data) {
|
|
$styleName = $textRun->getParagraphStyle()->getStyleName();
|
|
if ($index === 0) {
|
|
$result[] = [
|
|
'content' => $handler->handle($element),
|
|
'type' => 'textRun',
|
|
'depth' => $textRun->getDepth(),
|
|
'styleDepth' => $this->getStyleListDepth($styleName),
|
|
'styleName' => $styleName,
|
|
'index' => $textRun->getElementIndex(),
|
|
'children' => []
|
|
];
|
|
} else {
|
|
if (isset($result[ count($result) - 1 ])) {
|
|
|
|
$result[ count($result) - 1 ][ 'content' ][ 'content' ] .= ' '.$data[ 'content' ];
|
|
|
|
} else {
|
|
$result[] = [
|
|
'content' => $data,
|
|
'type' => 'textRun',
|
|
'depth' => (int) $textRun->getDepth(),
|
|
'styleDepth' => $this->getStyleListDepth($styleName),
|
|
'styleName' => $styleName,
|
|
'index' => $textRun->getElementIndex(),
|
|
'children' => []
|
|
|
|
];
|
|
}
|
|
}
|
|
|
|
}
|
|
} catch (Exception $e) {
|
|
dd($e, 2);
|
|
throw new Exception($e->getMessage());
|
|
}
|
|
|
|
|
|
}
|
|
if ($result) {
|
|
if (count($result) === 1) {
|
|
$result = reset($result);
|
|
$result[ 'content' ][ 'content' ] = '<p>'.$result[ 'content' ][ 'content' ].'</p>';
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
}
|