You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 line
3.2KB

  1. <?php
  2. namespace Lc\SovBundle\Component;
  3. class MetaComponent
  4. {
  5. protected FileComponent $fileComponent;
  6. protected StringComponent $stringComponent;
  7. public function __construct(FileComponent $fileComponent, StringComponent $stringComponent)
  8. {
  9. $this->fileComponent = $fileComponent;
  10. $this->stringComponent = $stringComponent;
  11. }
  12. public function getMetaTitle($entity, $title = null)
  13. {
  14. if($entity) {
  15. if(method_exists($entity, 'getMetaTitle') && $entity->getMetaTitle()) {
  16. return $entity->getMetaTitle() ;
  17. }
  18. elseif(!is_null($title)) {
  19. return $title;
  20. }
  21. elseif(method_exists($entity, 'getTitle') && $entity->getTitle()) {
  22. return $entity->getTitle() ;
  23. }
  24. }
  25. return '' ;
  26. }
  27. public function getMetaDescription($entity)
  28. {
  29. if($entity) {
  30. if(method_exists($entity, 'getMetaDescription') && $entity->getMetaDescription()) {
  31. return $entity->getMetaDescription() ;
  32. }
  33. elseif(method_exists($entity, 'getDescription') && $entity->getDescription()) {
  34. return $this->formatDescription($entity->getDescription());
  35. }
  36. }
  37. return '' ;
  38. }
  39. public function getOpenGraphTitle($entity, $title = null)
  40. {
  41. if($entity) {
  42. if(method_exists($entity, 'getOpenGraphTitle') && $entity->getOpenGraphTitle()) {
  43. return $entity->getOpenGraphTitle() ;
  44. }
  45. elseif(!is_null($title)) {
  46. return $title;
  47. }
  48. elseif(method_exists($entity, 'getTitle') && $entity->getTitle()) {
  49. return $entity->getTitle() ;
  50. }
  51. }
  52. return '' ;
  53. }
  54. public function getOpenGraphDescription($entity)
  55. {
  56. if($entity) {
  57. if(method_exists($entity, 'getOpenGraphDescription') && $entity->getOpenGraphDescription()) {
  58. return $entity->getOpenGraphDescription() ;
  59. }
  60. elseif(method_exists($entity, 'getDescription') && $entity->getDescription()) {
  61. return $this->formatDescription($entity->getDescription());
  62. }
  63. }
  64. return '' ;
  65. }
  66. public function getOpenGraphImage($entity)
  67. {
  68. if($entity) {
  69. if(method_exists($entity, 'getOpenGraphImage') && $entity->getOpenGraphImage()) {
  70. return $entity->getOpenGraphImage() ;
  71. }
  72. elseif(method_exists($entity, 'getImage') && $entity->getImage()) {
  73. return $entity->getImage() ;
  74. }
  75. }
  76. return '' ;
  77. }
  78. public function getOpenGraphImageUrl($entity)
  79. {
  80. $image = $this->getOpenGraphImage($entity);
  81. if($image && $image->getPath() && strlen($image->getPath()) > 0) {
  82. return $this->fileComponent->getAssetUrl($image->getPath());
  83. }
  84. return '';
  85. }
  86. public function formatDescription($description)
  87. {
  88. $description = trim($description);
  89. $description = $this->stringComponent->limitText($description, 50);
  90. $description = str_replace("\r\n","",$description);
  91. return $description;
  92. }
  93. }