Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codename to BlockInfo and CodepointInfo #39

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, [
', ' => '__',
' -' => '__',
' ' => '_',
'-' => '_',
]);
}
}