Skip to content

Commit

Permalink
createAutoShape : Add method to create geometric shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasanshahid authored and Progi1984 committed Jan 27, 2025
1 parent d16456d commit ea93563
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Group Shape: moving the shape now moves all contained shapes. Offsets and size are calculated based on the contained shapes by [@DennisBirkholz](https://github.com/DennisBirkholz) in [#690](https://github.com/PHPOffice/PHPPresentation/pull/690)
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#839](https://github.com/PHPOffice/PHPPresentation/pull/839)
- `phpoffice/phpspreadsheet`: Allow version 3.0 by [@Progi1984](https://github.com/Progi1984) fixing [#836](https://github.com/PHPOffice/PHPPresentation/pull/836) in [#839](https://github.com/PHPOffice/PHPPresentation/pull/839)
- `createAutoShape` : Add method to create geometric shapes by [@mhasanshahid](https://github.com/mhasanshahid) & [@Progi1984](https://github.com/Progi1984) in [#848](https://github.com/PHPOffice/PHPPresentation/pull/848)

## Bug fixes

Expand Down
8 changes: 8 additions & 0 deletions docs/usage/shapes/autoshape.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ $shape = new AutoShape();
$slide->addShape($shape)
```

Or we can use `createAutoShape` method of slide.

Example:

```php
$slide->createAutoShape();
```

## Text

You can define text of the geometric form with `setText` method.
Expand Down
11 changes: 11 additions & 0 deletions src/PhpPresentation/Shape/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ public function createLineShape(int $fromX, int $fromY, int $toX, int $toY): Lin
return $shape;
}

/**
* Create geometric shape.
*/
public function createAutoShape(): AutoShape
{
$shape = new AutoShape();
$this->addShape($shape);

return $shape;
}

/**
* Create chart shape.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/PhpPresentation/Slide/AbstractSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpOffice\PhpPresentation\ComparableInterface;
use PhpOffice\PhpPresentation\GeometryCalculator;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\AutoShape;
use PhpOffice\PhpPresentation\Shape\Chart;
use PhpOffice\PhpPresentation\Shape\Drawing\File;
use PhpOffice\PhpPresentation\Shape\Group;
Expand Down Expand Up @@ -225,6 +226,17 @@ public function createLineShape(int $fromX, int $fromY, int $toX, int $toY): Lin
return $shape;
}

/**
* Create geometric shape.
*/
public function createAutoShape(): AutoShape
{
$shape = new AutoShape();
$this->addShape($shape);

return $shape;
}

/**
* Create chart shape.
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/PhpPresentation/Tests/Shape/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public function testAdd(): void
{
$object = new Group();

self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\AutoShape', $object->createAutoShape());
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart', $object->createChartShape());
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Drawing\\File', $object->createDrawingShape());
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Line', $object->createLineShape(10, 10, 10, 10));
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->createRichTextShape());
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table', $object->createTableShape());
self::assertCount(5, $object->getShapeCollection());
self::assertCount(6, $object->getShapeCollection());
}

public function testExtentX(): void
Expand Down
45 changes: 31 additions & 14 deletions tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

namespace PhpOffice\PhpPresentation\Tests\Slide;

use PhpOffice\PhpPresentation\Shape\AutoShape;
use PhpOffice\PhpPresentation\Shape\Chart;
use PhpOffice\PhpPresentation\Shape\Drawing;
use PhpOffice\PhpPresentation\Shape\Line;
use PhpOffice\PhpPresentation\Shape\RichText;
use PhpOffice\PhpPresentation\Shape\Table;
use PhpOffice\PhpPresentation\Slide\AbstractSlide;
Expand All @@ -35,51 +38,65 @@ class AbstractSlideTest extends TestCase
{
public function testCollection(): void
{
/** @var AbstractSlide $stub */
$stub = $this->getMockForAbstractClass(AbstractSlide::class);
/** @var AbstractSlide $object */
$object = $this->getMockForAbstractClass(AbstractSlide::class);

$array = [];
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));
self::assertIsArray($stub->getShapeCollection());
self::assertCount(count($array), $stub->getShapeCollection());
self::assertInstanceOf(AbstractSlide::class, $object->setShapeCollection($array));
self::assertIsArray($object->getShapeCollection());
self::assertCount(count($array), $object->getShapeCollection());

$array = [
new RichText(),
new RichText(),
new RichText(),
];
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));
self::assertIsArray($stub->getShapeCollection());
self::assertCount(count($array), $stub->getShapeCollection());
self::assertInstanceOf(AbstractSlide::class, $object->setShapeCollection($array));
self::assertIsArray($object->getShapeCollection());
self::assertCount(count($array), $object->getShapeCollection());
}

public function testAdd(): void
{
/** @var AbstractSlide $object */
$object = $this->getMockForAbstractClass(AbstractSlide::class);

self::assertInstanceOf(AutoShape::class_parents, $object->createAutoShape());

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.

Check failure on line 64 in tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

Access to undefined constant PhpOffice\PhpPresentation\Shape\AutoShape::class_parents.
self::assertInstanceOf(Chart::class, $object->createChartShape());
self::assertInstanceOf(Drawing\File::class, $object->createDrawingShape());
self::assertInstanceOf(Line::class, $object->createLineShape(10, 10, 10, 10));
self::assertInstanceOf(RichText::class, $object->createRichTextShape());
self::assertInstanceOf(Table::class, $object->createTableShape());
self::assertCount(6, $object->getShapeCollection());
}

public function testSearchShapes(): void
{
/** @var AbstractSlide $stub */
$stub = $this->getMockForAbstractClass(AbstractSlide::class);
/** @var AbstractSlide $object */
$object = $this->getMockForAbstractClass(AbstractSlide::class);

$array = [
(new RichText())->setName('AAA'),
(new Table())->setName('BBB'),
(new Chart())->setName('AAA'),
];
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));
self::assertInstanceOf(AbstractSlide::class, $object->setShapeCollection($array));

// Search by Name
$result = $stub->searchShapes('AAA', null);
$result = $object->searchShapes('AAA', null);
self::assertIsArray($result);
self::assertCount(2, $result);
self::assertInstanceOf(RichText::class, $result[0]);
self::assertInstanceOf(Chart::class, $result[1]);

// Search by Name && Type
$result = $stub->searchShapes('AAA', Chart::class);
$result = $object->searchShapes('AAA', Chart::class);
self::assertIsArray($result);
self::assertCount(1, $result);
self::assertInstanceOf(Chart::class, $result[0]);

// Search by Type
$result = $stub->searchShapes(null, Table::class);
$result = $object->searchShapes(null, Table::class);
self::assertIsArray($result);
self::assertCount(1, $result);
self::assertInstanceOf(Table::class, $result[0]);
Expand Down

0 comments on commit ea93563

Please sign in to comment.