Skip to content

Commit

Permalink
Update private const MINIMUM_WORDS_COUNT
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdullahKaram-Dev committed Dec 26, 2023
1 parent 2fc9b3b commit c357a74
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/Services/LaravelSvg.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ class LaravelSvg

private const DEFAULT_SVG_TYPE = 'image/svg+xml';

/**
* @param array $settings
* @param string $firstWord
* @param string $lastWord
* @param bool $withLogoText
* @param string $svgTemplate
* @param int $wordsCount
*/
public function __construct(
protected array $settings = [],
protected string $firstWord = '',
Expand All @@ -26,6 +34,10 @@ public function __construct(
$this->settings = config('laravel-svg');
}

/**
* @param $key
* @return string
*/
protected function getSetting($key): string
{
if (isset($this->settings[$key]) && !empty($this->settings[$key])) {
Expand All @@ -35,9 +47,14 @@ protected function getSetting($key): string
}


/**
* @param string $words
* @return $this
*/
public function svgFor(string $words): self
{
$this->setCountWords($words);

if ($this->wordsCount < self::MINIMUM_WORDS_COUNT) {
throw new \InvalidArgumentException('Invalid words count passed to svgFor method');
}
Expand All @@ -49,6 +66,10 @@ public function svgFor(string $words): self
}


/**
* @param string $words
* @return void
*/
private function setCountWords(string $words): void
{
if ($this->isArabicWords($words)) {
Expand All @@ -61,12 +82,20 @@ private function setCountWords(string $words): void
}
}

/**
* @param string $words
* @return bool
*/
private function isArabicWords(string $words): bool
{
$arabic = preg_match('/\p{Arabic}/u', $words);
return $arabic;
}

/**
* @param string|null $logoText
* @return $this
*/
public function logoText(string $logoText = null): self
{
$this->withLogoText = true;
Expand All @@ -79,12 +108,18 @@ public function logoText(string $logoText = null): self
}


/**
* @return array
*/
public function generate(): array
{
$this->buildSvg();
return $this->saveSvg();
}

/**
* @return array
*/
protected function saveSvg(): array
{
$this->checkDisk();
Expand All @@ -105,13 +140,19 @@ protected function saveSvg(): array
}


/**
* @return void
*/
protected function checkDisk(): void
{
if (!File::exists(public_path($this->getSetting('folder').'/'.$this->getSetting('default_svg_path')))) {
File::makeDirectory(public_path($this->getSetting('folder').'/'.$this->getSetting('default_svg_path')));
}
}

/**
* @return void
*/
protected function buildSvg(): void
{
$this->svgTemplate .= '<svg width="100%" height="100%" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">';
Expand All @@ -122,23 +163,35 @@ protected function buildSvg(): void
$this->replaceSvgTemplate();
}

/**
* @return void
*/
protected function buildSvgLogoText(): void
{
if ($this->withLogoText) {
$this->svgTemplate .= '<text x="25" y="190" font-size="25" fill="{logo_text_color}">{logo_text}</text>';
}
}

/**
* @return void
*/
protected function buildSvgText(): void
{
$this->svgTemplate .= '<text x="50%" y="50%" word-spacing="-20" text-anchor="middle" stroke="{avtar_text_color}" stroke-width="1px" dy=".3em" font-size="90">{firstChar} {secondChar}</text>';
}

/**
* @return void
*/
protected function buildBackground(): void
{
$this->svgTemplate .= '<rect width="100%" height="100%" rx="50" ry="50" fill="{avatar_background_color}" /><rect x="50" y="50" width="100" height="100" rx="50" ry="50" fill="{avtar_text_color}" />';
}

/**
* @return void
*/
private function replaceSvgTemplate(): void
{
$this->svgTemplate = str_replace([
Expand Down

0 comments on commit c357a74

Please sign in to comment.