SimpleCallbacks.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Wikimedia\ParamValidator;
  3. use Wikimedia\ParamValidator\Util\UploadedFile;
  4. /**
  5. * Simple Callbacks implementation for $_GET/$_POST/$_FILES data
  6. *
  7. * Options array keys used by this class:
  8. * - 'useHighLimits': (bool) Return value from useHighLimits()
  9. *
  10. * @since 1.34
  11. * @unstable
  12. */
  13. class SimpleCallbacks implements Callbacks {
  14. /** @var (string|string[])[] $_GET/$_POST data */
  15. private $params;
  16. /** @var (array|UploadedFile)[] $_FILES data or UploadedFile instances */
  17. private $files;
  18. /** @var array Any recorded conditions */
  19. private $conditions = [];
  20. /**
  21. * @param (string|string[])[] $params Data from $_POST + $_GET
  22. * @param array[] $files Data from $_FILES
  23. */
  24. public function __construct( array $params, array $files = [] ) {
  25. $this->params = $params;
  26. $this->files = $files;
  27. }
  28. public function hasParam( $name, array $options ) {
  29. return isset( $this->params[$name] );
  30. }
  31. public function getValue( $name, $default, array $options ) {
  32. return $this->params[$name] ?? $default;
  33. }
  34. public function hasUpload( $name, array $options ) {
  35. return isset( $this->files[$name] );
  36. }
  37. public function getUploadedFile( $name, array $options ) {
  38. $file = $this->files[$name] ?? null;
  39. if ( $file && !$file instanceof UploadedFile ) {
  40. $file = new UploadedFile( $file );
  41. $this->files[$name] = $file;
  42. }
  43. return $file;
  44. }
  45. public function recordCondition( ValidationException $condition, array $options ) {
  46. $this->conditions[] = $condition;
  47. }
  48. /**
  49. * Fetch any recorded conditions
  50. * @return array[]
  51. */
  52. public function getRecordedConditions() {
  53. return $this->conditions;
  54. }
  55. /**
  56. * Clear any recorded conditions
  57. */
  58. public function clearRecordedConditions() {
  59. $this->conditions = [];
  60. }
  61. public function useHighLimits( array $options ) {
  62. return !empty( $options['useHighLimits'] );
  63. }
  64. }