Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Oct 14, 2023
1 parent 9cc9b96 commit b35a68d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
27 changes: 21 additions & 6 deletions src/FontList.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,36 @@

final class FontList
{
private array $fonts;
private array $fontList;

/**
* @param list<Font> $fonts
* @param list<Font> $fontList
*/
public function __construct(
array $fonts
private function __construct(
array $fontList
) {
$this->fonts = $fonts;
$this->fontList = $fontList;
}

/**
* @param list<Font> $fontList
*/
public static function of(
array $fontList
): self {
foreach ($fontList as $font) {
if (!($font instanceof Font)) {
throw new \InvalidArgumentException('Cannot instantiate FontList with ');
}
}

return new self($fontList);
}

public function getById(
string $id
): Font {
foreach ($this->fonts as $font) {
foreach ($this->fontList as $font) {
if ($font->id() === $id) {
return $font;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Parser/SimpleXmlSvgFontFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ public function parseFile(string $filePath): FontList
$xml = simplexml_load_file($filePath);
$xml->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');

$fonts = [];
$fontLists = [];

$fontElements = $xml->xpath('//svg:defs/svg:font');

foreach ($fontElements as $fontElement) {
$fonts[] = $this->initFont($fontElement);
$fontLists[] = $this->initFont($fontElement);
}

return new FontList($fonts);
return FontList::of(
$fontLists,
);
}

private function initFont(
Expand Down
8 changes: 4 additions & 4 deletions src/Parser/XmlReaderSvgFontFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function parseFile(
$xml = new \XMLReader();
$xml->open($filePath);

$fonts = [];
$fontList = [];

while ($xml->read()) {
if ($xml->name === self::ELEMENT_NAME_FONT) {
Expand All @@ -54,7 +54,7 @@ public function parseFile(
}

if ($xml->nodeType === \XMLReader::END_ELEMENT) {
$fonts[] = new Font(
$fontList[] = new Font(
$fontId,
$defaultHorizontalAdvance,
$fontFace,
Expand Down Expand Up @@ -93,8 +93,8 @@ public function parseFile(

$xml->close();

return new FontList(
$fonts,
return FontList::of(
$fontList,
);
}

Expand Down

0 comments on commit b35a68d

Please sign in to comment.