38
38
use MacFJA \RediSearch \Redis \Initializer ;
39
39
use MacFJA \RediSearch \Redis \Response \InfoResponse ;
40
40
41
- /**
42
- * @codeCoverageIgnore
43
- */
44
41
class Index
45
42
{
46
43
/** @var Client */
47
44
private $ client ;
48
45
49
- /** @var InfoResponse */
46
+ /** @var null| InfoResponse */
50
47
private $ info ;
51
48
52
49
/** @var string */
53
50
private $ index ;
54
51
55
- /** @var string */
52
+ /** @var null| string */
56
53
private $ version ;
57
54
58
- public function __construct (string $ index , Client $ client )
55
+ public function __construct (string $ index , Client $ client, ? string $ version = null )
59
56
{
60
57
$ this ->client = $ client ;
61
58
$ this ->index = $ index ;
62
- $ this ->version = Initializer::getRediSearchVersion ($ client ) ?? AbstractCommand::MIN_IMPLEMENTED_VERSION ;
63
- $ this ->getInfo ();
59
+ $ this ->version = $ version ;
64
60
}
65
61
66
62
/**
67
63
* @param array<string,float|int|string> $properties
68
64
*/
69
65
public function addDocumentFromArray (array $ properties , ?string $ hash = null ): string
70
66
{
71
- $ prefixes = $ this ->info ->getIndexDefinition ('prefixes ' );
67
+ $ prefixes = $ this ->getInfo () ->getIndexDefinition ('prefixes ' );
72
68
$ prefix = '' ;
73
69
if (is_array ($ prefixes ) && count ($ prefixes ) > 0 ) {
74
70
$ prefix = (string ) reset ($ prefixes );
@@ -94,7 +90,7 @@ public function deleteDocument(string $hash): bool
94
90
95
91
public function addField (CreateCommandFieldOption $ field ): bool
96
92
{
97
- $ command = new Alter ($ this ->version );
93
+ $ command = new Alter ($ this ->getVersion () );
98
94
$ command
99
95
->setIndex ($ this ->index )
100
96
->addField ($ field )
@@ -105,7 +101,7 @@ public function addField(CreateCommandFieldOption $field): bool
105
101
106
102
public function delete (bool $ withDocuments = false ): bool
107
103
{
108
- $ command = new DropIndex ($ this ->version );
104
+ $ command = new DropIndex ($ this ->getVersion () );
109
105
$ command ->setIndex ($ this ->index )
110
106
->setDeleteDocument ($ withDocuments )
111
107
;
@@ -116,34 +112,45 @@ public function delete(bool $withDocuments = false): bool
116
112
public function addAlias (string $ alias ): bool
117
113
{
118
114
return 'OK ' === (string ) $ this ->client ->execute (
119
- (new AliasAdd ($ this ->version ))
115
+ (new AliasAdd ($ this ->getVersion () ))
120
116
->setIndex ($ this ->index )
121
117
->setAlias ($ alias )
122
118
);
123
119
}
124
120
125
121
public function updateAlias (string $ alias ): bool
126
122
{
127
- return 'OK ' === (string ) $ this ->client ->execute ((new AliasUpdate ($ this ->version ))->setIndex ($ this ->index )->setAlias ($ alias ));
123
+ return 'OK ' === (string ) $ this ->client ->execute ((new AliasUpdate ($ this ->getVersion () ))->setIndex ($ this ->index )->setAlias ($ alias ));
128
124
}
129
125
130
126
public function deleteAlias (string $ alias ): bool
131
127
{
132
- return 'OK ' === (string ) $ this ->client ->execute ((new AliasDel ($ this ->version ))->setAlias ($ alias ));
128
+ return 'OK ' === (string ) $ this ->client ->execute ((new AliasDel ($ this ->getVersion () ))->setAlias ($ alias ));
133
129
}
134
130
135
131
/**
136
132
* @return array<string>
137
133
*/
138
134
public function getTagValues (string $ fieldName ): array
139
135
{
140
- return $ this ->client ->execute ((new TagVals ($ this ->version ))->setIndex ($ this ->index )->setField ($ fieldName ));
136
+ return $ this ->client ->execute ((new TagVals ($ this ->getVersion () ))->setIndex ($ this ->index )->setField ($ fieldName ));
141
137
}
142
138
143
139
public function getInfo (): InfoResponse
144
140
{
145
- $ this ->info = $ this ->client ->execute ((new Info ($ this ->version ))->setIndex ($ this ->index ));
141
+ if (!($ this ->info instanceof InfoResponse)) {
142
+ $ this ->info = $ this ->client ->execute ((new Info ($ this ->getVersion ()))->setIndex ($ this ->index ));
143
+ }
146
144
147
145
return $ this ->info ;
148
146
}
147
+
148
+ private function getVersion (): string
149
+ {
150
+ if (!is_string ($ this ->version )) {
151
+ $ this ->version = Initializer::getRediSearchVersion ($ this ->client ) ?? AbstractCommand::MIN_IMPLEMENTED_VERSION ;
152
+ }
153
+
154
+ return $ this ->version ;
155
+ }
149
156
}
0 commit comments