Skip to content

Commit a06e6d4

Browse files
committed
Remove Laravel support dependency
1 parent 7ac22d8 commit a06e6d4

21 files changed

Lines changed: 109 additions & 112 deletions

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ Documents contain the OpenSearch document ID and source payload:
7474
```php
7575
use DirectoryTree\OpenSearchAdapter\Documents\Document;
7676

77-
$documents->index('books', collect([
77+
$documents->index('books', [
7878
new Document('1', [
7979
'title' => 'The Hobbit',
8080
]),
81-
]));
81+
]);
8282
```
8383

8484
Routing values can be attached by document ID:
@@ -88,11 +88,11 @@ use DirectoryTree\OpenSearchAdapter\Documents\Routing;
8888

8989
$routing = (new Routing)->add('1', 'tenant-1');
9090

91-
$documents->index('books', collect([
91+
$documents->index('books', [
9292
new Document('1', [
9393
'title' => 'The Hobbit',
9494
]),
95-
]), routing: $routing);
95+
], routing: $routing);
9696
```
9797

9898
## Searching Documents
@@ -118,9 +118,10 @@ $response = $documents->search('books', $request);
118118

119119
$total = $response->total();
120120

121-
$hits = $response->hits()->map(function ($hit) {
122-
return $hit->document()->content();
123-
});
121+
$hits = array_map(
122+
fn ($hit) => $hit->document()->content(),
123+
$response->hits(),
124+
);
124125
```
125126

126127
## Aliases
@@ -148,6 +149,6 @@ $aliases = $indices->getAliases('books');
148149
Search response objects expose the original OpenSearch payload through `raw()`:
149150

