Skip to content

Commit 5f41933

Browse files
committed
[TASK] Add trait for getArrayRepresentation to populate class
Part of #1440.
1 parent 6604f35 commit 5f41933

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS;
6+
7+
/**
8+
* Provides a reusable implementation of `getArrayRepresentation()` which will populate the `class` element with the
9+
* short name of the actual class.
10+
* It is expected that this will be `use`d by (possibly-abstract) base classes, with extended classes calling on to the
11+
* parent implementation then further populating the array.
12+
* If a base class using this trait needs to further populate the array, the trait method can be `use`d `as`.
13+
*
14+
* @internal
15+
*
16+
* @phpstan-require-implements Renderable
17+
*/
18+
trait ArrayRepresentationClassProvider
19+
{
20+
/**
21+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
22+
*
23+
* @internal
24+
*/
25+
public function getArrayRepresentation(): array
26+
{
27+
$reflect = new \ReflectionClass($this);
28+
29+
return ['class' => $reflect->getShortName()];
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\Position;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\Tests\Unit\Fixtures\ConcreteArrayRepresentationClassProvider;
9+
10+
/**
11+
* @covers \Sabberworm\CSS\ArrayRepresentationClassProvider
12+
*/
13+
final class ArrayRepresentationClassProviderTest extends TestCase
14+
{
15+
/**
16+
* @test
17+
*/
18+
public function getArrayRepresentationIncludesClassName(): void
19+
{
20+
$subject = new ConcreteArrayRepresentationClassProvider();
21+
22+
$result = $subject->getArrayRepresentation();
23+
24+
self::assertArrayHasKey('class', $result);
25+
self::assertSame('ConcreteArrayRepresentationClassProvider', $result['class']);
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\Fixtures;
6+
7+
use Sabberworm\CSS\ArrayRepresentationClassProvider;
8+
use Sabberworm\CSS\OutputFormat;
9+
use Sabberworm\CSS\Renderable;
10+
11+
final class ConcreteArrayRepresentationClassProvider implements Renderable
12+
{
13+
use ArrayRepresentationClassProvider;
14+
15+
/**
16+
* @return never
17+
*/
18+
public function render(OutputFormat $outputFormat): string
19+
{
20+
throw new \BadMethodCallException('Nothing to see here :/', 1767308395);
21+
}
22+
}

0 commit comments

Comments
 (0)