@@ -2058,34 +2058,40 @@ public function deleteAttribute(string $collection, string $id): bool
20582058 public function renameAttribute (string $ collection , string $ old , string $ new ): bool
20592059 {
20602060 $ collection = $ this ->silent (fn () => $ this ->getCollection ($ collection ));
2061+
2062+ /**
2063+ * @var array<Document> $attributes
2064+ */
20612065 $ attributes = $ collection ->getAttribute ('attributes ' , []);
2062- $ indexes = $ collection ->getAttribute ('indexes ' , []);
20632066
2064- $ attribute = \in_array ($ old , \array_map (fn ($ attribute ) => $ attribute ['$id ' ], $ attributes ));
2067+ /**
2068+ * @var array<Document> $indexes
2069+ */
2070+ $ indexes = $ collection ->getAttribute ('indexes ' , []);
20652071
2066- if ($ attribute === false ) {
2067- throw new NotFoundException ('Attribute not found ' );
2068- }
2072+ $ attribute = new Document ();
20692073
2070- $ attributeNew = \in_array ($ new , \array_map (fn ($ attribute ) => $ attribute ['$id ' ], $ attributes ));
2074+ foreach ($ attributes as $ value ) {
2075+ if ($ value ->getId () === $ old ) {
2076+ $ attribute = $ value ;
2077+ }
20712078
2072- if ($ attributeNew !== false ) {
2073- throw new DuplicateException ('Attribute name already used ' );
2079+ if ($ value ->getId () === $ new ) {
2080+ throw new DuplicateException ('Attribute name already used ' );
2081+ }
20742082 }
20752083
2076- foreach ($ attributes as $ key => $ value ) {
2077- if (isset ($ value ['$id ' ]) && $ value ['$id ' ] === $ old ) {
2078- $ attributes [$ key ]['key ' ] = $ new ;
2079- $ attributes [$ key ]['$id ' ] = $ new ;
2080- $ attributeNew = $ attributes [$ key ];
2081- break ;
2082- }
2084+ if ($ attribute ->isEmpty ()) {
2085+ throw new NotFoundException ('Attribute not found ' );
20832086 }
20842087
2088+ $ attribute ->setAttribute ('$id ' , $ new );
2089+ $ attribute ->setAttribute ('key ' , $ new );
2090+
20852091 foreach ($ indexes as $ index ) {
20862092 $ indexAttributes = $ index ->getAttribute ('attributes ' , []);
20872093
2088- $ indexAttributes = \array_map (fn ($ attribute ) => ($ attribute === $ old ) ? $ new : $ attribute , $ indexAttributes );
2094+ $ indexAttributes = \array_map (fn ($ attr ) => ($ attr === $ old ) ? $ new : $ attr , $ indexAttributes );
20892095
20902096 $ index ->setAttribute ('attributes ' , $ indexAttributes );
20912097 }
@@ -2099,7 +2105,7 @@ public function renameAttribute(string $collection, string $old, string $new): b
20992105 $ this ->silent (fn () => $ this ->updateDocument (self ::METADATA , $ collection ->getId (), $ collection ));
21002106 }
21012107
2102- $ this ->trigger (self ::EVENT_ATTRIBUTE_UPDATE , $ attributeNew );
2108+ $ this ->trigger (self ::EVENT_ATTRIBUTE_UPDATE , $ attribute );
21032109
21042110 return $ renamed ;
21052111 }
0 commit comments