Skip to content

Commit b619180

Browse files
author
Josh Freeman
committed
Refactorz
1 parent 40c3100 commit b619180

File tree

10 files changed

+188
-36
lines changed

10 files changed

+188
-36
lines changed

cli

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env php
2+
<?php
3+
// composer auto loader
4+
require __DIR__ . '/vendor/autoload.php';
5+
6+
$option = isset($argv[1]) ? trim($argv[1]) : false;
7+
$arg1 = isset($argv[2]) ? trim($argv[2]) : false;
8+
9+
// setup xivapi
10+
$api = new \XIVAPI\XIVAPI();
11+
12+
// set env key
13+
$api->environment->key('my_api_key');
14+
15+
// do commands
16+
$results = $api->search->find('fire shard')->results();
17+
18+
print_r($results);

example.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/XIVAPI/Api/Search.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace XIVAPI\Api;
4+
5+
use GuzzleHttp\RequestOptions;
6+
use XIVAPI\Guzzle\Guzzle;
7+
8+
class Search extends Guzzle
9+
{
10+
const ENDPOINT = '/search';
11+
12+
private $options = [
13+
'indexes' => null,
14+
'string' => null,
15+
'string_column' => null,
16+
'string_algo' => null,
17+
'page' => null,
18+
'sort_field' => null,
19+
'sort_order' => null,
20+
'limit' => null,
21+
'columns' => null,
22+
'bool' => null,
23+
'filters' => [],
24+
];
25+
26+
public function __construct()
27+
{
28+
$this->options = (Object)$this->options;
29+
}
30+
31+
public function results()
32+
{
33+
$options = [
34+
RequestOptions::QUERY => []
35+
];
36+
37+
foreach ($this->options as $field => $value) {
38+
if (empty($value)) {
39+
continue;
40+
}
41+
42+
$options[RequestOptions::QUERY][$field] = $value;
43+
}
44+
45+
return $this->get(self::ENDPOINT, $options);
46+
}
47+
48+
public function indexes(array $indexes): Search
49+
{
50+
$this->options->indexes = implode(',', $indexes);
51+
return $this;
52+
}
53+
54+
public function find(string $string): Search
55+
{
56+
$this->options->string = trim($string);
57+
return $this;
58+
}
59+
60+
public function findColumn(string $column): Search
61+
{
62+
$this->options->string_column = trim($column);
63+
return $this;
64+
}
65+
66+
public function algorithm(string $searchStringAlgorithm): Search
67+
{
68+
$this->options->string_algo = trim($searchStringAlgorithm);
69+
return $this;
70+
}
71+
72+
public function page(int $number): Search
73+
{
74+
$this->options->page = $number;
75+
return $this;
76+
}
77+
78+
public function sort(string $field, string $order): Search
79+
{
80+
$this->options->sort_field = $field;
81+
$this->options->sort_order = $order;
82+
return $this;
83+
}
84+
85+
public function limit(int $limit): Search
86+
{
87+
$this->options->limit = $limit;
88+
return $this;
89+
}
90+
91+
public function columns(array $columns): Search
92+
{
93+
$this->options->columns = implode(',', $columns);
94+
return $this;
95+
}
96+
97+
public function bool(string $bool): Search
98+
{
99+
$this->options->bool = $bool;
100+
return $this;
101+
}
102+
103+
public function filter(string $field, $value, string $operand): Search
104+
{
105+
$this->options->filters[] = "${field}{$operand}{$value}";
106+
return $this;
107+
}
108+
}

src/XIVAPI/Api/SearchFilters.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace XIVAPI\Api;
4+
5+
class SearchFilters
6+
{
7+
const EQUAL_TO = '=';
8+
const GREATER_THAN = '>';
9+
const GREATER_THAN_OR_EQUAL_TO = '>=';
10+
const LESS_THAN = '>';
11+
const LESS_THAN_OR_EQUAL_TO = '>=';
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace XIVAPI\Api;
4+
5+
class SearchStringAlgorithms
6+
{
7+
const CUSTOM = 'custom';
8+
const WILDCARD = 'wildcard';
9+
const WILDCARD_PLUS = 'wildcard_plus';
10+
const FUZZY = 'fuzzy';
11+
const TERM = 'term';
12+
const PREFIX = 'prefix';
13+
const MATCH = 'match';
14+
const MATCH_PHRASE = 'match_phrase';
15+
const MATCH_PHRASE_PREFIX = 'match_phrase_prefix';
16+
const MULTI_MATCH = 'multi_match';
17+
const QUERY_STRING = 'query_string';
18+
const SIMILAR = 'similar';
19+
}

src/XIVAPI/Common/Environment.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace XIVAPI\Common;
4+
5+
/**
6+
* Set environment variables
7+
*/
8+
class Environment
9+
{
10+
const XIVAPI_KEY = 'XIVAPI_KEY';
11+
12+
public function key(string $key): Environment
13+
{
14+
putenv(self::XIVAPI_KEY .'='. $key);
15+
return $this;
16+
}
17+
}

src/XIVAPI/Guzzle/Guzzle.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Exception\ClientException;
7+
use GuzzleHttp\RequestOptions;
8+
use XIVAPI\Common\Environment;
79

810
class Guzzle
911
{
@@ -14,6 +16,10 @@ class Guzzle
1416

1517
public function query($method, $apiEndpoint, $options = [])
1618
{
19+
if ($key = getenv(Environment::XIVAPI_KEY)) {
20+
$options[RequestOptions::QUERY]['key'] = $key;
21+
}
22+
1723
$client = new Client([
1824
'base_uri' => self::ENDPOINT_PROD,
1925
'timeout' => self::TIMEOUT,

src/XIVAPI/Services/Content.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/XIVAPI/Services/ServiceHandler.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/XIVAPI/XIVAPI.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
namespace XIVAPI;
44

5-
use XIVAPI\Services\Content;
5+
use XIVAPI\Api\Search;
6+
use XIVAPI\Common\Environment;
67

78
class XIVAPI
89
{
9-
public $content;
10+
/** @var Environment */
11+
public $environment;
12+
/** @var Search */
13+
public $search;
1014

1115
public function __construct()
1216
{
13-
$this->content = new Content();
17+
$this->environment = new Environment();
18+
$this->search = new Search();
1419
}
1520
}

0 commit comments

Comments
 (0)