MediaWiki master
MutableRevisionSlots.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
25use Content;
26
37 private $resetCallback;
38
49 public static function newFromParentRevisionSlots(
50 array $slots,
51 ?callable $resetCallback = null
52 ) {
53 $inherited = [];
54 foreach ( $slots as $slot ) {
55 $role = $slot->getRole();
56 $inherited[$role] = SlotRecord::newInherited( $slot );
57 }
58
59 return new MutableRevisionSlots( $inherited, $resetCallback );
60 }
61
67 public function __construct( array $slots = [], ?callable $resetCallback = null ) {
68 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
69 parent::__construct( $slots );
70 $this->resetCallback = $resetCallback;
71 }
72
79 public function setSlot( SlotRecord $slot ) {
80 if ( !is_array( $this->slots ) ) {
81 $this->getSlots(); // initialize $this->slots
82 }
83
84 $role = $slot->getRole();
85 $this->slots[$role] = $slot;
86 $this->triggerResetCallback();
87 }
88
95 public function inheritSlot( SlotRecord $slot ) {
96 $this->setSlot( SlotRecord::newInherited( $slot ) );
97 }
98
106 public function setContent( $role, Content $content ) {
107 $slot = SlotRecord::newUnsaved( $role, $content );
108 $this->setSlot( $slot );
109 }
110
116 public function removeSlot( $role ) {
117 if ( !is_array( $this->slots ) ) {
118 $this->getSlots(); // initialize $this->slots
119 }
120
121 unset( $this->slots[$role] );
122 $this->triggerResetCallback();
123 }
124
128 private function triggerResetCallback() {
129 if ( $this->resetCallback ) {
130 ( $this->resetCallback )( $this );
131 }
132 }
133
134}
Mutable version of RevisionSlots, for constructing a new revision.
setSlot(SlotRecord $slot)
Sets the given slot.
inheritSlot(SlotRecord $slot)
Sets the given slot to an inherited version of $slot.
static newFromParentRevisionSlots(array $slots, ?callable $resetCallback=null)
Constructs a MutableRevisionSlots that inherits from the given list of slots.
__construct(array $slots=[], ?callable $resetCallback=null)
setContent( $role, Content $content)
Sets the content for the slot with the given role.
removeSlot( $role)
Remove the slot for the given role, discontinue the corresponding stream.
Value object representing the set of slots belonging to a revision.
getSlots()
Returns an associative array that maps role names to SlotRecords.
Value object representing a content slot associated with a page revision.
getRole()
Returns the role of the slot.
static newInherited(SlotRecord $slot)
Constructs a new SlotRecord for a new revision, inheriting the content of the given SlotRecord of a p...
static newUnsaved(string $role, Content $content, bool $derived=false)
Constructs a new Slot from a Content object for a new revision.
Base interface for representing page content.
Definition Content.php:37