]> git.mxchange.org Git - friendica.git/commitdiff
Create AddonInfo class
authorArt4 <art4@wlabs.de>
Tue, 4 Feb 2025 09:41:52 +0000 (09:41 +0000)
committerArt4 <art4@wlabs.de>
Tue, 4 Feb 2025 09:41:52 +0000 (09:41 +0000)
src/Core/Addon/AddonHelper.php
src/Core/Addon/AddonInfo.php [new file with mode: 0644]
src/Core/Addon/AddonProxy.php
tests/Unit/Core/Addon/AddonInfoTest.php [new file with mode: 0644]

index 1dd8dc93f52e6e502dcbad4903525f32ca9820dc..63f32a3ae7af6231cdfc72cb4959bd01868e3928 100644 (file)
@@ -44,6 +44,11 @@ interface AddonHelper
         */
        public function reloadAddons(): void;
 
+       /**
+        * Get the comment block of an addon as value object.
+        */
+       public function getAddonInfo(string $addonId): AddonInfo;
+
        /**
         * Checks if the provided addon is enabled
         */
diff --git a/src/Core/Addon/AddonInfo.php b/src/Core/Addon/AddonInfo.php
new file mode 100644 (file)
index 0000000..38f7dd6
--- /dev/null
@@ -0,0 +1,130 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+declare(strict_types=1);
+
+namespace Friendica\Core\Addon;
+
+/**
+ * Information about an addon
+ */
+final class AddonInfo
+{
+       /**
+        * @internal Never create this object by yourself, use `Friendica\Core\Addon\AddonHelper::getAddonInfo()` instead.
+        *
+        * @see Friendica\Core\Addon\AddonHelper::getAddonInfo()
+        */
+       public static function fromArray(array $info): self
+       {
+               $id          = array_key_exists('id', $info) ? (string) $info['id'] : '';
+               $name        = array_key_exists('name', $info) ? (string) $info['name'] : '';
+               $description = array_key_exists('description', $info) ? (string) $info['description'] : '';
+               $author      = array_key_exists('author', $info) ? self::parseContributor($info['author']) : [];
+               $maintainer  = array_key_exists('maintainer', $info) ? self::parseContributor($info['maintainer']) : [];
+               $version     = array_key_exists('version', $info) ? (string) $info['version'] : '';
+               $status      = array_key_exists('status', $info) ? (string) $info['status'] : '';
+
+               return new self(
+                       $id,
+                       $name,
+                       $description,
+                       $author,
+                       $maintainer,
+                       $version,
+                       $status
+               );
+       }
+
+       private static function parseContributor($entry): array
+       {
+               if (!is_array($entry)) {
+                       return [];
+               }
+
+               if (!array_key_exists('name', $entry)) {
+                       return [];
+               }
+
+               $contributor = [
+                       'name' => (string) $entry['name'],
+               ];
+
+               if (array_key_exists('link', $entry)) {
+                       $contributor['link'] = (string) $entry['link'];
+               }
+
+               return $contributor;
+       }
+
+       private string $id = '';
+
+       private string $name = '';
+
+       private string $description = '';
+
+       private array $author = [];
+
+       private array $maintainer = [];
+
+       private string $version = '';
+
+       private string $status = '';
+
+       private function __construct(
+               string $id,
+               string $name,
+               string $description,
+               array $author,
+               array $maintainer,
+               string $version,
+               string $status,
+       ) {
+               $this->id = $id;
+               $this->name = $name;
+               $this->description = $description;
+               $this->author = $author;
+               $this->maintainer = $maintainer;
+               $this->version = $version;
+               $this->status = $status;
+       }
+
+       public function getId(): string
+       {
+               return $this->id;
+       }
+
+       public function getName(): string
+       {
+               return $this->name;
+       }
+
+       public function getDescription(): string
+       {
+               return $this->description;
+       }
+
+       public function getAuthor(): array
+       {
+               return $this->author;
+       }
+
+       public function getMaintainer(): array
+       {
+               return $this->maintainer;
+       }
+
+       public function getVersion(): string
+       {
+               return $this->version;
+       }
+
+       public function getStatus(): string
+       {
+               return $this->status;
+       }
+}
index dead7cbb3edbd8cf216998b55be2d23a0f725cb6..ded52e8c293567266c96bac4a3dc9eb0cf1e5139 100644 (file)
@@ -60,6 +60,19 @@ final class AddonProxy implements AddonHelper
                Addon::reload();
        }
 
+       /**
+        * Get the comment block of an addon as value object.
+        */
+       public function getAddonInfo(string $addonId): AddonInfo
+       {
+               $data = Addon::getInfo($addonId);
+
+               // add addon ID
+               $data['id'] = $addonId;
+
+               return AddonInfo::fromArray($data);
+       }
+
        /**
         * Checks if the provided addon is enabled
         */
diff --git a/tests/Unit/Core/Addon/AddonInfoTest.php b/tests/Unit/Core/Addon/AddonInfoTest.php
new file mode 100644 (file)
index 0000000..12dc474
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+declare(strict_types=1);
+
+namespace Friendica\Test\Unit\Core;
+
+use Friendica\Core\Addon\AddonInfo;
+use PHPUnit\Framework\TestCase;
+
+class AddonInfoTest extends TestCase
+{
+       public function testFromArrayCreatesObject(): void
+       {
+               $data = [
+                       'id'          => '',
+                       'name'        => '',
+                       'description' => '',
+                       'author'      => [],
+                       'maintainer'  => [],
+                       'version'     => '',
+                       'status'      => '',
+               ];
+
+               $this->assertInstanceOf(AddonInfo::class, AddonInfo::fromArray($data));
+       }
+
+       public function testGetterReturningCorrectValues(): void
+       {
+               $data = [
+                       'id'          => 'test',
+                       'name'        => 'Test-Addon',
+                       'description' => 'This is an addon for tests',
+                       'author'      => ['name' => 'Sam'],
+                       'maintainer'  => ['name' => 'Sam', 'link' => 'https://example.com'],
+                       'version'     => '0.1',
+                       'status'      => 'In Development',
+               ];
+
+               $info = AddonInfo::fromArray($data);
+
+               $this->assertSame($data['id'], $info->getId());
+               $this->assertSame($data['name'], $info->getName());
+               $this->assertSame($data['description'], $info->getDescription());
+               $this->assertSame($data['description'], $info->getDescription());
+               $this->assertSame($data['author'], $info->getAuthor());
+               $this->assertSame($data['maintainer'], $info->getMaintainer());
+               $this->assertSame($data['version'], $info->getVersion());
+               $this->assertSame($data['status'], $info->getStatus());
+       }
+}