Browse Source

Fix up icon name usage for title.

mscherer 3 years ago
parent
commit
0fcabd8cda

+ 7 - 0
docs/Helper/Icon.md

@@ -160,3 +160,10 @@ class YourIcon extends AbstractIcon {
 }
 ```
 Now you can hook it into your config and enjoy!
+
+## TODO
+TBD:
+- `@icon/icofont` ( https://icofont.com/ )
+- https://fontello.com/
+
+Help welcome!

+ 3 - 0
src/View/Icon/Collector/BootstrapIconCollector.php

@@ -4,6 +4,9 @@ namespace Tools\View\Icon\Collector;
 
 use RuntimeException;
 
+/**
+ * Using e.g. "bootstrap-icons" npm package.
+ */
 class BootstrapIconCollector {
 
 	/**

+ 3 - 0
src/View/Icon/Collector/FeatherIconCollector.php

@@ -4,6 +4,9 @@ namespace Tools\View\Icon\Collector;
 
 use RuntimeException;
 
+/**
+ * Using e.g. "feather-icons" npm package.
+ */
 class FeatherIconCollector {
 
 	/**

+ 3 - 0
src/View/Icon/Collector/FontAwesome4IconCollector.php

@@ -4,6 +4,9 @@ namespace Tools\View\Icon\Collector;
 
 use RuntimeException;
 
+/**
+ * Using e.g. "font-awesome" npm package.
+ */
 class FontAwesome4IconCollector {
 
 	/**

+ 35 - 8
src/View/Icon/Collector/FontAwesome5IconCollector.php

@@ -4,6 +4,9 @@ namespace Tools\View\Icon\Collector;
 
 use RuntimeException;
 
+/**
+ * Using e.g. "@fortawesome/fontawesome-free" npm package or "font-awesome-v5-icons" npm meta package.
+ */
 class FontAwesome5IconCollector {
 
 	/**
@@ -17,14 +20,38 @@ class FontAwesome5IconCollector {
 			throw new RuntimeException('Cannot read file: ' . $filePath);
 		}
 
-		$array = json_decode($content, true);
-		if (!$array) {
-			throw new RuntimeException('Cannot parse JSON: ' . $filePath);
-		}
-
-		$icons = [];
-		foreach ($array['icons'] as $row) {
-			$icons[] = $row['name'];
+		$ext = pathinfo($filePath, PATHINFO_EXTENSION);
+		switch ($ext) {
+			case 'svg':
+				preg_match_all('/symbol id="([a-z][^"]+)"/', $content, $matches);
+				if (!$matches) {
+					throw new RuntimeException('Cannot parse SVG: ' . $filePath);
+				}
+				$icons = $matches[1];
+
+				break;
+
+			case 'yml':
+				$array = yaml_parse($content);
+				/** @var array<string> $icons */
+				$icons = array_keys($array);
+				break;
+
+			case 'json':
+				$array = json_decode($content, true);
+				if (!$array) {
+					throw new RuntimeException('Cannot parse JSON: ' . $filePath);
+				}
+
+				$icons = [];
+				foreach ($array['icons'] as $row) {
+					$icons[] = $row['name'];
+				}
+
+				break;
+
+			default:
+				throw new RuntimeException('Unknown file extension: ' . $ext);
 		}
 
 		return $icons;

+ 3 - 0
src/View/Icon/Collector/FontAwesome6IconCollector.php

@@ -4,6 +4,9 @@ namespace Tools\View\Icon\Collector;
 
 use RuntimeException;
 
+/**
+ * Using e.g. "fontawesome-free" npm package.
+ */
 class FontAwesome6IconCollector {
 
 	/**

+ 3 - 0
src/View/Icon/Collector/MaterialIconCollector.php

@@ -4,6 +4,9 @@ namespace Tools\View\Icon\Collector;
 
 use RuntimeException;
 
+/**
+ * Using e.g. "material-symbols" npm package.
+ */
 class MaterialIconCollector {
 
 	/**

+ 6 - 4
src/View/Icon/IconCollection.php

@@ -86,7 +86,9 @@ class IconCollection {
 	 * @return string
 	 */
 	public function render(string $icon, array $options = [], array $attributes = []): string {
+		$iconName = null;
 		if (isset($this->_config['map'][$icon])) {
+			$iconName = $icon;
 			$icon = $this->_config['map'][$icon];
 		}
 
@@ -106,11 +108,11 @@ class IconCollection {
 		if (!isset($options['title']) || $options['title'] !== false) {
 			$titleField = !isset($options['title']) || $options['title'] === true ? 'title' : $options['title'];
 			if (!isset($attributes[$titleField])) {
-				$attributes[$titleField] = ucwords(Inflector::humanize(Inflector::underscore($icon)));
+				$attributes[$titleField] = ucwords(Inflector::humanize(Inflector::underscore($iconName ?? $icon)));
+			}
+			if (!isset($options['translate']) || $options['translate'] !== false && isset($attributes[$titleField])) {
+				$attributes[$titleField] = __($attributes[$titleField]);
 			}
-		}
-		if (!isset($options['translate']) || $options['translate'] !== false) {
-			$attributes['title'] = __($attributes['title']);
 		}
 
 		return $this->iconSets[$set]->render($icon, $options, $attributes);

+ 10 - 1
tests/TestCase/View/Helper/IconHelperTest.php

@@ -70,6 +70,15 @@ class IconHelperTest extends TestCase {
 	/**
 	 * @return void
 	 */
+	public function testIconWithCustomTitleField() {
+		$result = $this->Icon->render('m:save', ['title' => 'data-title'], ['class' => 'my-extra']);
+		$expected = '<span class="material-icons my-extra" data-title="Save">save</span>';
+		$this->assertSame($expected, $result);
+	}
+
+	/**
+	 * @return void
+	 */
 	public function testIconWithCustomFontIcon() {
 		$config = [
 			'sets' => [
@@ -84,7 +93,7 @@ class IconHelperTest extends TestCase {
 		$this->Icon = new IconHelper(new View(null), $config);
 
 		$result = $this->Icon->render('edit');
-		$expected = '<span class="material-icons" title="Save">save</span>';
+		$expected = '<span class="material-icons" title="Edit">save</span>';
 		$this->assertSame($expected, $result);
 	}
 

+ 28 - 0
tests/TestCase/View/Icon/Collector/FontAwesome5CollectorTest.php

@@ -21,4 +21,32 @@ class FontAwesome5CollectorTest extends TestCase {
 		$this->assertTrue(in_array('thumbs-up', $result, true));
 	}
 
+	/**
+	 * Show that we are still API compatible/valid.
+	 *
+	 * @return void
+	 */
+	public function testCollectSvg(): void {
+		$path = TEST_FILES . 'font_icon' . DS . 'fa5' . DS . 'solid.svg';
+
+		$result = FontAwesome5IconCollector::collect($path);
+
+		$this->assertTrue(count($result) > 1000, 'count of ' . count($result));
+		$this->assertTrue(in_array('thumbs-up', $result, true));
+	}
+
+	/**
+	 * Show that we are still API compatible/valid.
+	 *
+	 * @return void
+	 */
+	public function testCollectYml(): void {
+		$path = TEST_FILES . 'font_icon' . DS . 'fa5' . DS . 'icons.yml';
+
+		$result = FontAwesome5IconCollector::collect($path);
+
+		$this->assertTrue(count($result) > 1400, 'count of ' . count($result));
+		$this->assertTrue(in_array('thumbs-up', $result, true));
+	}
+
 }

File diff suppressed because it is too large
+ 21780 - 0
tests/test_files/font_icon/fa5/icons.yml


File diff suppressed because it is too large
+ 3013 - 0
tests/test_files/font_icon/fa5/solid.svg