Repo for the search and displace core module including the interface to select files and search and displace operations to run on them. https://searchanddisplace.com
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.
 
 
 
 
 
 

58 lines
1.3 KiB

<?php
namespace App\SearchDisplace;
use Symfony\Component\Process\Process;
class SearchAndDisplaceXML
{
protected $file;
protected $searchers;
protected $type;
public function __construct($file, $searchers, $type = 'odt')
{
$this->file = $file;
$this->searchers = $searchers;
$this->type = $type;
}
public function execute()
{
$this->applySD();
$this->convertToOriginalFileType();
}
protected function convertToOriginalFileType()
{
(new Process(['soffice', '--convert-to', $this->type, $this->file, '--outdir', storage_path('app/tmp/')]))->run();
}
protected function applySD()
{
$dom = new \DOMDocument();
$dom->load($this->file);
foreach($dom->getElementsByTagName('span') as $p) {
$search = new SearchAndDisplace(
stripslashes($p->textContent),
[
'searchers' => $this->searchers,
],
false,
true
);
$changed = $search->execute();
if(!$changed) {
continue;
}
$p->textContent = $changed['content'];
}
$dom->save($this->file);
}
}