Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
FormListChangeOpDeserializer
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setContext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createEntityChangeOp
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization;
4
5use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpFormAdd;
6use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpRemoveForm;
7use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpsFormsEdit;
8use Wikibase\Lexeme\MediaWiki\Api\Error\JsonFieldHasWrongType;
9use Wikibase\Lexeme\MediaWiki\Api\Error\JsonFieldIsRequired;
10use Wikibase\Repo\ChangeOp\ChangeOp;
11use Wikibase\Repo\ChangeOp\ChangeOpDeserializer;
12use Wikibase\Repo\ChangeOp\ChangeOps;
13use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException;
14
15/**
16 * Deserialize change requests on multiple forms
17 *
18 * @see docs/change-op-serialization.wiki for a description of the serialization format.
19 *
20 * @license GPL-2.0-or-later
21 */
22class FormListChangeOpDeserializer implements ChangeOpDeserializer {
23
24    private const PARAM_FORM_ID = 'id';
25
26    /**
27     * @var FormChangeOpDeserializer
28     */
29    private $formChangeOpDeserializer;
30
31    /**
32     * @var FormIdDeserializer
33     */
34    private $formIdDeserializer;
35
36    /**
37     * @var ValidationContext
38     */
39    private $validationContext;
40
41    public function __construct(
42        FormIdDeserializer $formIdDeserializer,
43        FormChangeOpDeserializer $formChangeOpDeserializer
44    ) {
45        $this->formChangeOpDeserializer = $formChangeOpDeserializer;
46        $this->formIdDeserializer = $formIdDeserializer;
47    }
48
49    public function setContext( ValidationContext $context ) {
50        $this->validationContext = $context;
51    }
52
53    /**
54     * @see ChangeOpDeserializer::createEntityChangeOp
55     *
56     * @param array $changeRequest
57     *
58     * @throws ChangeOpDeserializationException
59     *
60     * @return ChangeOp
61     */
62    public function createEntityChangeOp( array $changeRequest ) {
63        $lexemeChangeOps = new ChangeOps();
64        $changeOpsForForm = [];
65
66        if ( !is_array( $changeRequest['forms'] ) ) {
67            $this->validationContext->addViolation(
68                new JsonFieldHasWrongType( 'array', gettype( $changeRequest['forms'] ) )
69            );
70        }
71
72        foreach ( $changeRequest['forms'] as $index => $serializedForm ) {
73            $formValidationContext = $this->validationContext->at( $index );
74            $this->formChangeOpDeserializer->setContext( $formValidationContext );
75
76            if ( !is_array( $serializedForm ) ) {
77                $formValidationContext->addViolation(
78                    new JsonFieldHasWrongType( 'array', gettype( $serializedForm ) )
79                );
80            }
81
82            if ( array_key_exists( 'remove', $serializedForm ) ) {
83                if ( !array_key_exists( self::PARAM_FORM_ID, $serializedForm ) ) {
84                    $formValidationContext->addViolation(
85                        new JsonFieldIsRequired( self::PARAM_FORM_ID )
86                    );
87                }
88
89                $formId = $this->formIdDeserializer->deserialize(
90                    $serializedForm[self::PARAM_FORM_ID],
91                    $formValidationContext->at( self::PARAM_FORM_ID )
92                );
93
94                $lexemeChangeOps->add( new ChangeOpRemoveForm( $formId ) );
95            } elseif ( array_key_exists( 'add', $serializedForm ) ) {
96                $lexemeChangeOps->add(
97                    new ChangeOpFormAdd(
98                        $this->formChangeOpDeserializer->createEntityChangeOp( $serializedForm )
99                    )
100                );
101            } elseif ( array_key_exists( self::PARAM_FORM_ID, $serializedForm ) ) {
102                $changeOpsForForm[$serializedForm[self::PARAM_FORM_ID]] =
103                    $this->formChangeOpDeserializer->createEntityChangeOp( $serializedForm );
104            }
105        }
106
107        return new ChangeOps( [ $lexemeChangeOps, new ChangeOpsFormsEdit( $changeOpsForForm ) ] );
108    }
109
110}