class-wpseo-validator.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals
  6. */
  7. /**
  8. * Class WPSEO_Validator.
  9. */
  10. class WPSEO_Validator {
  11. /**
  12. * Validates whether the passed variable is a boolean.
  13. *
  14. * @param mixed $variable The variable to validate.
  15. *
  16. * @return bool Whether or not the passed variable is a valid boolean.
  17. */
  18. public static function is_boolean( $variable ) {
  19. if ( is_bool( $variable ) ) {
  20. return true;
  21. }
  22. return filter_var( $variable, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) !== null;
  23. }
  24. /**
  25. * Validates whether the passed variable is a string.
  26. *
  27. * @param mixed $variable The variable to validate.
  28. *
  29. * @return bool Whether or not the passed variable is a string.
  30. */
  31. public static function is_string( $variable ) {
  32. return is_string( $variable );
  33. }
  34. /**
  35. * Validates whether the passed variable is a non-empty string.
  36. *
  37. * @param mixed $variable The variable to validate.
  38. *
  39. * @return bool Whether or not the passed value is a non-empty string.
  40. */
  41. public static function is_non_empty_string( $variable ) {
  42. return self::is_string( $variable ) && $variable !== '';
  43. }
  44. /**
  45. * Validates whether the passed variable is an integer.
  46. *
  47. * @param mixed $variable The variable to validate.
  48. *
  49. * @return bool Whether or not the passed variable is an integer.
  50. */
  51. public static function is_integer( $variable ) {
  52. return filter_var( $variable, FILTER_VALIDATE_INT ) || filter_var( $variable, FILTER_VALIDATE_INT ) === 0;
  53. }
  54. /**
  55. * Determines whether a particular key exists within the passed dataset.
  56. *
  57. * @param array $data The dataset to search through.
  58. * @param string $key The key to search for.
  59. *
  60. * @return bool Whether or not the key exists.
  61. */
  62. public static function key_exists( array $data, $key ) {
  63. return array_key_exists( $key, $data );
  64. }
  65. }