Skip to content

Commit

Permalink
add exists method, deprecate hasSVG and getSVG. Closes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
joemaller committed Aug 5, 2024
1 parent 320e2a6 commit 9c4366c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
21 changes: 21 additions & 0 deletions src/Deprecated/GetSVG.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace IdeasOnPurpose\WP\Deprecated;

trait GetSVG
{
/**
* Alias for $this->fetch()
* TODO: Pick a name
* @param mixed $key
* @param mixed $attributes
* @deprecated
* @return void
*/
public function getSVG($key, $attributes = [])
{
return $this->fetch($key, $attributes);
}

abstract public function fetch($key, $attributes = []);
}
17 changes: 17 additions & 0 deletions src/Deprecated/HasSVG.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace IdeasOnPurpose\WP\Deprecated;

trait HasSVG
{
/**
* Check if SVG exists
* @deprecated Renamed to `exists`
*/
public function hasSVG($name)
{
return $this->exists($name);
}

abstract public function exists($name);
}
28 changes: 9 additions & 19 deletions src/SVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SVG
use Deprecated\Directory;
use Deprecated\DumpSymbols;
use Deprecated\Get;
use Deprecated\GetSVG;
use Deprecated\HasSVG;

public $lib = [];

Expand Down Expand Up @@ -366,10 +368,11 @@ public function normalizeKey($key)
}

/**
* Check if SVG exists
* TODO: Naming?
* Check whether an SVG has been registered to the library
* @return boolean True if the SVG exists
*/
public function hasSVG($name)

public function exists($name)
{
$key = $this->normalizeKey($name);

Expand All @@ -385,7 +388,7 @@ public function __get($name)
}

/**
* TODO: Alternate name, getSVG(). Pi
* TODO: Alternate name, getSVG().
* @param string $key
* @param array $attributes
* @return object | WP_Error
Expand All @@ -394,11 +397,10 @@ public function fetch($key, $attributes = [])
{
$name = $this->normalizeKey($key);

if ($name && $this->hasSVG($name)) {
if ($name && $this->exists($name)) {
$svg = $this->lib[$name];
$svg = $this->removePrivateKeys($svg);

// $svg->name = $name;
$atts = $this->validateAttributes($attributes);
$svg = $this->rewrapSvg($svg, $atts);
return $svg;
Expand All @@ -409,18 +411,6 @@ public function fetch($key, $attributes = [])
// When to suppress errors? When to show? REST should always show errors
}

/**
* Alias for $this->fetch()
* TODO: Pick a name
* @param mixed $key
* @param mixed $attributes
* @return void
*/
public function getSVG($key, $attributes = [])
{
return $this->fetch($key, $attributes);
}

/**
* Inline SVGs directly by name
* '.svg' extensions are stripped, so 'arrow' and 'arrow.svg' will both return the 'arrow.svg' file
Expand Down Expand Up @@ -455,7 +445,7 @@ public function use($key)
{
$name = $this->normalizeKey($key);

if ($this->hasSVG($name)) {
if ($this->exists($name)) {
array_push($this->inUse, $name);
return sprintf(
'<svg class="%1$s"><use xlink:href="#%1$s" href="#%1$s" /></svg>',
Expand Down

0 comments on commit 9c4366c

Please sign in to comment.