client = $client; $this->url = $url; } public function load(): ?array { $folder = $this->folder; $files = new \DirectoryIterator($folder); $output = []; foreach ($files as $file) { if ($file->isDir() or $file->getExtension() != 'pdf') { continue; } $output []= ['filename' => $file->getBasename()]; } $response = $this->client->post($this->url, ['json' => ['files' => $output]]); $output = json_decode($response->getBody()); return $this->build($output); } protected function build(array $data): ?array { foreach ($data as &$file) { $i = $this->findStartRow($file->text); if ($i === -1) { continue; } $e = $this->findEndRow($file->text, $i); if ($e == $i) { continue; } $file->data = array_filter($file->text, function($key) use ($i, $e) { return ($key >= $i) and ($key <= $e); }, ARRAY_FILTER_USE_KEY); } return $data; } protected function findStartRow(array $data): int { foreach ($data as $i => $row) { if (!is_array($row)) { continue; } $maybe = false; foreach ($row as $cell) { if (str_contains($cell, '/')) { $maybe = true; } if ($maybe and str_contains($cell, '$')) { return $i - 1; } } } return -1; } protected function findEndRow(array $data, int $start): int { $l = count($data[$start]); for ($i = $start; $i < count($data); $i ++) { if (!is_array($data[$i])) { return $i - 1; } if (count($data[$i]) != $l) { return $i - 1; } } return $start; } }