Skip to content

Commit 7c72d1b

Browse files
committed
🧙‍♂️
1 parent ec74c65 commit 7c72d1b

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

src/Mapping/Analyzer/Custom/CommonGrams.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class CommonGrams implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterface, \Spameri\ElasticQuery\Collection\Item
66
{
77

8+
public const NAME = 'customCommonGrams';
9+
810
/**
911
* @var \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection
1012
*/
@@ -42,7 +44,7 @@ public function key(): string
4244

4345
public function name(): string
4446
{
45-
return 'customCommonGrams';
47+
return self::NAME;
4648
}
4749

4850

src/Mapping/Analyzer/Custom/EdgeNgram.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class EdgeNgram implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterface, \Spameri\ElasticQuery\Collection\Item
66
{
77

8+
public const NAME = 'customEdgeNgram';
9+
810
/**
911
* @var \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection
1012
*/
@@ -49,7 +51,7 @@ public function key(): string
4951

5052
public function name(): string
5153
{
52-
return 'customEdgeNgram';
54+
return self::NAME;
5355
}
5456

5557

src/Mapping/Analyzer/Custom/WordDelimiter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class WordDelimiter implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterface, \Spameri\ElasticQuery\Collection\Item
66
{
77

8+
public const NAME = 'customWordDelimiter';
9+
810
/**
911
* @var \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection
1012
*/
@@ -35,7 +37,7 @@ public function key(): string
3537

3638
public function name(): string
3739
{
38-
return 'customWordDelimiter';
40+
return self::NAME;
3941
}
4042

4143

src/Mapping/Settings/Mapping.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ class Mapping implements \Spameri\ElasticQuery\Entity\ArrayInterface
1515
*/
1616
private $fields;
1717

18+
private bool $dynamic;
19+
1820

1921
public function __construct(
2022
string $indexName,
21-
?\Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection $fields = NULL
23+
?\Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection $fields = NULL,
24+
bool $dynamic = true
2225
)
2326
{
2427
$this->indexName = $indexName;
@@ -28,8 +31,13 @@ public function __construct(
2831
}
2932

3033
$this->fields = $fields;
34+
$this->dynamic = $dynamic;
3135
}
3236

37+
public function enableStrictMapping(): void
38+
{
39+
$this->dynamic = false;
40+
}
3341

3442
public function getIndexName(): string
3543
{
@@ -103,6 +111,7 @@ public function toArray(): array
103111
return [
104112
'mappings' => [
105113
'properties' => $fields,
114+
'dynamic' => $this->dynamic,
106115
],
107116
];
108117
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Mapping\Settings\Mapping;
4+
5+
class FieldSeparator
6+
{
7+
8+
public const FIELD_SEPARATOR = '.';
9+
10+
}

src/Response/Result/Hit.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,36 @@ public function getValue(
6969
string $key
7070
)
7171
{
72-
$value = $this->source[$key] ?? NULL;
73-
if ($value === NULL) {
74-
return NULL;
75-
}
72+
$value = $this->getSubValue($key);
7673

77-
if (\strpos($key, '.') === FALSE) {
74+
if ($value !== NULL) {
7875
return $value;
7976
}
8077

81-
$levels = \explode('.', $key);
78+
return $this->source[$key] ?? NULL;
79+
}
80+
81+
/**
82+
* @phpstan-return mixed
83+
*/
84+
public function getSubValue($key)
85+
{
86+
if (\str_contains($key, \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldSeparator::FIELD_SEPARATOR) === TRUE) {
87+
$levels = \explode(\Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldSeparator::FIELD_SEPARATOR, $key);
88+
89+
$value = $this->source[$levels[0]];
90+
unset($levels[0]);
8291

83-
foreach ($levels as $subKey) {
84-
$value = $value[$subKey] ?? NULL;
92+
foreach ($levels as $subKey) {
93+
$value = $value[$subKey] ?? NULL;
8594

86-
if ($value === NULL) {
87-
return NULL;
95+
if ($value !== NULL) {
96+
return $value;
97+
}
8898
}
8999
}
90100

91-
return $value;
101+
return NULL;
92102
}
93103

94104

0 commit comments

Comments
 (0)