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