SmartCane/laravel_app/app/PivotNameAnalyser.php

80 lines
2.3 KiB
PHP

<?php
namespace App;
use App\Models\Project;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\File;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\HeadingRowImport;
class PivotNameAnalyser
{
private string $geoJsonPath;
private string $excelPath;
public function __construct(string $geoJsonPath, string $excelPath)
{
$this->geoJsonPath = $geoJsonPath;
$this->excelPath = $excelPath;
}
public function getGeoJsonPivotNames():Collection
{
$geoJson = json_decode(file_get_contents($this->geoJsonPath), true);
$pivotNames = collect([]);
foreach ($geoJson['features'] as $feature) {
$pivotNames->add( $feature['properties']['field']);
}
return $pivotNames->unique();
}
public function getHarvestDataPivotNames():Collection
{
$excelData = Excel::toCollection(new \App\Imports\ExcelFileImport(), $this->excelPath);
$sheetOne = $excelData->toArray()[0];
$fieldNames = collect([]);
foreach ($sheetOne as $rowIndex => $row) {
if ($rowIndex === 0) {
foreach ($row as $columnIndex => $cell) {
if (strtolower($cell) === 'field') {
$fieldColumnName = $columnIndex;
}
}
continue;
}
$fieldNames->add(trim($row[$fieldColumnName]));
}
return $fieldNames->unique();
}
public function getCommonPivotNames():Collection
{
return $this->getGeoJsonPivotNames()->intersect($this->getHarvestDataPivotNames());
}
public function getGeoJsonPivotNamesNotInHarvest():Collection
{
return $this->getGeoJsonPivotNames()->diff($this->getHarvestDataPivotNames());
}
public function getHarvestPivotNamesNotInGeoJson():Collection
{
return $this->getHarvestDataPivotNames()->diff($this->getGeoJsonPivotNames());
}
public function hasErrors():bool
{
return $this->getGeoJsonPivotNamesNotInHarvest()->isNotEmpty() || $this->getHarvestPivotNamesNotInGeoJson()->isNotEmpty();
}
}