Skip to content

Shapes : Add moveShape(), and say how shapes stack - #1002

Merged
Progi1984 merged 1 commit into
PHPOffice:masterfrom
sapientpro:feat/move-shape
Sep 9, 2026
Merged

Shapes : Add moveShape(), and say how shapes stack#1002
Progi1984 merged 1 commit into
PHPOffice:masterfrom
sapientpro:feat/move-shape

Conversation

@dkulyk

@dkulyk dkulyk commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Description

The shapes of a container are written in the order the collection holds them, and that order is the stacking order -- the first shape is at the back, the last is in front. Nothing could change that order but rebuilding the array and handing it back through setShapeCollection(), which is what the reporter does today:

Now i've fixed this with changing keys in the array but can there be a function added for this?

Remove-and-re-add is not a substitute either: unsetShape() is a bare unset(), so it leaves a hole in the keys and addShape()'s [] = then appends at max + 1.

moveShape(AbstractShape $shape, int $index) is moveSlide() (#966) one level down, with the same two guards and the same array_splice in one step. It lives on the ShapeCollection trait rather than on AbstractSlide, so a slide, a slide layout, a slide master, a Group and the note of a slide all get it at once.

$image = $slide->createDrawingShape();
$text = $slide->createRichTextShape();

$slide->moveShape($image, 1);   // the image is now in front of the text

The collection is reindexed before the search, because after unsetShape() a key no longer says the place a shape sits in and array_splice() counts places, not keys.

Fixes #866

Checklist:

  • I have run composer run-script check --timeout=0 and no errors were reported

phpstan reports [OK] No errors, php-cs-fixer 0 of 302, and the suite is green at 1149 tests, 9264 assertions.

  • The new code is covered by unit tests

Four tests in SlideTest: a move to the back and to the front, a move after unsetShape() has left a hole in the keys, a shape the container does not hold (InvalidParameterException) and an index past the end (OutOfBoundsException).

  • I have updated the documentation to describe the changes

A Stacking order section in docs/usage/shapes/introduction.md, which said nothing about stacking order before.

  • I have added a changelog entry

An entry under ## Enhancements in docs/changes/1.3.0.md.

@dkulyk
dkulyk force-pushed the feat/move-shape branch 2 times, most recently from ca0cfc9 to 86bd567 Compare September 8, 2026 19:06
@coveralls

coveralls commented Sep 8, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 94.127% (+0.004%) from 94.123% — sapientpro:feat/move-shape into PHPOffice:master

*
* @return static
*/
public function moveShape(AbstractShape $shape, int $index)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function moveShape(AbstractShape $shape, int $index)
public function moveShape(AbstractShape $shape, int $index): self

@Progi1984 Progi1984 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small feedback (and rebase if needed)

@Progi1984 Progi1984 added this to the 1.3.0 milestone Sep 9, 2026
The collection of a container is written in order and stacks in that order -- the first shape is
at the back -- and there was no way to change that order but to rebuild the array and hand it to
`setShapeCollection()`, which is what the reporter of PHPOffice#866 does today. Remove-and-re-add is not a
substitute: `unsetShape()` is a bare `unset()`, so it leaves a hole in the keys.

`moveShape(AbstractShape $shape, int $index): self` is `moveSlide()` one level down, on the
`ShapeCollection` trait, so a slide, a slide layout, a slide master, a group and the note of a
slide all get it. The keys are reindexed first, because a hole makes a key say something other
than the place the shape sits in.

The usage page said nothing about stacking order at all; it now does.
@Progi1984
Progi1984 merged commit e9676ae into PHPOffice:master Sep 9, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Possibility to change shapes from positition

3 participants