Os.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace WhichBrowser\Model;
  3. use WhichBrowser\Model\Primitive\NameVersion;
  4. class Os extends NameVersion
  5. {
  6. /**
  7. * @var \WhichBrowser\Model\Family $family To which family does this operating system belong
  8. */
  9. public $family;
  10. /** @var string */
  11. public $edition;
  12. /** @var boolean */
  13. public $hidden = false;
  14. /**
  15. * Set the properties to the default values
  16. *
  17. * @param array|null $properties An optional array of properties to set after setting it to the default values
  18. *
  19. * @internal
  20. */
  21. public function reset($properties = null)
  22. {
  23. parent::reset();
  24. unset($this->family);
  25. unset($this->edition);
  26. $this->hidden = false;
  27. if (is_array($properties)) {
  28. $this->set($properties);
  29. }
  30. }
  31. /**
  32. * Return the name of the operating system family
  33. *
  34. * @return string
  35. */
  36. public function getFamily()
  37. {
  38. if (isset($this->family)) {
  39. return $this->family->getName();
  40. }
  41. return $this->getName();
  42. }
  43. /**
  44. * Is the operating from the specified family
  45. *
  46. * @param string $name The name of the family
  47. *
  48. * @return boolean
  49. */
  50. public function isFamily($name)
  51. {
  52. if ($this->getName() == $name) {
  53. return true;
  54. }
  55. if (isset($this->family)) {
  56. if ($this->family->getName() == $name) {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. /**
  63. * Get the name and version in a human readable format
  64. *
  65. * @return string
  66. */
  67. public function toString()
  68. {
  69. if ($this->hidden) {
  70. return '';
  71. }
  72. return trim($this->getName() .
  73. (!empty($this->version) && !$this->version->hidden ? ' ' . $this->getVersion() : '')) .
  74. (!empty($this->edition) ? ' ' . $this->edition : '');
  75. }
  76. /**
  77. * Get an array of all defined properties
  78. *
  79. * @internal
  80. *
  81. * @return array
  82. */
  83. public function toArray()
  84. {
  85. $result = [];
  86. if (!empty($this->name)) {
  87. $result['name'] = $this->name;
  88. }
  89. if (!empty($this->family)) {
  90. $result['family'] = $this->family->toArray();
  91. }
  92. if (!empty($this->alias)) {
  93. $result['alias'] = $this->alias;
  94. }
  95. if (!empty($this->edition)) {
  96. $result['edition'] = $this->edition;
  97. }
  98. if (!empty($this->version)) {
  99. $result['version'] = $this->version->toArray();
  100. }
  101. if (isset($result['version']) && !count($result['version'])) {
  102. unset($result['version']);
  103. }
  104. return $result;
  105. }
  106. }