Alex Puiu
2 years ago
2 changed files with 84 additions and 27 deletions
@ -0,0 +1,71 @@ |
|||
<?php |
|||
|
|||
namespace App\SearchDisplace\Output; |
|||
|
|||
class HtmlOutput { |
|||
protected $data; |
|||
protected $content; |
|||
protected $indexes = []; |
|||
protected $additions = []; |
|||
|
|||
public function __construct($data) |
|||
{ |
|||
$this->content = $data['content']; |
|||
$this->indexes = $data['indexes']; |
|||
} |
|||
|
|||
public function getData() |
|||
{ |
|||
$html = $this->convertToHTML(); |
|||
|
|||
$indexes = $this->updateIndexes(); |
|||
|
|||
return [ |
|||
'content' => $html, |
|||
'indexes' => $indexes |
|||
]; |
|||
} |
|||
|
|||
protected function convertToHTML() |
|||
{ |
|||
$html = ''; |
|||
$url = url('/') . '/contracts-images'; |
|||
|
|||
foreach($this->content as $key => $element) { |
|||
$diff = 0; |
|||
if($element->tag !== 'img') { |
|||
$html .= "<$element->tag style=\"$element->style\">$element->content</$element->tag>";
|
|||
} else { |
|||
$src = $url . '/' . str_replace(' ', '%20', $element->src); |
|||
$html .= "<img $element->style src=\"$src\" alt=\"$element->details\">";
|
|||
} |
|||
|
|||
if($key !== array_key_last($this->content)) { |
|||
$html .= '<br>'; |
|||
$diff = 4; |
|||
} |
|||
|
|||
if(isset($element->paragraph)) { |
|||
$this->additions[$element->paragraph][] = strlen($html) - strlen($element->content) - strlen("</$element->tag>") - $diff; |
|||
} |
|||
} |
|||
|
|||
return $html; |
|||
} |
|||
|
|||
protected function updateIndexes() |
|||
{ |
|||
$indexes = $this->indexes; |
|||
|
|||
foreach($this->additions as $akey => $addition) { |
|||
foreach($addition as $ckey => $change) { |
|||
$indexes[$akey][$ckey]['start'] += $change; |
|||
$indexes[$akey][$ckey]['end'] += $change; |
|||
$indexes[$akey][$ckey]['original_start'] += $change; |
|||
$indexes[$akey][$ckey]['original_end'] += $change; |
|||
} |
|||
} |
|||
|
|||
return $indexes; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue