33namespace SolutionForest \InspireCms \Dtos ;
44
55use Illuminate \Contracts \Support \Htmlable ;
6+ use Illuminate \Support \Collection ;
67use Illuminate \Support \HtmlString ;
78use SolutionForest \InspireCms \Support \Base \Dtos \BaseDto ;
89
@@ -212,9 +213,9 @@ class SeoDto extends BaseDto
212213 public $ noCache ;
213214
214215 /**
215- * @var self|null
216+ * @var Collection< self> |null
216217 */
217- protected $ root = null ;
218+ protected $ ancestors = null ;
218219
219220 public static function fromArray (array $ parameters )
220221 {
@@ -262,12 +263,15 @@ public static function fromArray(array $parameters)
262263 }
263264
264265 /**
265- * @param self| null $root
266+ * @param self[]|Collection<slef>| null $ancestors
266267 * @return self
267268 */
268- public function setRoot ( $ root )
269+ public function setAncestors ( $ ancestors )
269270 {
270- $ this ->root = $ root ;
271+ $ this ->ancestors = collect ($ ancestors ?? [])
272+ ->whereInstanceOf (static ::class)
273+ ->filter ()
274+ ->values ();
271275
272276 return $ this ;
273277 }
@@ -281,10 +285,12 @@ public function __toString(): string
281285 {
282286 $ html = '' ;
283287
288+ $ parentSeo = $ this ->ancestors ->filter ()->last ();
289+
284290 if ($ this ->title ) {
285291 $ title = $ this ->title ;
286- if ($ this -> root ) {
287- $ title .= ' - ' . $ this -> root ->title ;
292+ if ($ parentSeo ) {
293+ $ title .= ' - ' . $ parentSeo ->title ;
288294 }
289295 $ html .= "<title> {$ title }</title> \n" ;
290296 }
@@ -299,8 +305,8 @@ public function __toString(): string
299305
300306 if ($ this ->ogTitle || $ this ->title ) {
301307 $ ogTitle = $ this ->ogTitle ?: $ this ->title ;
302- if ($ this -> root ) {
303- $ ogTitle .= ' - ' . $ this -> root ->title ;
308+ if ($ parentSeo ) {
309+ $ ogTitle .= ' - ' . $ parentSeo ->title ;
304310 }
305311
306312 $ html .= "<meta property= \"og:title \" content= \"{$ ogTitle }\"> \n" ;
0 commit comments