Browser.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace WhichBrowser\Model;
  3. use WhichBrowser\Model\Primitive\NameVersion;
  4. class Browser extends NameVersion
  5. {
  6. /**
  7. * @var \WhichBrowser\Model\Using $using Information about web views the browser is using
  8. * @var \WhichBrowser\Model\Family $family To which browser family does this browser belong
  9. */
  10. public $using;
  11. public $family;
  12. /** @var string */
  13. public $channel;
  14. /** @var boolean */
  15. public $stock = true;
  16. /** @var boolean */
  17. public $hidden = false;
  18. /** @var string */
  19. public $mode = '';
  20. /** @var string */
  21. public $type = '';
  22. /**
  23. * Set the properties to the default values
  24. *
  25. * @param array|null $properties An optional array of properties to set after setting it to the default values
  26. *
  27. * @internal
  28. */
  29. public function reset($properties = null)
  30. {
  31. parent::reset();
  32. unset($this->channel);
  33. unset($this->using);
  34. unset($this->family);
  35. $this->stock = true;
  36. $this->hidden = false;
  37. $this->mode = '';
  38. $this->type = '';
  39. if (is_array($properties)) {
  40. $this->set($properties);
  41. }
  42. }
  43. /**
  44. * Get the name in a human readable format
  45. *
  46. * @return string
  47. */
  48. public function getName()
  49. {
  50. $name = !empty($this->alias) ? $this->alias : (!empty($this->name) ? $this->name : '');
  51. return $name ? $name . (!empty($this->channel) ? ' ' . $this->channel : '') : '';
  52. }
  53. /**
  54. * Is the browser from the specified family
  55. *
  56. * @param string $name The name of the family
  57. *
  58. * @return boolean
  59. */
  60. public function isFamily($name)
  61. {
  62. if ($this->getName() == $name) {
  63. return true;
  64. }
  65. if (isset($this->family)) {
  66. if ($this->family->getName() == $name) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. /**
  73. * Is the browser using the specified webview
  74. *
  75. * @param string $name The name of the webview
  76. *
  77. * @return boolean
  78. */
  79. public function isUsing($name)
  80. {
  81. if (isset($this->using)) {
  82. if ($this->using->getName() == $name) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. /**
  89. * Get a combined name and version number in a human readable format
  90. *
  91. * @return string
  92. */
  93. public function toString()
  94. {
  95. if ($this->hidden) {
  96. return '';
  97. }
  98. $result = trim($this->getName() . ' ' . (!empty($this->version) && !$this->version->hidden ? $this->getVersion() : ''));
  99. if (empty($result) && isset($this->using)) {
  100. return $this->using->toString();
  101. }
  102. return $result;
  103. }
  104. /**
  105. * Get an array of all defined properties
  106. *
  107. * @internal
  108. *
  109. * @return array
  110. */
  111. public function toArray()
  112. {
  113. $result = [];
  114. if (!empty($this->name)) {
  115. $result['name'] = $this->name;
  116. }
  117. if (!empty($this->alias)) {
  118. $result['alias'] = $this->alias;
  119. }
  120. if (!empty($this->using)) {
  121. $result['using'] = $this->using->toArray();
  122. }
  123. if (!empty($this->family)) {
  124. $result['family'] = $this->family->toArray();
  125. }
  126. if (!empty($this->version)) {
  127. $result['version'] = $this->version->toArray();
  128. }
  129. if (!empty($this->type)) {
  130. $result['type'] = $this->type;
  131. }
  132. if (isset($result['version']) && !count($result['version'])) {
  133. unset($result['version']);
  134. }
  135. return $result;
  136. }
  137. }