added dateformat and timezone options to project importer (#2042)
This commit is contained in:
@@ -39,11 +39,11 @@ abstract class AbstractProjectImporter implements ProjectImporterInterface
|
||||
return $this->customerService->findCustomerByName($name);
|
||||
}
|
||||
|
||||
public function convertEntryToProject(array $entry): Project
|
||||
public function convertEntryToProject(array $entry, array $options = []): Project
|
||||
{
|
||||
$project = $this->findProject($entry);
|
||||
|
||||
$this->convertEntry($project, $entry);
|
||||
$this->convertEntry($project, $entry, $options);
|
||||
|
||||
return $project;
|
||||
}
|
||||
@@ -71,7 +71,7 @@ abstract class AbstractProjectImporter implements ProjectImporterInterface
|
||||
if ($customer->getId() !== $project->getCustomer()->getId()) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf(
|
||||
'Customer mismatch for project %s with attached customer %s and new customer %s',
|
||||
'Customer mismatch for project "%s" with attached customer "%s" and new customer "%s"',
|
||||
$project->getName(),
|
||||
$project->getCustomer()->getName(),
|
||||
$customer->getName()
|
||||
@@ -84,7 +84,10 @@ abstract class AbstractProjectImporter implements ProjectImporterInterface
|
||||
|
||||
private function findCustomer(string $customerName): Customer
|
||||
{
|
||||
if (!\array_key_exists($customerName, $this->customerCache)) {
|
||||
$customerName = trim($customerName);
|
||||
$key = strtolower($customerName);
|
||||
|
||||
if (!\array_key_exists($key, $this->customerCache)) {
|
||||
$customer = $this->findCustomerByName($customerName);
|
||||
|
||||
if ($customer === null) {
|
||||
@@ -92,10 +95,10 @@ abstract class AbstractProjectImporter implements ProjectImporterInterface
|
||||
$customer->setName($customerName);
|
||||
}
|
||||
|
||||
$this->customerCache[$customerName] = $customer;
|
||||
$this->customerCache[$key] = $customer;
|
||||
}
|
||||
|
||||
return $this->customerCache[$customerName];
|
||||
return $this->customerCache[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,7 +155,8 @@ abstract class AbstractProjectImporter implements ProjectImporterInterface
|
||||
*
|
||||
* @param Project $project
|
||||
* @param array $entry
|
||||
* @return mixed
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function convertEntry(Project $project, array $entry);
|
||||
abstract protected function convertEntry(Project $project, array $entry, array $options = []): void;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ use App\Entity\ProjectMeta;
|
||||
|
||||
final class DefaultProjectImporter extends AbstractProjectImporter
|
||||
{
|
||||
protected function convertEntry(Project $project, array $row)
|
||||
protected function convertEntry(Project $project, array $row, array $options = []): void
|
||||
{
|
||||
foreach ($row as $name => $value) {
|
||||
switch (strtolower($name)) {
|
||||
@@ -33,7 +33,7 @@ final class DefaultProjectImporter extends AbstractProjectImporter
|
||||
case 'order-number':
|
||||
case 'order number':
|
||||
if (!empty($value)) {
|
||||
$project->setOrderNumber($value);
|
||||
$project->setOrderNumber(substr($value, 0, 20));
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -42,8 +42,20 @@ final class DefaultProjectImporter extends AbstractProjectImporter
|
||||
case 'order date':
|
||||
if (!empty($value)) {
|
||||
$timezone = $project->getCustomer()->getTimezone();
|
||||
if (isset($options['timezone'])) {
|
||||
$timezone = $options['timezone'];
|
||||
}
|
||||
$timezone = new \DateTimeZone($timezone ?? date_default_timezone_get());
|
||||
$project->setOrderDate(new \DateTime($value, $timezone));
|
||||
if (isset($options['dateformat'])) {
|
||||
$date = \DateTime::createFromFormat($options['dateformat'], $value, $timezone);
|
||||
} else {
|
||||
$date = new \DateTime($value, $timezone);
|
||||
}
|
||||
if ($date instanceof \DateTime) {
|
||||
$project->setOrderDate($date);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Invalid order date: ' . $value);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -84,7 +96,5 @@ final class DefaultProjectImporter extends AbstractProjectImporter
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $project;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,5 +13,13 @@ use App\Entity\Project;
|
||||
|
||||
interface ProjectImporterInterface
|
||||
{
|
||||
public function convertEntryToProject(array $entry): Project;
|
||||
/**
|
||||
* Convert an entry (key-value pairs) to a Project entity.
|
||||
* Accepts an optional array of options (like "dateformat" or "timezone").
|
||||
*
|
||||
* @param array<string, mixed> $entry
|
||||
* @param array<string, mixed> $options
|
||||
* @return Project
|
||||
*/
|
||||
public function convertEntryToProject(array $entry, array $options = []): Project;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user