123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace WhichBrowser\Model;
- use WhichBrowser\Constants;
- use WhichBrowser\Data;
- use WhichBrowser\Model\Primitive\Base;
- class Device extends Base
- {
- /** @var string */
- public $manufacturer;
- /** @var string */
- public $model;
- /** @var string */
- public $series;
- /** @var string */
- public $carrier;
- /** @var int */
- public $identifier;
- /** @var mixed */
- public $flag;
- /** @var string */
- public $type = '';
- /** @var string */
- public $subtype = '';
- /** @var int */
- public $identified = Constants\Id::NONE;
- /** @var boolean */
- public $generic = true;
- /** @var boolean */
- public $hidden = false;
- /**
- * Set the properties to the default values
- *
- * @param array|null $properties An optional array of properties to set after setting it to the default values
- *
- * @internal
- */
- public function reset($properties = null)
- {
- unset($this->manufacturer);
- unset($this->model);
- unset($this->series);
- unset($this->carrier);
- unset($this->identifier);
- $this->type = '';
- $this->subtype = '';
- $this->identified = Constants\Id::NONE;
- $this->generic = true;
- $this->hidden = false;
- if (is_array($properties)) {
- $this->set($properties);
- }
- }
- /**
- * Identify the manufacturer and model based on a pattern
- *
- * @param string $pattern The regular expression that defines the group that matches the model
- * @param string $subject The string the regular expression is matched with
- * @param array|null $defaults An optional array of properties to set together
- *
- * @return string
- */
- public function identifyModel($pattern, $subject, $defaults = [])
- {
- if (preg_match($pattern, $subject, $match)) {
- $this->manufacturer = !empty($defaults['manufacturer']) ? $defaults['manufacturer'] : null;
- $this->model = Data\DeviceModels::cleanup($match[1]);
- $this->identifier = preg_replace('/ (Mozilla|Opera|Obigo|AU.Browser|UP.Browser|Build|Java|PPC|AU-MIC.*)$/iu', '', $match[0]);
- $this->identifier = preg_replace('/_(TD|GPRS|LTE|BLEU|CMCC|CUCC)$/iu', '', $match[0]);
- if (isset($defaults['model'])) {
- if (is_callable($defaults['model'])) {
- $this->model = call_user_func($defaults['model'], $this->model);
- } else {
- $this->model = $defaults['model'];
- }
- }
- $this->generic = false;
- $this->identified |= Constants\Id::PATTERN;
- if (!empty($defaults['carrier'])) {
- $this->carrier = $defaults['carrier'];
- }
- if (!empty($defaults['type'])) {
- $this->type = $defaults['type'];
- }
- }
- }
- /**
- * Declare an positive identification
- *
- * @param array $properties An array, the key of an element determines the name of the property
- *
- * @return string
- */
- public function setIdentification($properties)
- {
- $this->reset($properties);
- if (!empty($this->model)) {
- $this->generic = false;
- }
-
- $this->identified |= Constants\Id::MATCH_UA;
- }
- /**
- * Get the name of the carrier in a human readable format
- *
- * @return string
- */
- public function getCarrier()
- {
- return $this->identified && !empty($this->carrier) ? $this->carrier : '';
- }
- /**
- * Get the name of the manufacturer in a human readable format
- *
- * @return string
- */
- public function getManufacturer()
- {
- return $this->identified && !empty($this->manufacturer) ? $this->manufacturer : '';
- }
- /**
- * Get the name of the model in a human readable format
- *
- * @return string
- */
- public function getModel()
- {
- if ($this->identified) {
- return trim((!empty($this->model) ? $this->model . ' ' : '') . (!empty($this->series) ? $this->series : ''));
- }
- return !empty($this->model) ? $this->model : '';
- }
- /**
- * Get the combined name of the manufacturer and model in a human readable format
- *
- * @return string
- */
- public function toString()
- {
- if ($this->hidden) {
- return '';
- }
-
- if ($this->identified) {
- $model = $this->getModel();
- $manufacturer = $this->getManufacturer();
- if ($manufacturer != '' && strpos($model, $manufacturer) === 0) {
- $manufacturer = '';
- }
- return trim($manufacturer . ' ' . $model);
- }
- return !empty($this->model) ? 'unrecognized device (' . $this->model . ')' : '';
- }
- /**
- * Check if device information is detected
- *
- * @return boolean
- */
- public function isDetected()
- {
- return !empty($this->type) || !empty($this->model) || !empty($this->manufacturer);
- }
- /**
- * Get an array of all defined properties
- *
- * @internal
- *
- * @return array
- */
- public function toArray()
- {
- $result = [];
- if (!empty($this->type)) {
- $result['type'] = $this->type;
- }
-
- if (!empty($this->subtype)) {
- $result['subtype'] = $this->subtype;
- }
- if (!empty($this->manufacturer)) {
- $result['manufacturer'] = $this->manufacturer;
- }
- if (!empty($this->model)) {
- $result['model'] = $this->model;
- }
- if (!empty($this->series)) {
- $result['series'] = $this->series;
- }
- if (!empty($this->carrier)) {
- $result['carrier'] = $this->carrier;
- }
- return $result;
- }
- }
|