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

3 years ago
3 years ago
  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 $tag;
  10. protected $rows = [];
  11. protected $storage;
  12. protected $searchersCollection;
  13. public function __construct($name, $tag, $description)
  14. {
  15. $this->name = $name;
  16. $this->tag = $tag;
  17. $this->description = $description;
  18. $this->id = $this->generateID();
  19. $this->storage = Storage::disk('local');
  20. $this->searchersCollection = new SearchersCollection();
  21. }
  22. abstract public function create();
  23. protected function store()
  24. {
  25. $id = "{$this->id}_{$this->name}";
  26. if ($this->tag) {
  27. $id = "{$id}_{$this->tag}";
  28. }
  29. return $this->save($id);
  30. }
  31. public function update($id)
  32. {
  33. $result = explode('_', $id);
  34. $uniqueId = $result[0];
  35. $newId = $uniqueId . '_' . $this->name;
  36. if ($this->tag) {
  37. $newId = "{$newId}_{$this->tag}";
  38. }
  39. if ($id !== $newId) {
  40. $this->storage->move("searchers/$id.json", "searchers/$newId.json");
  41. }
  42. return $this->save($newId);
  43. }
  44. public function save($id)
  45. {
  46. $this->rows = $this->processRows($this->rows);
  47. $contents = [
  48. 'id' => $id,
  49. 'name' => $this->name,
  50. 'description' => $this->description,
  51. 'tag' => $this->tag,
  52. 'rows' => $this->rows,
  53. ];
  54. $this->storage->put("searchers/$id.json", json_encode($contents));
  55. return array_merge($contents, [
  56. 'id' => $id,
  57. ]);
  58. }
  59. protected function processRows($rows)
  60. {
  61. foreach ($rows as $rowIndex => $row) {
  62. foreach ($row as $columnIndex => $column) {
  63. if (array_key_exists('id', $column)) {
  64. $rows[$rowIndex][$columnIndex]['id'] = $this->generateNewIDFromID($column);
  65. // if ( ! array_key_exists('rows', $column)) {
  66. // $searcher = $this->searchersCollection->get($column['id']);
  67. //
  68. // $rows[$rowIndex][$columnIndex]['rows'] = $searcher['rows'];
  69. // }
  70. }
  71. if (array_key_exists('rows', $rows[$rowIndex][$columnIndex])) {
  72. $rows[$rowIndex][$columnIndex]['rows'] = $this->processRows($rows[$rowIndex][$columnIndex]['rows']);
  73. }
  74. }
  75. }
  76. return $rows;
  77. }
  78. protected function generateNewIDFromID($searcher)
  79. {
  80. if (array_key_exists('type', $searcher) && $searcher['type'] === 'duckling') {
  81. return $searcher['id'];
  82. }
  83. $result = explode('_', $searcher['id']);
  84. if (count($result) === 1) {
  85. return $result[0];
  86. }
  87. return $this->generateID() . '_' . $result[1];
  88. }
  89. protected function generateID()
  90. {
  91. return md5(uniqid(rand(), true));
  92. }
  93. }