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.
 
 
 
 
 
 

119 lines
3.0 KiB

<?php
namespace App\SearchDisplace\Searchers;
use Illuminate\Support\Facades\Storage;
abstract class SearcherCreator
{
protected $name;
protected $id;
protected $description;
protected $tag;
protected $rows = [];
protected $storage;
protected $searchersCollection;
public function __construct($name, $tag, $description)
{
$this->name = $name;
$this->tag = $tag;
$this->description = $description;
$this->id = $this->generateID();
$this->storage = Storage::disk('local');
$this->searchersCollection = new SearchersCollection();
}
abstract public function create();
protected function store()
{
$id = "{$this->id}_{$this->name}";
if ($this->tag) {
$id = "{$id}_{$this->tag}";
}
return $this->save($id);
}
public function update($id)
{
$result = explode('_', $id);
$uniqueId = $result[0];
$newId = $uniqueId . '_' . $this->name;
if ($this->tag) {
$newId = "{$newId}_{$this->tag}";
}
if ($id !== $newId) {
$this->storage->move("searchers/$id.json", "searchers/$newId.json");
}
return $this->save($newId);
}
public function save($id)
{
$this->rows = $this->processRows($this->rows);
$contents = [
'id' => $id,
'name' => $this->name,
'tag' => $this->tag,
'description' => $this->description,
'rows' => $this->rows,
];
$this->storage->put("searchers/$id.json", json_encode($contents));
return array_merge($contents, [
'id' => $id,
]);
}
protected function processRows($rows)
{
foreach ($rows as $rowIndex => $row) {
foreach ($row as $columnIndex => $column) {
if (array_key_exists('id', $column)) {
$rows[$rowIndex][$columnIndex]['id'] = $this->generateNewIDFromID($column);
// if ( ! array_key_exists('rows', $column)) {
// $searcher = $this->searchersCollection->get($column['id']);
//
// $rows[$rowIndex][$columnIndex]['rows'] = $searcher['rows'];
// }
}
if (array_key_exists('rows', $rows[$rowIndex][$columnIndex])) {
$rows[$rowIndex][$columnIndex]['rows'] = $this->processRows($rows[$rowIndex][$columnIndex]['rows']);
}
}
}
return $rows;
}
protected function generateNewIDFromID($searcher)
{
if (array_key_exists('type', $searcher) && $searcher['type'] === 'duckling') {
return $searcher['id'];
}
$result = explode('_', $searcher['id']);
if (count($result) === 1) {
return $result[0];
}
return $this->generateID() . '_' . $result[1];
}
protected function generateID()
{
return md5(uniqid(rand(), true));
}
}