Как произвести валидацию данных распарсенного CSV файла
Здраствуйте, Не могу понять как можно произвести валидацию данных распарсенного файла в обьекте транспортировки данных (DTO). Как я могу пропустить DTO через валидатор? вот DTO:
<?php declare(strict_types=1);
namespace App\Lots\Model;
use Doctrine\ORM\NoResultException;
use Money\Currency;
use Money\Money;
use Money\MoneyParser;
use Symfony\Component\Validator\Constraints as Assert;
final class LotImportDto
{
public Lot $lot;
#[Assert\Type(['string', 'null'])]
public ?string $number = null;
#[Assert\Type('string')]
private string $title = '';
#[Assert\Type(['string', 'null'])]
private ?string $description = null;
#[Assert\Type(['string', 'null'])]
private ?string $introtext = null;
#[Assert\Type('int')]
private int $qty = 1;
#[Assert\Type('array')]
private array $mapper = [
'Lot Title' => 'title',
'Title' => 'title',
'Description' => 'description',
'Item Quantity' => 'qty',
'Quantity' => 'qty',
'Qty' => 'qty',
];
private LotRepository $lotRepository;
private MoneyParser $parser;
private SellerRepository $sellerRepository;
public function __construct(
LotRepository $lotRepository,
MoneyParser $parser,
SellerRepository $sellerRepository,
array $row
)
{
$this->lotRepository = $lotRepository;
$this->parser = $parser;
$this->sellerRepository = $sellerRepository;
$this->item = $row;
}
public function parseCSV(Auction $auction): void
{
$currency = new Currency('USD');
foreach ($this->item as $column => $value) {
if (isset($this->mapper[$column])) {
$property = $this->mapper[$column];
// Do validation/casting on the value if need be
switch ($property) {
case 'qty':
$value = (int) $value;
// Reset to default if the value is less than 1, i.e. an empty cell
if ($value < 1) {
$value = 1;
}
break;
case 'tax_exempt':
$value = strcmp('Yes', trim($value)) === 0;
break;
}
$this->$property = $value;
} elseif (strcmp(
str_replace(
' ',
'-',
trim(strtolower($column))), 'seller')) {
/** @phpstan-var positive-int $sellerNumber */
$sellerNumber = (int) $value;
// Try to find an existing seller for this event with this number otherwise generate a stub
if (($seller = $this->sellerRepository->findSellerForEvent($auction, $sellerNumber)) === null) {
$seller = Seller::createForAuctionWithNumber($auction, $sellerNumber);
$auction->addSeller($seller);
// Immediately add the seller to the database
$this->sellerRepository->save($seller);
}
$this->seller = $seller;
}
}
}
public function isNew(Auction $auction): bool
{
try {
$this->lot = $this->lotRepository->findLotNumberForAuction($auction, $this->number);
return FALSE;
} catch (NoResultException) {
$this->lot = new Lot();
$this->lot->number = $this->number;
$auction->addLot($this->lot);
return TRUE;
}
}
public function getLotModel(): Lot
{
$this->lot->title = $this->title;
$this->lot->description = $this->description;
$this->lot->introtext = $this->introtext;
$this->lot->qty = $this->qty;
$this->lot->status = $this->status;
$this->lot->seller = $this->seller;
$this->lot->sale_order = $this->sale_order;
return $this->lot;
}
}
Источник: Stack Overflow на русском