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.
98 lines
2.5 KiB
98 lines
2.5 KiB
<?php
|
|
|
|
namespace App\SearchDisplace;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\SearchDisplace\Output\HtmlOutput;
|
|
|
|
class SearchAndDisplaceJSON
|
|
{
|
|
protected $file;
|
|
protected $searchers;
|
|
protected $searchOnly;
|
|
protected $content;
|
|
|
|
public function __construct($file, $searchers, $searchOnly)
|
|
{
|
|
$searchOnly ? $this->content = $file : $this->file = $file . '/document.json';
|
|
$this->searchers = $searchers;
|
|
|
|
$this->storage = Storage::disk('local');
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
if(isset($this->content)) {
|
|
$search = new SearchAndDisplace(
|
|
stripslashes($this->content),
|
|
[
|
|
'searchers' => $this->searchers,
|
|
],
|
|
true
|
|
);
|
|
|
|
return $search->execute();
|
|
}
|
|
|
|
if(! $this->storage->exists("contracts/$this->file")) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$content = $this->getContent();
|
|
} catch (\Exception $exception) {
|
|
\Illuminate\Support\Facades\Log::info('EXCEPTION: ' . $exception->getMessage());
|
|
|
|
return;
|
|
}
|
|
|
|
$searchDisplace = $this->applySD($content);
|
|
|
|
$convert = new HtmlOutput($searchDisplace);
|
|
|
|
return $convert->getData();
|
|
}
|
|
|
|
protected function getContent()
|
|
{
|
|
return json_decode($this->storage->get("contracts/$this->file"));
|
|
}
|
|
|
|
protected function applySD($elements)
|
|
{
|
|
$indexes = [];
|
|
|
|
foreach($elements as $element) {
|
|
if($element->tag === 'img')
|
|
continue;
|
|
|
|
$search = new SearchAndDisplace(
|
|
stripslashes($element->content),
|
|
[
|
|
'searchers' => $this->searchers,
|
|
],
|
|
false,
|
|
true
|
|
);
|
|
|
|
$changed = $search->execute();
|
|
|
|
if($changed) {
|
|
foreach($changed['indexes'] as $key => $searcher) {
|
|
foreach($searcher as $change) {
|
|
if($change['start']) {
|
|
$indexes[$key][] = $change;
|
|
$element->paragraph = $key;
|
|
}
|
|
}
|
|
}
|
|
$element->content = $changed['content'];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'content' => $elements,
|
|
'indexes' => $indexes
|
|
];
|
|
}
|
|
}
|