Skip to content

Commit

Permalink
use library
Browse files Browse the repository at this point in the history
  • Loading branch information
cancan101 committed Nov 22, 2024
1 parent ed3c430 commit ab8ad90
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 63 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"doctrine/dbal": "^2.12 || ^3.5",
"dragonmantank/cron-expression": "^3.1",
"league/flysystem-sftp-v3": "^3.0",
"mtdowling/jmespath.php": "^2.8",
"nesbot/carbon": "^2.27",
"phpoffice/phpspreadsheet": "^1.24 || ^2.2",
"pimcore/compatibility-bridge-v10": "^1.0",
Expand Down
72 changes: 9 additions & 63 deletions src/DataSource/Interpreter/JsonFileInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Pimcore\Bundle\DataImporterBundle\DataSource\Interpreter;

use JmesPath;
use Pimcore\Bundle\DataImporterBundle\PimcoreDataImporterBundle;
use Pimcore\Bundle\DataImporterBundle\Preview\Model\PreviewData;

Expand Down Expand Up @@ -97,18 +98,18 @@ public function fileValid(string $path, bool $originalFilename = false): bool

$data = json_decode($this->prepareContent($content), true);

if (!$this->isJsonValid($data)) {
if (json_last_error() === JSON_ERROR_NONE) {
$this->cachedContent = $data;
$this->cachedFilePath = $path;

return true;
} else {
$this->applicationLogger->error('Reading file ERROR: ' . json_last_error_msg(), [
'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $this->configName
]);

return false;
}

$this->cachedContent = $data;
$this->cachedFilePath = $path;

return true;
}

public function previewData(string $path, int $recordNumber = 0, array $mappedColumns = []): PreviewData
Expand Down Expand Up @@ -140,66 +141,11 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo
return new PreviewData($columns, $previewData, $readRecordNumber, $mappedColumns);
}

/**
* Returns false if any errors occurred during the last JSON decoding or
* if the user-specified path wasn't found in the array `$data`
*/
private function isJsonValid(?array $data): bool
{
if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}

if (!empty($this->path) && !$this->isValueOnPath($this->path, $data)) {
return false;
}

return true;
}

/**
* Checks if a value exists at the specified path in a nested array `$data`.
*
* This method takes a path string (in the form of '/key1/key2/.../keyN') and traverses
* the provided array `$data` to determine if the specified path exists.
*
* @param string $path The path to check, represented as a string with keys separated by slashes.
* @param array $data The associative array to search through.
*
* @return bool Returns true if the entire path exists in the data array
* false if any part of the path does not exist or if the value on the path isn't array
*/
private function isValueOnPath(string $path, array $data): bool
{
$pathParts = explode('/', trim($path, '/'));

foreach ($pathParts as $pathPart) {
if (isset($data[$pathPart])) {
$data = $data[$pathPart];
} else {
return false;
}
}

if (!is_array($data)) {
return false;
}

return true;
}

/**
* Returns a value from the specified path in a nested array `$data`.
* Validation is done by the function `JsonFileInterpreter::isValueOnPath`
*/
private function getValueFromPath(string $path, array $data): array
private function getValueFromPath(string $path, array $data): mixed
{
$pathParts = explode('/', trim($path, '/'));

foreach ($pathParts as $pathPart) {
$data = $data[$pathPart];
}

return $data;
return JmesPath\Env::search($path, $data);
}
}

0 comments on commit ab8ad90

Please sign in to comment.