Skip to content

Commit

Permalink
Add codename to BlockInfo and CodepointInfo (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati authored Nov 7, 2023
1 parent 4232cbb commit 4c4897f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
6 changes: 2 additions & 4 deletions build/BlocksBuilder/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MLUnipoints\Build\BlocksBuilder;

use MLUnipoints\Info\BlockInfo;
use MLUnipoints\Plane;

class Block
Expand All @@ -16,9 +17,6 @@ public function __construct(
public readonly string $name,
public readonly Plane $plane,
) {
$this->codename = strtr($this->name, [
' ' => '_',
'-' => '',
]);
$this->codename = BlockInfo::buildCodename($this->name);
}
}
8 changes: 2 additions & 6 deletions build/CodepointsBuilder/Codepoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use MLUnipoints\Build\BlocksBuilder\Block;
use MLUnipoints\Category;
use MLUnipoints\Info\CodepointInfo;

class Codepoint
{
Expand All @@ -18,11 +19,6 @@ public function __construct(
public readonly Category $category,
public readonly Block $block,
) {
$this->codename = strtr($this->name, [
', ' => '__',
' -' => '__',
' ' => '_',
'-' => '_',
]);
$this->codename = CodepointInfo::buildCodename($this->name);
}
}
17 changes: 16 additions & 1 deletion src/Info/BlockInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,32 @@
#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)]
class BlockInfo
{
public readonly string $codename;

public function __construct(
public readonly int $fromCodepoint,
public readonly int $toCodepoint,
public readonly string $name,
public readonly Plane $plane,
) {}
) {
$this->codename = self::buildCodename($this->name);
}

public static function from(Block $block): self
{
$reflection = new ReflectionEnumUnitCase(Block::class, $block->name);

return $reflection->getAttributes(self::class)[0]->newInstance();
}

/**
* @internal
*/
public static function buildCodename(string $name): string
{
return strtr($name, [
' ' => '_',
'-' => '',
]);
}
}
19 changes: 18 additions & 1 deletion src/Info/CodepointInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)]
class CodepointInfo
{
public readonly string $codename;

public function __construct(
public readonly int $id,
public readonly string $name,
Expand Down Expand Up @@ -63,12 +65,27 @@ public function __construct(
* @var string[]
*/
public readonly array $abbreviations = [],
) {}
) {
$this->codename = self::buildCodename($this->name);
}

public static function from(BackedEnum $codepoint): self
{
$reflection = new ReflectionEnumBackedCase($codepoint, $codepoint->name);

return $reflection->getAttributes(self::class)[0]->newInstance();
}

/**
* @internal
*/
public static function buildCodename(string $name): string
{
return strtr($name, [
', ' => '__',
' -' => '__',
' ' => '_',
'-' => '_',
]);
}
}

0 comments on commit 4c4897f

Please sign in to comment.