Device.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace WhichBrowser\Model;
  3. use WhichBrowser\Constants;
  4. use WhichBrowser\Data;
  5. use WhichBrowser\Model\Primitive\Base;
  6. class Device extends Base
  7. {
  8. /** @var string */
  9. public $manufacturer;
  10. /** @var string */
  11. public $model;
  12. /** @var string */
  13. public $series;
  14. /** @var string */
  15. public $carrier;
  16. /** @var int */
  17. public $identifier;
  18. /** @var mixed */
  19. public $flag;
  20. /** @var string */
  21. public $type = '';
  22. /** @var string */
  23. public $subtype = '';
  24. /** @var int */
  25. public $identified = Constants\Id::NONE;
  26. /** @var boolean */
  27. public $generic = true;
  28. /** @var boolean */
  29. public $hidden = false;
  30. /**
  31. * Set the properties to the default values
  32. *
  33. * @param array|null $properties An optional array of properties to set after setting it to the default values
  34. *
  35. * @internal
  36. */
  37. public function reset($properties = null)
  38. {
  39. unset($this->manufacturer);
  40. unset($this->model);
  41. unset($this->series);
  42. unset($this->carrier);
  43. unset($this->identifier);
  44. $this->type = '';
  45. $this->subtype = '';
  46. $this->identified = Constants\Id::NONE;
  47. $this->generic = true;
  48. $this->hidden = false;
  49. if (is_array($properties)) {
  50. $this->set($properties);
  51. }
  52. }
  53. /**
  54. * Identify the manufacturer and model based on a pattern
  55. *
  56. * @param string $pattern The regular expression that defines the group that matches the model
  57. * @param string $subject The string the regular expression is matched with
  58. * @param array|null $defaults An optional array of properties to set together
  59. *
  60. * @return string
  61. */
  62. public function identifyModel($pattern, $subject, $defaults = [])
  63. {
  64. if (preg_match($pattern, $subject, $match)) {
  65. $this->manufacturer = !empty($defaults['manufacturer']) ? $defaults['manufacturer'] : null;
  66. $this->model = Data\DeviceModels::cleanup($match[1]);
  67. $this->identifier = preg_replace('/ (Mozilla|Opera|Obigo|AU.Browser|UP.Browser|Build|Java|PPC|AU-MIC.*)$/iu', '', $match[0]);
  68. $this->identifier = preg_replace('/_(TD|GPRS|LTE|BLEU|CMCC|CUCC)$/iu', '', $match[0]);
  69. if (isset($defaults['model'])) {
  70. if (is_callable($defaults['model'])) {
  71. $this->model = call_user_func($defaults['model'], $this->model);
  72. } else {
  73. $this->model = $defaults['model'];
  74. }
  75. }
  76. $this->generic = false;
  77. $this->identified |= Constants\Id::PATTERN;
  78. if (!empty($defaults['carrier'])) {
  79. $this->carrier = $defaults['carrier'];
  80. }
  81. if (!empty($defaults['type'])) {
  82. $this->type = $defaults['type'];
  83. }
  84. }
  85. }
  86. /**
  87. * Declare an positive identification
  88. *
  89. * @param array $properties An array, the key of an element determines the name of the property
  90. *
  91. * @return string
  92. */
  93. public function setIdentification($properties)
  94. {
  95. $this->reset($properties);
  96. if (!empty($this->model)) {
  97. $this->generic = false;
  98. }
  99. $this->identified |= Constants\Id::MATCH_UA;
  100. }
  101. /**
  102. * Get the name of the carrier in a human readable format
  103. *
  104. * @return string
  105. */
  106. public function getCarrier()
  107. {
  108. return $this->identified && !empty($this->carrier) ? $this->carrier : '';
  109. }
  110. /**
  111. * Get the name of the manufacturer in a human readable format
  112. *
  113. * @return string
  114. */
  115. public function getManufacturer()
  116. {
  117. return $this->identified && !empty($this->manufacturer) ? $this->manufacturer : '';
  118. }
  119. /**
  120. * Get the name of the model in a human readable format
  121. *
  122. * @return string
  123. */
  124. public function getModel()
  125. {
  126. if ($this->identified) {
  127. return trim((!empty($this->model) ? $this->model . ' ' : '') . (!empty($this->series) ? $this->series : ''));
  128. }
  129. return !empty($this->model) ? $this->model : '';
  130. }
  131. /**
  132. * Get the combined name of the manufacturer and model in a human readable format
  133. *
  134. * @return string
  135. */
  136. public function toString()
  137. {
  138. if ($this->hidden) {
  139. return '';
  140. }
  141. if ($this->identified) {
  142. $model = $this->getModel();
  143. $manufacturer = $this->getManufacturer();
  144. if ($manufacturer != '' && strpos($model, $manufacturer) === 0) {
  145. $manufacturer = '';
  146. }
  147. return trim($manufacturer . ' ' . $model);
  148. }
  149. return !empty($this->model) ? 'unrecognized device (' . $this->model . ')' : '';
  150. }
  151. /**
  152. * Check if device information is detected
  153. *
  154. * @return boolean
  155. */
  156. public function isDetected()
  157. {
  158. return !empty($this->type) || !empty($this->model) || !empty($this->manufacturer);
  159. }
  160. /**
  161. * Get an array of all defined properties
  162. *
  163. * @internal
  164. *
  165. * @return array
  166. */
  167. public function toArray()
  168. {
  169. $result = [];
  170. if (!empty($this->type)) {
  171. $result['type'] = $this->type;
  172. }
  173. if (!empty($this->subtype)) {
  174. $result['subtype'] = $this->subtype;
  175. }
  176. if (!empty($this->manufacturer)) {
  177. $result['manufacturer'] = $this->manufacturer;
  178. }
  179. if (!empty($this->model)) {
  180. $result['model'] = $this->model;
  181. }
  182. if (!empty($this->series)) {
  183. $result['series'] = $this->series;
  184. }
  185. if (!empty($this->carrier)) {
  186. $result['carrier'] = $this->carrier;
  187. }
  188. return $result;
  189. }
  190. }