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.
117 lines
2.7 KiB
117 lines
2.7 KiB
<?php
|
|
|
|
namespace App\Parser\DocxParser\Traits;
|
|
|
|
use ReflectionClass;
|
|
|
|
trait Helper
|
|
{
|
|
|
|
/**
|
|
* @param $element
|
|
*
|
|
* @return string
|
|
* @throws \Exception
|
|
*/
|
|
public function getHandler($element)
|
|
{
|
|
try {
|
|
$reflectClass = $this->getReflectionClass($element);
|
|
} catch (\Exception $exception) {
|
|
throw new \Exception($exception->getMessage());
|
|
}
|
|
$handleClass = 'App\Parser\DocxParser\\'.$reflectClass;
|
|
if (class_exists($handleClass)) {
|
|
return new $handleClass;
|
|
} else {
|
|
throw new \Exception("Handler class $handleClass dose not exists!");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $element
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getReflectionClass($element)
|
|
{
|
|
try {
|
|
$reflectClass = new ReflectionClass($element);
|
|
} catch (\ReflectionException $e) {
|
|
throwException($e);
|
|
}
|
|
|
|
return $reflectClass->getShortName();
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the child elements of an element
|
|
*
|
|
* @param $element
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function getElements($element)
|
|
{
|
|
return $element->getElements();
|
|
}
|
|
|
|
|
|
/**
|
|
* Check if an element has childrens
|
|
*
|
|
* @param $element
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function hasElements($element)
|
|
{
|
|
return (bool) count($this->getElements($element));
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $styles
|
|
*
|
|
* @return string
|
|
*/
|
|
private function getInlineStyles($styles)
|
|
{
|
|
$styleString = '';
|
|
$acceptedInline = [
|
|
"dStrike" => 'text-decoration: line-through;text-decoration-style: double;',
|
|
"smallCaps" => 'text-transform: lowercase;',
|
|
"allCaps" => 'text-transform: capitalize;',
|
|
"fgColor" => 'background-color:'.$styles[ 'fgColor' ].';',
|
|
"hidden" => 'display:none;',
|
|
"size" => 'font-size:'.$styles[ 'size' ].'pt;',
|
|
"color" => 'color:#'.$styles[ 'color' ].';'
|
|
];
|
|
|
|
foreach ($styles as $style => $value) {
|
|
if (array_key_exists($style, $acceptedInline) && $value && ! in_array($value, ['none', 'auto'])) {
|
|
$styleString .= $acceptedInline[ $style ];
|
|
}
|
|
}
|
|
|
|
return $styleString;
|
|
}
|
|
|
|
|
|
public function getStyleListDepth($styleName)
|
|
{
|
|
|
|
$getNumberFromStyleName = filter_var($styleName, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
|
|
if (is_numeric($getNumberFromStyleName) && strpos(strtolower($styleName), 'definition') === false) {
|
|
$depth = (int) $getNumberFromStyleName - 1;
|
|
|
|
} else {
|
|
$depth = null;
|
|
|
|
}
|
|
|
|
return $depth;
|
|
}
|
|
}
|