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.

94 lines
2.4 KiB

  1. <?php
  2. namespace App\SearchDisplace\Searchers;
  3. use Illuminate\Support\Facades\Storage;
  4. abstract class SearcherCreator
  5. {
  6. protected $name;
  7. protected $id;
  8. protected $description;
  9. protected $rows = [];
  10. protected $storage;
  11. protected $searchersCollection;
  12. public function __construct($name, $description)
  13. {
  14. $this->name = $name;
  15. $this->description = $description;
  16. $this->id = $this->generateID();
  17. $this->storage = Storage::disk('local');
  18. $this->searchersCollection = new SearchersCollection();
  19. }
  20. abstract public function create();
  21. protected function store()
  22. {
  23. $id = "{$this->id}_{$this->name}";
  24. return $this->save($id);
  25. }
  26. public function save($id)
  27. {
  28. $this->rows = $this->processRows($this->rows);
  29. $contents = [
  30. 'id' => $id,
  31. 'name' => $this->name,
  32. 'description' => $this->description,
  33. 'rows' => $this->rows,
  34. ];
  35. $this->storage->put("searchers/$id.json", json_encode($contents));
  36. return array_merge($contents, [
  37. 'id' => $id,
  38. ]);
  39. }
  40. protected function processRows($rows)
  41. {
  42. foreach ($rows as $rowIndex => $row) {
  43. foreach ($row as $columnIndex => $column) {
  44. if (array_key_exists('id', $column)) {
  45. $rows[$rowIndex][$columnIndex]['id'] = $this->generateNewIDFromID($column);
  46. // if ( ! array_key_exists('rows', $column)) {
  47. // $searcher = $this->searchersCollection->get($column['id']);
  48. //
  49. // $rows[$rowIndex][$columnIndex]['rows'] = $searcher['rows'];
  50. // }
  51. }
  52. if (array_key_exists('rows', $rows[$rowIndex][$columnIndex])) {
  53. $rows[$rowIndex][$columnIndex]['rows'] = $this->processRows($rows[$rowIndex][$columnIndex]['rows']);
  54. }
  55. }
  56. }
  57. return $rows;
  58. }
  59. protected function generateNewIDFromID($searcher)
  60. {
  61. if (array_key_exists('type', $searcher) && $searcher['type'] === 'duckling') {
  62. return $searcher['id'];
  63. }
  64. $result = explode('_', $searcher['id']);
  65. if (count($result) === 1) {
  66. return $result[0];
  67. }
  68. return $this->generateID() . '_' . $result[1];
  69. }
  70. protected function generateID()
  71. {
  72. return md5(uniqid(rand(), true));
  73. }
  74. }