150151
```php
151-
$rawHit = $response->hits()->first()->raw();
152+
$rawHit = $response->hits()[0]->raw();
152153
$rawResponse = $response->raw();
153154
```

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"require": {
2929
"php": "^8.2",
3030
"guzzlehttp/guzzle": "^7.0",
31-
"illuminate/support": "^11.0|^12.0|^13.0",
3231
"opensearch-project/opensearch-php": "^2.6"
3332
},
3433
"require-dev": {

src/Documents/Document.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace DirectoryTree\OpenSearchAdapter\Documents;
44

5-
use Illuminate\Contracts\Support\Arrayable;
6-
use Illuminate\Support\Arr;
7-
8-
class Document implements Arrayable
5+
class Document
96
{
107
/**
118
* Create a new document instance.
@@ -39,7 +36,11 @@ public function id(): string
3936
*/
4037
public function content(?string $key = null): mixed
4138
{
42-
return Arr::get($this->content, $key);
39+
if ($key === null) {
40+
return $this->content;
41+
}
42+
43+
return $this->content[$key] ?? null;
4344
}
4445

4546
/**

src/Documents/DocumentManager.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException;
66
use DirectoryTree\OpenSearchAdapter\Search\SearchRequest;
77
use DirectoryTree\OpenSearchAdapter\Search\SearchResponse;
8-
use Illuminate\Support\Collection;
98
use OpenSearch\Client;
109

1110
class DocumentManager
@@ -23,13 +22,13 @@ public function __construct(
2322
/**
2423
* Index the given documents into OpenSearch.
2524
*
26-
* @param Collection<int, Document> $documents
25+
* @param array<int, Document> $documents
2726
*
2827
* @throws BulkRequestException
2928
*/
3029
public function index(
3130
string $indexName,
32-
Collection $documents,
31+
array $documents,
3332
bool $refresh = false,
3433
?Routing $routing = null
3534
): self {

src/Indices/IndexManager.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace DirectoryTree\OpenSearchAdapter\Indices;
44

5-
use Illuminate\Support\Collection;
65
use OpenSearch\Client;
76
use OpenSearch\Namespaces\IndicesNamespace;
87

@@ -180,23 +179,26 @@ public function drop(string $indexName): self
180179
/**
181180
* Get the aliases for the given index.
182181
*
183-
* @return Collection<string, Alias>
182+
* @return array<string, Alias>
184183
*/
185-
public function getAliases(string $indexName): Collection
184+
public function getAliases(string $indexName): array
186185
{
187186
$response = $this->indices->getAlias([
188187
'index' => $indexName,
189188
]);
190189

191190
$aliases = $response[$indexName]['aliases'] ?? [];
191+
$results = [];
192192

193-
return collect($aliases)->map(static function (array $parameters, string $name) {
194-
return new Alias(
193+
foreach ($aliases as $name => $parameters) {
194+
$results[$name] = new Alias(
195195
$name,
196196
$parameters['filter'] ?? null,
197197
$parameters['routing'] ?? null
198198
);
199-
});
199+
}
200+
201+
return $results;
200202
}
201203

202204
/**

src/Indices/Mapping.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace DirectoryTree\OpenSearchAdapter\Indices;
44

55
use Closure;
6-
use Illuminate\Contracts\Support\Arrayable;
7-
use Illuminate\Support\Traits\ForwardsCalls;
86

97
/**
108
* @method $this alias(string $name, array|null $parameters = null)
@@ -48,10 +46,8 @@
4846
* @method $this tokenCount(string $name, array|null $parameters = null)
4947
* @method $this wildcard(string $name, array|null $parameters = null)
5048
*/
51-
class Mapping implements Arrayable
49+
class Mapping
5250
{
53-
use ForwardsCalls;
54-
5551
/**
5652
* Indicates if OpenSearch should index field names.
5753
*/
@@ -141,7 +137,7 @@ public function dynamicTemplate(string $name, array $parameters): self
141137
*/
142138
public function __call(string $method, array $parameters): self
143139
{
144-
$this->forwardCallTo($this->properties, $method, $parameters);
140+
$this->properties->{$method}(...$parameters);
145141

146142
return $this;
147143
}

src/Indices/MappingProperties.php

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

55
use BadMethodCallException;
66
use Closure;
7-
use Illuminate\Contracts\Support\Arrayable;
8-
use Illuminate\Support\Str;
97

108
/**
119
* @method $this alias(string $name, array|null $parameters = null)
@@ -47,7 +45,7 @@
4745
* @method $this tokenCount(string $name, array|null $parameters = null)
4846
* @method $this wildcard(string $name, array|null $parameters = null)
4947
*/
50-
class MappingProperties implements Arrayable
48+
class MappingProperties
5149
{
5250
/**
5351
* The property definitions.
@@ -101,7 +99,7 @@ public function __call(string $method, array $arguments): self
10199
throw new BadMethodCallException(sprintf('Invalid number of arguments for %s method', $method));
102100
}
103101

104-
$property = ['type' => Str::snake($method)];
102+
$property = ['type' => $this->snake($method)];
105103

106104
if (isset($arguments[1])) {
107105
$property += $arguments[1];
@@ -140,4 +138,12 @@ protected function normalizeParametersWithProperties(Closure|array $parameters):
140138

141139
return $parameters;
142140
}
141+
142+
/**
143+
* Convert a camel-case field type into an OpenSearch snake-case type.
144+
*/
145+
protected function snake(string $value): string
146+
{
147+
return strtolower((string) preg_replace('/(?<!^)[A-Z]/', '_$0', $value));
148+
}
143149
}

src/Indices/Settings.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
namespace DirectoryTree\OpenSearchAdapter\Indices;
44

55
use BadMethodCallException;
6-
use Illuminate\Contracts\Support\Arrayable;
7-
use Illuminate\Support\Str;
86

97
/**
108
* @method $this index(array $configuration)
119
* @method $this analysis(array $configuration)
1210
*/
13-
class Settings implements Arrayable
11+
class Settings
1412
{
1513
/**
1614
* The index settings payload.
@@ -32,7 +30,7 @@ public function __call(string $method, array $arguments): self
3230
throw new BadMethodCallException(sprintf('Invalid number of arguments for %s method', $method));
3331
}
3432

35-
$this->settings[Str::snake($method)] = $arguments[0];
33+
$this->settings[$this->snake($method)] = $arguments[0];
3634

3735
return $this;
3836
}
@@ -46,4 +44,12 @@ public function toArray(): array
4644
{
4745
return $this->settings;
4846
}
47+
48+
/**
49+
* Convert a camel-case settings group into snake case.
50+
*/
51+
protected function snake(string $value): string
52+
{
53+
return strtolower((string) preg_replace('/(?<!^)[A-Z]/', '_$0', $value));
54+
}
4955
}

src/Search/Aggregation.php

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

33
namespace DirectoryTree\OpenSearchAdapter\Search;
44

5-
use Illuminate\Support\Collection;
6-
75
class Aggregation implements RawResponseInterface
86
{
97
/**
@@ -23,15 +21,13 @@ public function __construct(
2321
/**
2422
* Get the aggregation buckets.
2523
*
26-
* @return Collection<int, Bucket>
24+
* @return array<int, Bucket>
2725
*/
28-
public function buckets(): Collection
26+
public function buckets(): array
2927
{
3028
$buckets = $this->aggregation['buckets'] ?? [];
3129

32-
return collect($buckets)->map(static function (array $bucket) {
33-
return new Bucket($bucket);
34-
});
30+
return array_map(static fn (array $bucket) => new Bucket($bucket), $buckets);
3531
}
3632

3733
/**

src/Search/Highlight.php

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

33
namespace DirectoryTree\OpenSearchAdapter\Search;
44

5-
use Illuminate\Support\Collection;
6-
75
class Highlight implements RawResponseInterface
86
{
97
/**
@@ -23,11 +21,11 @@ public function __construct(
2321
/**
2422
* Get highlighted snippets for the given field.
2523
*
26-
* @return Collection<int, string>
24+
* @return array<int, string>
2725
*/
28-
public function snippets(string $field): Collection
26+
public function snippets(string $field): array
2927
{
30-
return collect($this->highlight[$field] ?? []);
28+
return $this->highlight[$field] ?? [];
3129
}
3230

3331
/**

0 commit comments

Comments
 (0)