Whip_VersionRequirement

A value object containing a version requirement for a component version.

Defined (1)

The class is defined in the following location(s).

/vendor/yoast/whip/src/Whip_VersionRequirement.php  
  1. class Whip_VersionRequirement implements Whip_Requirement { 
  2. /** 
  3. * @var string 
  4. */ 
  5. private $component; 
  6.  
  7. /** 
  8. * @var string 
  9. */ 
  10. private $version; 
  11.  
  12. /** 
  13. * @var string 
  14. */ 
  15. private $operator; 
  16.  
  17. /** 
  18. * Whip_Requirement constructor. 
  19. * @param string $component 
  20. * @param string $version 
  21. * @param string $operator 
  22. */ 
  23. public function __construct( $component, $version, $operator = '=' ) { 
  24. $this->validateParameters( $component, $version, $operator ); 
  25.  
  26. $this->component = $component; 
  27. $this->version = $version; 
  28. $this->operator = $operator; 
  29.  
  30. /** 
  31. * Gets the component name defined for the requirement. 
  32. * @return string The component name. 
  33. */ 
  34. public function component() { 
  35. return $this->component; 
  36.  
  37. /** 
  38. * Gets the components version defined for the requirement. 
  39. * @return string 
  40. */ 
  41. public function version() { 
  42. return $this->version; 
  43.  
  44. /** 
  45. * Returns the operator to use when comparing version numbers. 
  46. * @return string The comparison operator. 
  47. */ 
  48. public function operator() { 
  49. return $this->operator; 
  50.  
  51. /** 
  52. * Creates a new version requirement from a comparison string 
  53. * @throws Whip_InvalidVersionComparisonString When an invalid version comparison string is passed. 
  54. *@param string $component The component for this version requirement. 
  55. * @param string $comparisonString The comparison string for this version requirement. 
  56. *@returns Whip_VersionRequirement The parsed version requirement. 
  57. */ 
  58. public static function fromCompareString( $component, $comparisonString ) { 
  59.  
  60. $matcher = '(' . 
  61. '(>=?)' . // Matches >= and >. 
  62. '|' . 
  63. '(<=?)' . // Matches <= and <. 
  64. ')' . 
  65. '([^>=<\s]+)'; // Matches anything except >, <, =, and whitespace. 
  66.  
  67. if ( ! preg_match( '#' . $matcher . '#', $comparisonString, $match ) ) { 
  68. throw new Whip_InvalidVersionComparisonString( $comparisonString ); 
  69.  
  70. $version = $match[4]; 
  71. $operator = $match[1]; 
  72.  
  73. return new Whip_VersionRequirement( $component, $version, $operator ); 
  74.  
  75. /** 
  76. * Validates the parameters passed to the requirement. 
  77. * @param string $component The component name. 
  78. * @param string $version The component version. 
  79. * @param string $operator The operator to use when comparing version. 
  80. * @throws Whip_EmptyProperty 
  81. * @throws Whip_InvalidOperatorType 
  82. * @throws Whip_InvalidType 
  83. */ 
  84. private function validateParameters( $component, $version, $operator ) { 
  85. if ( empty( $component ) ) { 
  86. throw new Whip_EmptyProperty( 'Component' ); 
  87.  
  88. if ( !is_string( $component ) ) { 
  89. throw new Whip_InvalidType( 'Component', 'string', $component ); 
  90.  
  91. if ( empty( $version ) ) { 
  92. throw new Whip_EmptyProperty( 'Version' ); 
  93.  
  94. if ( !is_string( $version ) ) { 
  95. throw new Whip_InvalidType( 'Version', 'string', $version ); 
  96.  
  97. if ( empty( $operator ) ) { 
  98. throw new Whip_EmptyProperty( 'Operator' ); 
  99.  
  100. if ( !is_string( $operator ) ) { 
  101. throw new Whip_InvalidType( 'Operator', 'string', $operator ); 
  102.  
  103. if ( ! in_array( $operator, array( '=', '==', '===', '<', '>', '<=', '>=' ), true ) ) { 
  104. throw new Whip_InvalidOperatorType( $operator );