55use codemonauts \glossary \elements \Glossary ;
66use codemonauts \glossary \elements \Term ;
77use Craft ;
8+ use craft \helpers \ArrayHelper ;
89use craft \helpers \Html ;
910use Exception ;
1011use Twig \Error \SyntaxError ;
1314
1415class Terms extends Component
1516{
16- protected $ renderedTerms = '' ;
17+ protected string $ renderedTerms = '' ;
18+ protected array $ usedTerms = [];
1719
1820 /**
1921 * Returns all terms to search for.
@@ -28,12 +30,12 @@ public function parseTerms(Term $term): array
2830 $ term ->term ,
2931 ];
3032
31- if ($ term ->synonyms !== '' ) {
33+ if (! empty ( $ term ->synonyms ) ) {
3234 $ synonyms = explode (', ' , $ term ->synonyms );
3335 $ terms = array_merge ($ terms , $ synonyms );
3436 }
3537
36- return $ terms ;
38+ return ArrayHelper:: filterEmptyStringsFromArray ( $ terms) ;
3739 }
3840
3941 /**
@@ -50,10 +52,9 @@ public function renderTerms(string $text, Glossary $glossary): string
5052 $ originalText = $ text ;
5153
5254 try {
53- $ termTemplate = $ glossary ->termTemplate !== '' ? $ glossary ->termTemplate : '<span>{{ text }}</span> ' ;
55+ $ termTemplate = ! empty ( $ glossary ->termTemplate ) ? $ glossary ->termTemplate : '<span>{{ text }}</span> ' ;
5456 $ replacements = [];
5557 $ terms = Term::find ()->glossary ($ glossary )->all ();
56- $ usedTerms = [];
5758
5859 foreach ($ terms as $ term ) {
5960 $ template = Html::modifyTagAttributes ($ termTemplate , [
@@ -74,7 +75,7 @@ public function renderTerms(string $text, Glossary $glossary): string
7475 if (!$ term ->caseSensitive ) {
7576 $ pattern .= 'i ' ;
7677 }
77- $ text = s ($ text )->replaceMatches ($ pattern , function ($ matches ) use ($ term , $ template , &$ replacements , &$ index , $ view , & $ usedTerms , $ glossary ) {
78+ $ text = s ($ text )->replaceMatches ($ pattern , function ($ matches ) use ($ term , $ template , &$ replacements , &$ index , $ view , $ glossary ) {
7879 try {
7980 $ replacement = trim ($ view ->renderString ($ template , [
8081 'term ' => $ term ,
@@ -99,7 +100,7 @@ public function renderTerms(string $text, Glossary $glossary): string
99100 $ variables ['term ' ] = $ term ;
100101
101102 try {
102- $ usedTerms [$ term ->id ] = $ view ->renderTemplate ($ glossary ->tooltipTemplate , $ variables , 'site ' );
103+ $ this -> usedTerms [$ term ->id ] = $ view ->renderTemplate ($ glossary ->tooltipTemplate , $ variables , 'site ' );
103104 } catch (SyntaxError $ e ) {
104105 Craft::error ($ e ->getMessage (), 'glossary ' );
105106 }
@@ -114,7 +115,7 @@ public function renderTerms(string $text, Glossary $glossary): string
114115 }
115116
116117 $ renderedTerms = '' ;
117- foreach ($ usedTerms as $ id => $ usedTerm ) {
118+ foreach ($ this -> usedTerms as $ id => $ usedTerm ) {
118119 $ renderedTerms .= Html::tag ('div ' , $ usedTerm , [
119120 'id ' => 'term- ' . $ id ,
120121 ]);
0 commit comments