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, 'description' => $this->description, 'tag' => $this->tag, '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)); } }