Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
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

  1. <?php
  2. namespace App\Parser\DocxParser\Traits;
  3. use ReflectionClass;
  4. trait Helper
  5. {
  6. /**
  7. * @param $element
  8. *
  9. * @return string
  10. * @throws \Exception
  11. */
  12. public function getHandler($element)
  13. {
  14. try {
  15. $reflectClass = $this->getReflectionClass($element);
  16. } catch (\Exception $exception) {
  17. throw new \Exception($exception->getMessage());
  18. }
  19. $handleClass = 'App\Parser\DocxParser\\'.$reflectClass;
  20. if (class_exists($handleClass)) {
  21. return new $handleClass;
  22. } else {
  23. throw new \Exception("Handler class $handleClass dose not exists!");
  24. }
  25. }
  26. /**
  27. * @param $element
  28. *
  29. * @return string
  30. */
  31. public function getReflectionClass($element)
  32. {
  33. try {
  34. $reflectClass = new ReflectionClass($element);
  35. } catch (\ReflectionException $e) {
  36. throwException($e);
  37. }
  38. return $reflectClass->getShortName();
  39. }
  40. /**
  41. * Get the child elements of an element
  42. *
  43. * @param $element
  44. *
  45. * @return mixed
  46. */
  47. public function getElements($element)
  48. {
  49. return $element->getElements();
  50. }
  51. /**
  52. * Check if an element has childrens
  53. *
  54. * @param $element
  55. *
  56. * @return bool
  57. */
  58. public function hasElements($element)
  59. {
  60. return (bool) count($this->getElements($element));
  61. }
  62. /**
  63. * @param $styles
  64. *
  65. * @return string
  66. */
  67. private function getInlineStyles($styles)
  68. {
  69. $styleString = '';
  70. $acceptedInline = [
  71. "dStrike" => 'text-decoration: line-through;text-decoration-style: double;',
  72. "smallCaps" => 'text-transform: lowercase;',
  73. "allCaps" => 'text-transform: capitalize;',
  74. "fgColor" => 'background-color:'.$styles[ 'fgColor' ].';',
  75. "hidden" => 'display:none;',
  76. "size" => 'font-size:'.$styles[ 'size' ].'pt;',
  77. "color" => 'color:#'.$styles[ 'color' ].';'
  78. ];
  79. foreach ($styles as $style => $value) {
  80. if (array_key_exists($style, $acceptedInline) && $value && ! in_array($value, ['none', 'auto'])) {
  81. $styleString .= $acceptedInline[ $style ];
  82. }
  83. }
  84. return $styleString;
  85. }
  86. public function getStyleListDepth($styleName)
  87. {
  88. $getNumberFromStyleName = filter_var($styleName, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
  89. if (is_numeric($getNumberFromStyleName) && strpos(strtolower($styleName), 'definition') === false) {
  90. $depth = (int) $getNumberFromStyleName - 1;
  91. } else {
  92. $depth = null;
  93. }
  94. return $depth;
  95. }
  96. }