Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdnbrk committed Oct 8, 2024
1 parent 082d631 commit f1d9600
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions app/View/Components/NinjaSvgComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class NinjaSvgComponent extends Component implements Htmlable

private Collection $cssRules;

private Collection $placeholders;

public function __construct(
public string $inscriptionId,
public array $config,
Expand All @@ -30,12 +32,14 @@ public function __construct(

$this->removeNewlinesFromSvgPaths();

$this->parseCssRulesToArray();
$this->parseCssRulesToCollection();

$this->parseConfigCssRulesToArray();
$this->parsePlaceholdersToCollection();

$this->addCssStringsToInnerSvg();

$this->replacePlaceholders();

$this->removeClassAttributesFromInnerSvg();
}

Expand Down Expand Up @@ -74,20 +78,9 @@ protected function parseCssRulesToAttributeString(): Collection
{
return $this->cssRules->map(function ($attributes) {
return collect($attributes)->map(function ($value, $key) {
if (preg_match('/%%ST\d+%%/', $value)) {
return null;
}

return "$key=\"$value\"";
})
->implode(' ');
})
->mapWithKeys(function ($attributeString, $class) {
return [
ltrim($class, '.') => $attributeString,
];
})
->filter();
})->implode(' ');
});
}

protected function addCssStringsToInnerSvg(): void
Expand All @@ -105,7 +98,7 @@ protected function addCssStringsToInnerSvg(): void
});
}

protected function parseCssRulesToArray(): void
protected function parseCssRulesToCollection(): void
{
$cssContent = Str::of($this->styleElement())
->between('<style type="text/css">', '</style>')
Expand All @@ -121,7 +114,7 @@ protected function parseCssRulesToArray(): void

$ruleParts = $rule->split('/\\{/', 2);

$selector = $ruleParts[0];
$selector = ltrim($ruleParts[0], '.');
$properties = $ruleParts[1];

$properties = Str::of($properties)->split('/\s*;\s*/')
Expand Down Expand Up @@ -149,27 +142,14 @@ protected function parseCssRulesToArray(): void
->collapse();
}

protected function parseConfigCssRulesToArray(): void
protected function parsePlaceholdersToCollection(): void
{
Collection::make($this->config)
$this->placeholders = Collection::make($this->config)
->filter(function ($value, $key) {
return preg_match('/^ST\d+$/', $key);
})
->mapWithKeys(function ($value, $key) {
$key = Str::of($key)
->lower()
->prepend('.')
->toString();

return [
$key => $value,
];
})
->each(function ($hexColor, $class) {
$this->cssRules->put(
$class,
['fill' => $hexColor],
);
return ['%%'.$key.'%%' => $value];
});
}

Expand Down Expand Up @@ -204,6 +184,17 @@ protected function removeClassAttributesFromInnerSvg(): void
);
}

protected function replacePlaceholders(): void
{
$this->placeholders->each(function ($value, $placeholder) {
$this->innerSvgContent = Str::replaceMatches(
pattern: '/'.preg_quote($placeholder, '/').'/',
replace: fn () => $value,
subject: $this->innerSvgContent
);
});
}

protected function readContentsFromDisk(): void
{
$this->fileContent = Storage::disk('ninja_components')
Expand Down

0 comments on commit f1d9600

Please sign in to comment.