¨4.0.1¨
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Modules\Product\Http\Response;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Category\Entities\Category;
|
||||
@@ -9,20 +11,21 @@ use Illuminate\Contracts\Support\Responsable;
|
||||
|
||||
class SuggestionsResponse implements Responsable
|
||||
{
|
||||
private $query;
|
||||
private $products;
|
||||
private $categories;
|
||||
private $totalResults;
|
||||
private string $query;
|
||||
private Collection $products;
|
||||
private Collection $categories;
|
||||
private int $totalResults;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param string $query
|
||||
* @param int $totalResults
|
||||
* @param \Illuminate\Support\Collection $products
|
||||
* @param \Illuminate\Support\Collection $categories
|
||||
* @param Collection $products
|
||||
* @param Collection $categories
|
||||
*/
|
||||
public function __construct($query, Collection $products, Collection $categories, $totalResults)
|
||||
public function __construct(string $query, Collection $products, Collection $categories, int $totalResults)
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->products = $products;
|
||||
@@ -30,13 +33,15 @@ class SuggestionsResponse implements Responsable
|
||||
$this->totalResults = $totalResults;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create an HTTP response that represents the object.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function toResponse($request)
|
||||
public function toResponse($request): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'categories' => $this->transformCategories(),
|
||||
@@ -45,12 +50,13 @@ class SuggestionsResponse implements Responsable
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Transform the categories.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
* @return Collection
|
||||
*/
|
||||
private function transformCategories()
|
||||
private function transformCategories(): Collection
|
||||
{
|
||||
return $this->categories->map(function (Category $category) {
|
||||
return [
|
||||
@@ -61,44 +67,48 @@ class SuggestionsResponse implements Responsable
|
||||
})->unique('slug')->values();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Transform the products.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
* @return Collection
|
||||
*/
|
||||
private function transformProducts()
|
||||
private function transformProducts(): Collection
|
||||
{
|
||||
return $this->products->map(function (Product $product) {
|
||||
return [
|
||||
'slug' => $product->slug,
|
||||
'name' => $this->highlight($product->name),
|
||||
'formatted_price' => $product->getFormattedPriceAttribute(),
|
||||
'base_image' => $product->getBaseImageAttribute(),
|
||||
'is_out_of_stock' => $product->isOutOfStock(),
|
||||
'url' => $product->url(),
|
||||
'formatted_price' => $product->variant?->formatted_price ?? $product->formatted_price,
|
||||
'base_image' => $product->variant?->base_image ?? $product->base_image,
|
||||
'is_out_of_stock' => $product->variant?->isOutOfStock() ?? $product->isOutOfStock(),
|
||||
'url' => $product->variant?->url() ?? $product->url(),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Highlight the given text.
|
||||
*
|
||||
* @param string $text
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function highlight($text)
|
||||
private function highlight($text): string
|
||||
{
|
||||
$query = str_replace(' ', '|', preg_quote($this->query));
|
||||
|
||||
return preg_replace("/($query)/i", '<em>$1</em>', $text);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get remaining results count.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function getRemainingCount()
|
||||
private function getRemainingCount(): int
|
||||
{
|
||||
return $this->totalResults - $this->products->count();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user