3838use MacFJA \RediSearch \Redis \Initializer ;
3939use MacFJA \RediSearch \Redis \Response \InfoResponse ;
4040
41- /**
42- * @codeCoverageIgnore
43- */
4441class Index
4542{
4643 /** @var Client */
4744 private $ client ;
4845
49- /** @var InfoResponse */
46+ /** @var null| InfoResponse */
5047 private $ info ;
5148
5249 /** @var string */
5350 private $ index ;
5451
55- /** @var string */
52+ /** @var null| string */
5653 private $ version ;
5754
58- public function __construct (string $ index , Client $ client )
55+ public function __construct (string $ index , Client $ client, ? string $ version = null )
5956 {
6057 $ this ->client = $ client ;
6158 $ this ->index = $ index ;
62- $ this ->version = Initializer::getRediSearchVersion ($ client ) ?? AbstractCommand::MIN_IMPLEMENTED_VERSION ;
63- $ this ->getInfo ();
59+ $ this ->version = $ version ;
6460 }
6561
6662 /**
6763 * @param array<string,float|int|string> $properties
6864 */
6965 public function addDocumentFromArray (array $ properties , ?string $ hash = null ): string
7066 {
71- $ prefixes = $ this ->info ->getIndexDefinition ('prefixes ' );
67+ $ prefixes = $ this ->getInfo () ->getIndexDefinition ('prefixes ' );
7268 $ prefix = '' ;
7369 if (is_array ($ prefixes ) && count ($ prefixes ) > 0 ) {
7470 $ prefix = (string ) reset ($ prefixes );
@@ -94,7 +90,7 @@ public function deleteDocument(string $hash): bool
9490
9591 public function addField (CreateCommandFieldOption $ field ): bool
9692 {
97- $ command = new Alter ($ this ->version );
93+ $ command = new Alter ($ this ->getVersion () );
9894 $ command
9995 ->setIndex ($ this ->index )
10096 ->addField ($ field )
@@ -105,7 +101,7 @@ public function addField(CreateCommandFieldOption $field): bool
105101
106102 public function delete (bool $ withDocuments = false ): bool
107103 {
108- $ command = new DropIndex ($ this ->version );
104+ $ command = new DropIndex ($ this ->getVersion () );
109105 $ command ->setIndex ($ this ->index )
110106 ->setDeleteDocument ($ withDocuments )
111107 ;
@@ -116,34 +112,45 @@ public function delete(bool $withDocuments = false): bool
116112 public function addAlias (string $ alias ): bool
117113 {
118114 return 'OK ' === (string ) $ this ->client ->execute (
119- (new AliasAdd ($ this ->version ))
115+ (new AliasAdd ($ this ->getVersion () ))
120116 ->setIndex ($ this ->index )
121117 ->setAlias ($ alias )
122118 );
123119 }
124120
125121 public function updateAlias (string $ alias ): bool
126122 {
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 ));
128124 }
129125
130126 public function deleteAlias (string $ alias ): bool
131127 {
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 ));
133129 }
134130
135131 /**
136132 * @return array<string>
137133 */
138134 public function getTagValues (string $ fieldName ): array
139135 {
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 ));
141137 }
142138
143139 public function getInfo (): InfoResponse
144140 {
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+ }
146144
147145 return $ this ->info ;
148146 }
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+ }
149156}
0 commit comments