Skip to content

Commit 6d11e3e

Browse files
mhasanshahidProgi1984
authored andcommitted
createAutoShape : Add method to create geometric shapes
1 parent d16456d commit 6d11e3e

File tree

6 files changed

+65
-15
lines changed

6 files changed

+65
-15
lines changed

docs/changes/1.2.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- 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)
99
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#839](https://github.com/PHPOffice/PHPPresentation/pull/839)
1010
- `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)
11+
- `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)
1112

1213
## Bug fixes
1314

docs/usage/shapes/autoshape.md

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ $shape = new AutoShape();
1414
$slide->addShape($shape)
1515
```
1616

17+
Or we can use `createAutoShape` method of slide.
18+
19+
Example:
20+
21+
```php
22+
$slide->createAutoShape();
23+
```
24+
1725
## Text
1826

1927
You can define text of the geometric form with `setText` method.

src/PhpPresentation/Shape/Group.php

+11
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ public function createLineShape(int $fromX, int $fromY, int $toX, int $toY): Lin
194194
return $shape;
195195
}
196196

197+
/**
198+
* Create geometric shape.
199+
*/
200+
public function createAutoShape(): AutoShape
201+
{
202+
$shape = new AutoShape();
203+
$this->addShape($shape);
204+
205+
return $shape;
206+
}
207+
197208
/**
198209
* Create chart shape.
199210
*/

src/PhpPresentation/Slide/AbstractSlide.php

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PhpOffice\PhpPresentation\ComparableInterface;
2424
use PhpOffice\PhpPresentation\GeometryCalculator;
2525
use PhpOffice\PhpPresentation\PhpPresentation;
26+
use PhpOffice\PhpPresentation\Shape\AutoShape;
2627
use PhpOffice\PhpPresentation\Shape\Chart;
2728
use PhpOffice\PhpPresentation\Shape\Drawing\File;
2829
use PhpOffice\PhpPresentation\Shape\Group;
@@ -225,6 +226,17 @@ public function createLineShape(int $fromX, int $fromY, int $toX, int $toY): Lin
225226
return $shape;
226227
}
227228

229+
/**
230+
* Create geometric shape.
231+
*/
232+
public function createAutoShape(): AutoShape
233+
{
234+
$shape = new AutoShape();
235+
$this->addShape($shape);
236+
237+
return $shape;
238+
}
239+
228240
/**
229241
* Create chart shape.
230242
*/

tests/PhpPresentation/Tests/Shape/GroupTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ public function testAdd(): void
4848
{
4949
$object = new Group();
5050

51+
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\AutoShape', $object->createAutoShape());
5152
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart', $object->createChartShape());
5253
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Drawing\\File', $object->createDrawingShape());
5354
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Line', $object->createLineShape(10, 10, 10, 10));
5455
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->createRichTextShape());
5556
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table', $object->createTableShape());
56-
self::assertCount(5, $object->getShapeCollection());
57+
self::assertCount(6, $object->getShapeCollection());
5758
}
5859

5960
public function testExtentX(): void

tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php

+31-14
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
namespace PhpOffice\PhpPresentation\Tests\Slide;
2222

23+
use PhpOffice\PhpPresentation\Shape\AutoShape;
2324
use PhpOffice\PhpPresentation\Shape\Chart;
25+
use PhpOffice\PhpPresentation\Shape\Drawing;
26+
use PhpOffice\PhpPresentation\Shape\Line;
2427
use PhpOffice\PhpPresentation\Shape\RichText;
2528
use PhpOffice\PhpPresentation\Shape\Table;
2629
use PhpOffice\PhpPresentation\Slide\AbstractSlide;
@@ -35,51 +38,65 @@ class AbstractSlideTest extends TestCase
3538
{
3639
public function testCollection(): void
3740
{
38-
/** @var AbstractSlide $stub */
39-
$stub = $this->getMockForAbstractClass(AbstractSlide::class);
41+
/** @var AbstractSlide $object */
42+
$object = $this->getMockForAbstractClass(AbstractSlide::class);
4043

4144
$array = [];
42-
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));
43-
self::assertIsArray($stub->getShapeCollection());
44-
self::assertCount(count($array), $stub->getShapeCollection());
45+
self::assertInstanceOf(AbstractSlide::class, $object->setShapeCollection($array));
46+
self::assertIsArray($object->getShapeCollection());
47+
self::assertCount(count($array), $object->getShapeCollection());
4548

4649
$array = [
4750
new RichText(),
4851
new RichText(),
4952
new RichText(),
5053
];
51-
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));
52-
self::assertIsArray($stub->getShapeCollection());
53-
self::assertCount(count($array), $stub->getShapeCollection());
54+
self::assertInstanceOf(AbstractSlide::class, $object->setShapeCollection($array));
55+
self::assertIsArray($object->getShapeCollection());
56+
self::assertCount(count($array), $object->getShapeCollection());
57+
}
58+
59+
public function testAdd(): void
60+
{
61+
/** @var AbstractSlide $object */
62+
$object = $this->getMockForAbstractClass(AbstractSlide::class);
63+
64+
self::assertInstanceOf(AutoShape::class, $object->createAutoShape());
65+
self::assertInstanceOf(Chart::class, $object->createChartShape());
66+
self::assertInstanceOf(Drawing\File::class, $object->createDrawingShape());
67+
self::assertInstanceOf(Line::class, $object->createLineShape(10, 10, 10, 10));
68+
self::assertInstanceOf(RichText::class, $object->createRichTextShape());
69+
self::assertInstanceOf(Table::class, $object->createTableShape());
70+
self::assertCount(6, $object->getShapeCollection());
5471
}
5572

5673
public function testSearchShapes(): void
5774
{
58-
/** @var AbstractSlide $stub */
59-
$stub = $this->getMockForAbstractClass(AbstractSlide::class);
75+
/** @var AbstractSlide $object */
76+
$object = $this->getMockForAbstractClass(AbstractSlide::class);
6077

6178
$array = [
6279
(new RichText())->setName('AAA'),
6380
(new Table())->setName('BBB'),
6481
(new Chart())->setName('AAA'),
6582
];
66-
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));
83+
self::assertInstanceOf(AbstractSlide::class, $object->setShapeCollection($array));
6784

6885
// Search by Name
69-
$result = $stub->searchShapes('AAA', null);
86+
$result = $object->searchShapes('AAA', null);
7087
self::assertIsArray($result);
7188
self::assertCount(2, $result);
7289
self::assertInstanceOf(RichText::class, $result[0]);
7390
self::assertInstanceOf(Chart::class, $result[1]);
7491

7592
// Search by Name && Type
76-
$result = $stub->searchShapes('AAA', Chart::class);
93+
$result = $object->searchShapes('AAA', Chart::class);
7794
self::assertIsArray($result);
7895
self::assertCount(1, $result);
7996
self::assertInstanceOf(Chart::class, $result[0]);
8097

8198
// Search by Type
82-
$result = $stub->searchShapes(null, Table::class);
99+
$result = $object->searchShapes(null, Table::class);
83100
self::assertIsArray($result);
84101
self::assertCount(1, $result);
85102
self::assertInstanceOf(Table::class, $result[0]);

0 commit comments

Comments
 (0)