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.
64 lines
1.6 KiB
64 lines
1.6 KiB
<?php
|
|
|
|
namespace App\SearchDisplace;
|
|
|
|
use App\SearchDisplace\Searchers\Searcher;
|
|
|
|
class SearchAndDisplace
|
|
{
|
|
protected $documentContent;
|
|
protected $info;
|
|
|
|
public function __construct($documentContent, $info)
|
|
{
|
|
$this->documentContent = $documentContent;
|
|
$this->info = $info;
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$searchResult = $this->search();
|
|
|
|
return $this->displace($searchResult);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
protected function search()
|
|
{
|
|
$searcher = new Searcher($this->info['searchers'], $this->documentContent);
|
|
|
|
return $searcher->execute();
|
|
}
|
|
|
|
protected function displace($searchResult)
|
|
{
|
|
$replacements = [];
|
|
|
|
foreach ($this->info['searchers'] as $searcher) {
|
|
$replacements[$searcher['key']] = $searcher['replace_with'];
|
|
}
|
|
|
|
$updatedDocumentContent = '';
|
|
$currentIndex = 0;
|
|
|
|
foreach ($searchResult as $item) {
|
|
$partialContent = substr($this->documentContent, $currentIndex, $item['start'] - $currentIndex);
|
|
|
|
$updatedDocumentContent = $updatedDocumentContent . $partialContent;
|
|
|
|
$updatedDocumentContent = $updatedDocumentContent . $replacements[$item['dim']];
|
|
|
|
$currentIndex = $item['end'];
|
|
}
|
|
|
|
if ($currentIndex < strlen($this->documentContent) - 1) {
|
|
$updatedDocumentContent = $updatedDocumentContent . substr($this->documentContent, $currentIndex);
|
|
}
|
|
|
|
return $updatedDocumentContent;
|
|
}
|
|
}
|