class-invalid-argument-exception.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals
  6. */
  7. /**
  8. * Class WPSEO_Invalid_Argument_Exception.
  9. */
  10. class WPSEO_Invalid_Argument_Exception extends InvalidArgumentException {
  11. /**
  12. * Creates an invalid empty parameter exception.
  13. *
  14. * @param string $name The name of the parameter.
  15. *
  16. * @return WPSEO_Invalid_Argument_Exception The exception.
  17. */
  18. public static function empty_parameter( $name ) {
  19. return new self(
  20. sprintf(
  21. /* translators: %1$s expands to the parameter name. */
  22. __( 'The parameter `%1$s` cannot be empty.', 'wordpress-seo' ),
  23. $name
  24. )
  25. );
  26. }
  27. /**
  28. * Creates an invalid parameter exception.
  29. *
  30. * @param mixed $parameter The parameter value of the field.
  31. * @param string $name The name of the field.
  32. * @param string $expected The expected type.
  33. *
  34. * @return WPSEO_Invalid_Argument_Exception The exception.
  35. */
  36. public static function invalid_parameter_type( $parameter, $name, $expected ) {
  37. return new self(
  38. sprintf(
  39. /* translators: %1$s expands to the parameter name. %2$s expands to the expected type and %3$s expands to the expected type. */
  40. __( 'Invalid type for parameter `%1$s` passed. Expected `%2$s`, but got `%3$s`', 'wordpress-seo' ),
  41. $name,
  42. $expected,
  43. gettype( $parameter )
  44. )
  45. );
  46. }
  47. /**
  48. * Creates an invalid integer parameter exception.
  49. *
  50. * @param mixed $parameter The parameter value of the field.
  51. * @param string $name The name of the field.
  52. *
  53. * @return WPSEO_Invalid_Argument_Exception The exception.
  54. */
  55. public static function invalid_integer_parameter( $parameter, $name ) {
  56. return self::invalid_parameter_type( $parameter, $name, 'integer' );
  57. }
  58. /**
  59. * Creates an invalid string parameter exception.
  60. *
  61. * @param mixed $parameter The parameter value of the field.
  62. * @param string $name The name of the field.
  63. *
  64. * @return WPSEO_Invalid_Argument_Exception The exception.
  65. */
  66. public static function invalid_string_parameter( $parameter, $name ) {
  67. return self::invalid_parameter_type( $parameter, $name, 'string' );
  68. }
  69. /**
  70. * Creates an invalid boolean parameter exception.
  71. *
  72. * @param mixed $parameter The parameter value of the field.
  73. * @param string $name The name of the field.
  74. *
  75. * @return WPSEO_Invalid_Argument_Exception The exception.
  76. */
  77. public static function invalid_boolean_parameter( $parameter, $name ) {
  78. return self::invalid_parameter_type( $parameter, $name, 'boolean' );
  79. }
  80. /**
  81. * Creates an invalid callable parameter exception.
  82. *
  83. * @param mixed $parameter The parameter value of the field.
  84. * @param string $name The name of the field.
  85. *
  86. * @return WPSEO_Invalid_Argument_Exception The exception.
  87. */
  88. public static function invalid_callable_parameter( $parameter, $name ) {
  89. return self::invalid_parameter_type( $parameter, $name, 'callable' );
  90. }
  91. /**
  92. * Creates an invalid object type exception.
  93. *
  94. * @param string $type The type of the field.
  95. *
  96. * @return WPSEO_Invalid_Argument_Exception The exception.
  97. */
  98. public static function invalid_type( $type ) {
  99. return new self(
  100. sprintf(
  101. /* translators: %1$s expands to the object type. */
  102. __( 'The object type `%1$s` is invalid', 'wordpress-seo' ),
  103. $type
  104. )
  105. );
  106. }
  107. /**
  108. * Creates an invalid object subtype exception.
  109. *
  110. * @param string $subtype The invalid subtype.
  111. * @param string $type The parent type of the subtype.
  112. *
  113. * @return WPSEO_Invalid_Argument_Exception The exception.
  114. */
  115. public static function invalid_subtype( $subtype, $type ) {
  116. return new self(
  117. sprintf(
  118. /* translators: %1$s expands to the object subtype. %2$s resolved to the object type. */
  119. __( '`%1$s` is not a valid subtype of `%2$s`', 'wordpress-seo' ),
  120. $subtype,
  121. $type
  122. )
  123. );
  124. }
  125. /**
  126. * Creates an unknown object exception.
  127. *
  128. * @param int $id The ID that was searched for.
  129. * @param string $type The type of object that was being searched for.
  130. *
  131. * @return WPSEO_Invalid_Argument_Exception The exception.
  132. */
  133. public static function unknown_object( $id, $type ) {
  134. return new self(
  135. sprintf(
  136. /* translators: %1$s expands to the object ID. %2$s resolved to the object type. */
  137. __( 'No object with ID %1$s and %2$s could be found', 'wordpress-seo' ),
  138. $id,
  139. $type
  140. )
  141. );
  142. }
  143. }