-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathItemScraperImporter.php
More file actions
55 lines (44 loc) · 1.43 KB
/
Copy pathItemScraperImporter.php
File metadata and controls
55 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace App\Model\Scraper;
use App\Entity\Path;
use App\Entity\Scraper;
use App\Enum\ScraperTypeEnum;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
class ItemScraperImporter
{
#[Assert\File(mimeTypes: ['application/json'])]
#[Assert\NotBlank]
private ?File $file = null;
public function getFile(): ?File
{
return $this->file;
}
public function setFile(?File $file): ItemScraperImporter
{
$this->file = $file;
return $this;
}
public function toScrapper(): Scraper
{
$scraper = new Scraper();
$data = json_decode($this->file->getContent(), true);
$scraper->setName($data['name'] ?? null);
$scraper->setNamePath($data['namePath'] ?? null);
$scraper->setImagePath($data['imagePath'] ?? null);
$scraper->setPricePath($data['pricePath'] ?? null);
$scraper->setUrlPattern($data['urlPattern'] ?? null);
$scraper->setType(ScraperTypeEnum::TYPE_ITEM);
foreach ($data['dataPaths'] as $key => $dataPath) {
$newPath = (new Path())
->setName($dataPath['name'])
->setType($dataPath['type'])
->setPosition($dataPath['position'] ?? $key)
->setPath($dataPath['path'])
;
$scraper->addDataPath($newPath);
}
return $scraper;
}
}