Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.48% covered (success)
90.48%
19 / 21
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
MutableRevisionSlots
90.48% covered (success)
90.48%
19 / 21
71.43% covered (warning)
71.43%
5 / 7
11.10
0.00% covered (danger)
0.00%
0 / 1
 newFromParentRevisionSlots
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setSlot
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 inheritSlot
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setContent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 removeSlot
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 triggerResetCallback
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Mutable version of RevisionSlots, for constructing a new revision.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Revision;
10
11use MediaWiki\Content\Content;
12
13/**
14 * Mutable version of RevisionSlots, for constructing a new revision.
15 *
16 * @since 1.31
17 * @since 1.32 Renamed from MediaWiki\Storage\MutableRevisionSlots
18 */
19class MutableRevisionSlots extends RevisionSlots {
20    /**
21     * @var callable|null
22     */
23    private $resetCallback;
24
25    /**
26     * Constructs a MutableRevisionSlots that inherits from the given
27     * list of slots.
28     *
29     * @param SlotRecord[] $slots
30     * @param callable|null $resetCallback Callback to be triggered whenever slots change.
31     *        Signature: function ( MutableRevisionSlots ): void.
32     *
33     * @return MutableRevisionSlots
34     */
35    public static function newFromParentRevisionSlots(
36        array $slots,
37        ?callable $resetCallback = null
38    ) {
39        $inherited = [];
40        foreach ( $slots as $slot ) {
41            $role = $slot->getRole();
42            $inherited[$role] = SlotRecord::newInherited( $slot );
43        }
44
45        return new MutableRevisionSlots( $inherited, $resetCallback );
46    }
47
48    /**
49     * @param SlotRecord[] $slots An array of SlotRecords.
50     * @param callable|null $resetCallback Callback to be triggered whenever slots change.
51     *        Signature: function ( MutableRevisionSlots ): void.
52     */
53    public function __construct( array $slots = [], ?callable $resetCallback = null ) {
54        parent::__construct( $slots );
55        $this->resetCallback = $resetCallback;
56    }
57
58    /**
59     * Sets the given slot.
60     * If a slot with the same role is already present, it is replaced.
61     */
62    public function setSlot( SlotRecord $slot ) {
63        if ( !is_array( $this->slots ) ) {
64            $this->getSlots(); // initialize $this->slots
65        }
66
67        $role = $slot->getRole();
68        $this->slots[$role] = $slot;
69        $this->triggerResetCallback();
70    }
71
72    /**
73     * Sets the given slot to an inherited version of $slot.
74     * If a slot with the same role is already present, it is replaced.
75     */
76    public function inheritSlot( SlotRecord $slot ) {
77        $this->setSlot( SlotRecord::newInherited( $slot ) );
78    }
79
80    /**
81     * Sets the content for the slot with the given role.
82     * If a slot with the same role is already present, it is replaced.
83     *
84     * @param string $role
85     * @param Content $content
86     */
87    public function setContent( $role, Content $content ) {
88        $slot = SlotRecord::newUnsaved( $role, $content );
89        $this->setSlot( $slot );
90    }
91
92    /**
93     * Remove the slot for the given role, discontinue the corresponding stream.
94     *
95     * @param string $role
96     */
97    public function removeSlot( $role ) {
98        if ( !is_array( $this->slots ) ) {
99            $this->getSlots();  // initialize $this->slots
100        }
101
102        unset( $this->slots[$role] );
103        $this->triggerResetCallback();
104    }
105
106    /**
107     * Trigger the reset callback supplied to the constructor, if any.
108     */
109    private function triggerResetCallback() {
110        if ( $this->resetCallback ) {
111            ( $this->resetCallback )( $this );
112        }
113    }
114
115}