Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.17% covered (warning)
54.17%
13 / 24
37.50% covered (danger)
37.50%
3 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParamValidatorCallbacks
54.17% covered (warning)
54.17%
13 / 24
37.50% covered (danger)
37.50%
3 / 8
40.65
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getParamsFromSource
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 hasParam
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getValue
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasUpload
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getUploadedFile
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 recordCondition
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 useHighLimits
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Rest\Validator;
4
5use InvalidArgumentException;
6use MediaWiki\Permissions\Authority;
7use MediaWiki\Rest\RequestInterface;
8use Psr\Http\Message\UploadedFileInterface;
9use Wikimedia\Message\DataMessageValue;
10use Wikimedia\ParamValidator\Callbacks;
11
12class ParamValidatorCallbacks implements Callbacks {
13
14    /** @var Authority */
15    private $authority;
16
17    /** @var RequestInterface */
18    private $request;
19
20    public function __construct(
21        RequestInterface $request,
22        Authority $authority
23    ) {
24        $this->request = $request;
25        $this->authority = $authority;
26    }
27
28    /**
29     * Get the raw parameters from a source in the request
30     * @param string $source 'path', 'query', or 'post'
31     * @return array
32     */
33    private function getParamsFromSource( $source ) {
34        // This switch block must match Validator::KNOWN_PARAM_SOURCES
35        switch ( $source ) {
36            case 'path':
37                return $this->request->getPathParams();
38
39            case 'query':
40                return $this->request->getQueryParams();
41
42            case 'post':
43                return $this->request->getPostParams();
44
45            case 'body':
46                return $this->request->getParsedBody() ?? [];
47
48            default:
49                throw new InvalidArgumentException( __METHOD__ . ": Invalid source '$source'" );
50        }
51    }
52
53    public function hasParam( $name, array $options ) {
54        $params = $this->getParamsFromSource( $options['source'] );
55        return isset( $params[$name] );
56    }
57
58    public function getValue( $name, $default, array $options ) {
59        $params = $this->getParamsFromSource( $options['source'] );
60        return $params[$name] ?? $default;
61        // @todo Should normalization to NFC UTF-8 be done here (much like in the
62        // action API and the rest of MW), or should it be left to handlers to
63        // do whatever normalization they need?
64    }
65
66    public function hasUpload( $name, array $options ) {
67        if ( $options['source'] !== 'post' ) {
68            return false;
69        }
70        return $this->getUploadedFile( $name, $options ) !== null;
71    }
72
73    public function getUploadedFile( $name, array $options ) {
74        if ( $options['source'] !== 'post' ) {
75            return null;
76        }
77        $upload = $this->request->getUploadedFiles()[$name] ?? null;
78        return $upload instanceof UploadedFileInterface ? $upload : null;
79    }
80
81    public function recordCondition(
82        DataMessageValue $message, $name, $value, array $settings, array $options
83    ) {
84        // @todo Figure out how to handle warnings
85    }
86
87    public function useHighLimits( array $options ) {
88        return $this->authority->isAllowed( 'apihighlimits' );
89    }
90
91}