1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27namespace PrestaShop\PrestaShop\Core\Domain\Product\Query;
28
29use PrestaShop\PrestaShop\Core\Domain\Currency\Exception\CurrencyConstraintException;
30use PrestaShop\PrestaShop\Core\Domain\Currency\ValueObject\AlphaIsoCode;
31use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderException;
32use PrestaShop\PrestaShop\Core\Domain\Order\ValueObject\OrderId;
33use PrestaShop\PrestaShop\Core\Domain\Product\Exception\ProductSearchEmptyPhraseException;
34
35/**
36 * Queries for products by provided search phrase
37 */
38class SearchProducts
39{
40    /**
41     * @var string
42     */
43    private $phrase;
44
45    /**
46     * @var int
47     */
48    private $resultsLimit;
49
50    /**
51     * @var AlphaIsoCode
52     */
53    private $alphaIsoCode;
54
55    /**
56     * @var OrderId|null
57     */
58    private $orderId;
59
60    /**
61     * @param string $phrase
62     * @param int $resultsLimit
63     * @param string $isoCode
64     * @param int|null $orderId
65     *
66     * @throws ProductSearchEmptyPhraseException
67     * @throws CurrencyConstraintException
68     */
69    public function __construct(
70        string $phrase,
71        int $resultsLimit,
72        string $isoCode,
73        ?int $orderId = null
74    ) {
75        $this->assertIsNotEmptyString($phrase);
76        $this->phrase = $phrase;
77        $this->resultsLimit = $resultsLimit;
78        $this->alphaIsoCode = new AlphaIsoCode($isoCode);
79        if (null !== $orderId) {
80            $this->setOrderId($orderId);
81        }
82    }
83
84    /**
85     * @return AlphaIsoCode
86     */
87    public function getAlphaIsoCode(): AlphaIsoCode
88    {
89        return $this->alphaIsoCode;
90    }
91
92    /**
93     * @return string
94     */
95    public function getPhrase()
96    {
97        return $this->phrase;
98    }
99
100    /**
101     * @return int
102     */
103    public function getResultsLimit(): int
104    {
105        return $this->resultsLimit;
106    }
107
108    /**
109     * @return OrderId|null
110     */
111    public function getOrderId(): ?OrderId
112    {
113        return $this->orderId;
114    }
115
116    /**
117     * @param int $orderId
118     *
119     * @throws OrderException
120     */
121    private function setOrderId(int $orderId): void
122    {
123        $this->orderId = new OrderId($orderId);
124    }
125
126    /**
127     * @param string $phrase
128     *
129     * @throws ProductSearchEmptyPhraseException
130     */
131    private function assertIsNotEmptyString(string $phrase): void
132    {
133        if ($phrase === '') {
134            throw new ProductSearchEmptyPhraseException('Product search phrase must be a not empty string');
135        }
136    }
137}
138