Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
ValidationContext | |
100.00% |
20 / 20 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
at | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addViolation | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
toApiUsageException | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
getParts | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization; |
4 | |
5 | use ApiUsageException; |
6 | use MediaWiki\Status\Status; |
7 | use Wikibase\Lexeme\MediaWiki\Api\Error\ApiError; |
8 | |
9 | /** |
10 | * @license GPL-2.0-or-later |
11 | */ |
12 | class ValidationContext { |
13 | |
14 | /** |
15 | * @var self|null |
16 | */ |
17 | private $parentContext; |
18 | |
19 | private $field; |
20 | |
21 | private $level = ''; |
22 | |
23 | /** |
24 | * @var ApiError[] |
25 | */ |
26 | private $violations = []; |
27 | |
28 | private function __construct( ?self $parentContext, $field, $level = null ) { |
29 | $this->parentContext = $parentContext; |
30 | $this->field = $field; |
31 | if ( $level !== null ) { |
32 | $this->level = $level; |
33 | } |
34 | } |
35 | |
36 | public static function create( $field ) { |
37 | return new self( null, $field ); |
38 | } |
39 | |
40 | /** |
41 | * Start a new level |
42 | * |
43 | * @param string $level |
44 | * @return self |
45 | */ |
46 | public function at( $level ) { |
47 | return new self( $this, $this->field, $level ); |
48 | } |
49 | |
50 | public function addViolation( ApiError $error ) { |
51 | $this->violations[] = $error; |
52 | |
53 | $this->toApiUsageException(); |
54 | } |
55 | |
56 | private function toApiUsageException() { |
57 | foreach ( $this->violations as $violation ) { |
58 | /** @var ApiError $error */ |
59 | $msg = $violation->asApiMessage( $this->field, $this->getParts() ); |
60 | $msg->setApiData( [ 'parameterName' => $this->field, 'fieldPath' => $this->getParts() ] ); |
61 | |
62 | $status = Status::newGood(); |
63 | $status->fatal( $msg ); |
64 | |
65 | throw new ApiUsageException( null, $status ); |
66 | } |
67 | } |
68 | |
69 | private function getParts() { |
70 | if ( $this->parentContext === null ) { |
71 | return []; |
72 | } |
73 | return array_merge( |
74 | $this->parentContext->getParts(), |
75 | [ $this->level ] |
76 | ); |
77 | } |
78 | |
79 | } |