1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenFoodFacts\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use OpenFoodFacts\Api; |
9
|
|
|
use OpenFoodFacts\Document; |
10
|
|
|
use OpenFoodFacts\Exception\ProductNotFoundException; |
11
|
|
|
|
12
|
|
|
/** @mixin Api */ |
13
|
|
|
class OpenFoodFacts extends OpenFoodFactsApiWrapper |
14
|
|
|
{ |
15
|
|
|
protected int $max_results; |
16
|
|
|
|
17
|
30 |
|
public function __construct(Container $app, ?string $geography = null, string $environment = 'food') |
18
|
|
|
{ |
19
|
30 |
|
parent::__construct( |
20
|
30 |
|
[ |
21
|
30 |
|
'geography' => $geography ?? $app['config']->get('openfoodfacts.geography'), |
22
|
30 |
|
'app' => $app['config']->get('app.name'), |
23
|
30 |
|
], |
24
|
30 |
|
$app['cache.store'], |
25
|
30 |
|
$environment |
26
|
30 |
|
); |
27
|
|
|
|
28
|
30 |
|
$this->max_results = $app['config']->get('openfoodfacts.max_results', 1000); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Find product by barcode |
33
|
|
|
* |
34
|
|
|
* @param string $value |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
15 |
|
public function barcode(string $value): array |
38
|
|
|
{ |
39
|
15 |
|
if (empty($value)) { |
40
|
3 |
|
throw new InvalidArgumentException('Argument must represent a barcode'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
try { |
44
|
12 |
|
$doc = $this->api->getProduct($value); |
45
|
|
|
|
46
|
9 |
|
return empty($doc->code) ? [] : $doc->getData(); |
47
|
3 |
|
} catch (ProductNotFoundException) { |
48
|
3 |
|
return []; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Search products by term |
54
|
|
|
* |
55
|
|
|
* @param string $searchterm |
56
|
|
|
* @return Collection<int, array> |
57
|
|
|
*/ |
58
|
12 |
|
public function find(string $searchterm): Collection |
59
|
|
|
{ |
60
|
12 |
|
if (empty($searchterm)) { |
61
|
3 |
|
throw new InvalidArgumentException('Specify a search term to find data for matching products'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @var Collection<int, Document> $products */ |
65
|
9 |
|
$products = Collection::make(); |
66
|
9 |
|
$page = 0; |
67
|
|
|
|
68
|
|
|
do { |
69
|
9 |
|
$pageResults = $this->api->search($searchterm, ++$page, 100); |
70
|
9 |
|
$totalMatches = $pageResults->searchCount(); |
71
|
|
|
|
72
|
9 |
|
if ($this->max_results > 0 && $totalMatches > $this->max_results) { |
73
|
3 |
|
throw new \Exception("ERROR: {$totalMatches} results found, while buffer limited to {$this->max_results}. Please narrow your search."); |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
$pages = (int)ceil($totalMatches / $pageResults->getPageSize()); |
77
|
|
|
/** @var Document[] $array */ |
78
|
6 |
|
$array = iterator_to_array($pageResults); |
79
|
|
|
|
80
|
6 |
|
$products = $products->concat($array); |
81
|
6 |
|
} while ($page < $pages); |
82
|
|
|
|
83
|
6 |
|
return $products->map(fn ($product) => $product->getData()); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
public function __call(string $method, array $parameters): mixed |
87
|
|
|
{ |
88
|
3 |
|
return $this->api->$method(...$parameters); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|