Link.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Part of XML_XRD
  4. *
  5. * PHP version 5
  6. *
  7. * @category XML
  8. * @package XML_XRD
  9. * @author Christian Weiske <cweiske@php.net>
  10. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  11. * @link http://pear.php.net/package/XML_XRD
  12. */
  13. require_once 'XML/XRD/PropertyAccess.php';
  14. /**
  15. * Link element in a XRD file. Attribute access via object properties.
  16. *
  17. * Retrieving the title of a link is possible with the getTitle() convenience
  18. * method.
  19. *
  20. * @category XML
  21. * @package XML_XRD
  22. * @author Christian Weiske <cweiske@php.net>
  23. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  24. * @version Release: @package_version@
  25. * @link http://pear.php.net/package/XML_XRD
  26. */
  27. class XML_XRD_Element_Link extends XML_XRD_PropertyAccess
  28. {
  29. /**
  30. * Link relation
  31. *
  32. * @var string
  33. */
  34. public $rel;
  35. /**
  36. * Link type (MIME type)
  37. *
  38. * @var string
  39. */
  40. public $type;
  41. /**
  42. * Link URL
  43. *
  44. * @var string
  45. */
  46. public $href;
  47. /**
  48. * Link URL template
  49. *
  50. * @var string
  51. */
  52. public $template;
  53. /**
  54. * Array of key-value pairs: Key is the language, value the title
  55. *
  56. * @var array
  57. */
  58. public $titles = array();
  59. /**
  60. * Create a new instance and load data from the XML element
  61. *
  62. * @param string $relOrXml string with the relation name/URL
  63. * @param string $href HREF value
  64. * @param string $type Type value
  65. * @param boolean $isTemplate When set to true, the $href is
  66. * used as template
  67. */
  68. public function __construct(
  69. $rel = null, $href = null, $type = null, $isTemplate = false
  70. ) {
  71. $this->rel = $rel;
  72. if ($isTemplate) {
  73. $this->template = $href;
  74. } else {
  75. $this->href = $href;
  76. }
  77. $this->type = $type;
  78. }
  79. /**
  80. * Returns the title of the link in the given language.
  81. * If the language is not available, the first title without the language
  82. * is returned. If no such one exists, the first title is returned.
  83. *
  84. * @param string $lang 2-letter language name
  85. *
  86. * @return string|null Link title
  87. */
  88. public function getTitle($lang = null)
  89. {
  90. if (count($this->titles) == 0) {
  91. return null;
  92. }
  93. if ($lang == null) {
  94. return reset($this->titles);
  95. }
  96. if (isset($this->titles[$lang])) {
  97. return $this->titles[$lang];
  98. }
  99. if (isset($this->titles[''])) {
  100. return $this->titles[''];
  101. }
  102. //return first
  103. return reset($this->titles);
  104. }
  105. }
  106. ?>