build: Upgrade mediawiki-codesniffer from 26.0.0 to 28.0.0
[lhc/web/wiklou.git] / includes / Revision / MutableRevisionSlots.php
1 <?php
2 /**
3 * Mutable version of RevisionSlots, for constructing a new revision.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Revision;
24
25 use Content;
26
27 /**
28 * Mutable version of RevisionSlots, for constructing a new revision.
29 *
30 * @since 1.31
31 * @since 1.32 Renamed from MediaWiki\Storage\MutableRevisionSlots
32 */
33 class MutableRevisionSlots extends RevisionSlots {
34
35 /**
36 * Constructs a MutableRevisionSlots that inherits from the given
37 * list of slots.
38 *
39 * @param SlotRecord[] $slots
40 *
41 * @return MutableRevisionSlots
42 */
43 public static function newFromParentRevisionSlots( array $slots ) {
44 $inherited = [];
45 foreach ( $slots as $slot ) {
46 $role = $slot->getRole();
47 $inherited[$role] = SlotRecord::newInherited( $slot );
48 }
49
50 return new MutableRevisionSlots( $inherited );
51 }
52
53 /**
54 * @param SlotRecord[] $slots An array of SlotRecords.
55 */
56 public function __construct( array $slots = [] ) {
57 parent::__construct( $slots );
58 }
59
60 /**
61 * Sets the given slot.
62 * If a slot with the same role is already present, it is replaced.
63 *
64 * @param SlotRecord $slot
65 */
66 public function setSlot( SlotRecord $slot ) {
67 if ( !is_array( $this->slots ) ) {
68 $this->getSlots(); // initialize $this->slots
69 }
70
71 $role = $slot->getRole();
72 $this->slots[$role] = $slot;
73 }
74
75 /**
76 * Sets the given slot to an inherited version of $slot.
77 * If a slot with the same role is already present, it is replaced.
78 *
79 * @param SlotRecord $slot
80 */
81 public function inheritSlot( SlotRecord $slot ) {
82 $this->setSlot( SlotRecord::newInherited( $slot ) );
83 }
84
85 /**
86 * Sets the content for the slot with the given role.
87 * If a slot with the same role is already present, it is replaced.
88 *
89 * @param string $role
90 * @param Content $content
91 */
92 public function setContent( $role, Content $content ) {
93 $slot = SlotRecord::newUnsaved( $role, $content );
94 $this->setSlot( $slot );
95 }
96
97 /**
98 * Remove the slot for the given role, discontinue the corresponding stream.
99 *
100 * @param string $role
101 */
102 public function removeSlot( $role ) {
103 if ( !is_array( $this->slots ) ) {
104 $this->getSlots(); // initialize $this->slots
105 }
106
107 unset( $this->slots[$role] );
108 }
109
110 }
111
112 /**
113 * Retain the old class name for backwards compatibility.
114 * @deprecated since 1.32
115 */
116 class_alias( MutableRevisionSlots::class, 'MediaWiki\Storage\MutableRevisionSlots' );