-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clothes.php
49 lines (44 loc) · 1.4 KB
/
Clothes.php
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
<?php
include 'keys.php';
require 'vendor/autoload.php';
class Clothes
{
public $index;
public function __construct($indexName)
{
// APPLICATION_ID & API_KEY are defined in keys.php
$client = new \AlgoliaSearch\Client(APPLICATION_ID, API_KEY);
$this->index = $client->initIndex($indexName);
}
public function getIndex() {
return $this->index;
}
public function getInformationClothes($clothes = "dress", $hitsPerPage = 10)
{
$query = $this->index->search($clothes,
array("hitsPerPage" => $hitsPerPage,
"attributesToRetrieve"=>"Name,Price,Designer"));
if($this->validateNbHits($query)) {
return $this->arrayInformation($query);
} else {
return "Please, try with other option";
}
}
public function validateNbHits($query)
{
if($query["nbHits"] == 0) {
return 0;
}
return 1;
}
public function arrayInformation($query)
{
$information = array();
for($i = 0; $i < sizeof($query["hits"]); $i++) {
$information[$i] = array("name" => $query["hits"][$i]["Name"],
"price" => $query["hits"][$i]["Price"],
"designer" => $query["hits"][$i]["Designer"]);
}
return $information;
}
}