NameVersion.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace WhichBrowser\Model\Primitive;
  3. use WhichBrowser\Model\Version;
  4. /**
  5. * @internal
  6. */
  7. class NameVersion extends Base
  8. {
  9. /**
  10. * @var string $name The name
  11. * @var string $alias An alternative name that is used for readable strings
  12. * @var \WhichBrowser\Model\Version $version Version information
  13. */
  14. public $name;
  15. public $alias;
  16. public $version;
  17. /**
  18. * Set the properties to the default values
  19. *
  20. * @param array|null $properties An optional array of properties to set after setting it to the default values
  21. *
  22. * @internal
  23. */
  24. public function reset($properties = null)
  25. {
  26. unset($this->name);
  27. unset($this->alias);
  28. unset($this->version);
  29. if (is_array($properties)) {
  30. $this->set($properties);
  31. }
  32. }
  33. /**
  34. * Identify the version based on a pattern
  35. *
  36. * @param string $pattern The regular expression that defines the group that matches the version string
  37. * @param string $subject The string the regular expression is matched with
  38. * @param array|null $defaults An optional array of properties to set together with the value
  39. *
  40. * @return string
  41. */
  42. public function identifyVersion($pattern, $subject, $defaults = [])
  43. {
  44. if (preg_match($pattern, $subject, $match)) {
  45. $version = $match[1];
  46. if (isset($defaults['type'])) {
  47. switch ($defaults['type']) {
  48. case 'underscore':
  49. $version = str_replace('_', '.', $version);
  50. break;
  51. case 'legacy':
  52. $version = preg_replace("/([0-9])([0-9])/", '$1.$2', $version);
  53. break;
  54. }
  55. }
  56. $this->version = new Version(array_merge($defaults, [ 'value' => $version ]));
  57. }
  58. }
  59. /**
  60. * Get the name in a human readable format
  61. *
  62. * @return string
  63. */
  64. public function getName()
  65. {
  66. return !empty($this->alias) ? $this->alias : (!empty($this->name) ? $this->name : '');
  67. }
  68. /**
  69. * Get the version in a human readable format
  70. *
  71. * @return string
  72. */
  73. public function getVersion()
  74. {
  75. return !empty($this->version) ? $this->version->toString() : '';
  76. }
  77. /**
  78. * Is a name detected?
  79. *
  80. * @return boolean
  81. */
  82. public function isDetected()
  83. {
  84. return !empty($this->name);
  85. }
  86. /**
  87. * Get the name and version in a human readable format
  88. *
  89. * @return string
  90. */
  91. public function toString()
  92. {
  93. return trim($this->getName() . ' ' . (!empty($this->version) && !$this->version->hidden ? $this->getVersion() : ''));
  94. }
  95. }