Add DDD classes for Mastodon\Field entity
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 23 Jan 2020 14:04:00 +0000 (09:04 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Thu, 30 Jan 2020 04:26:07 +0000 (23:26 -0500)
src/Collection/Api/Mastodon/Fields.php [new file with mode: 0644]
src/DI.php
src/Factory/Api/Mastodon/Field.php [new file with mode: 0644]
src/Object/Api/Mastodon/Field.php

diff --git a/src/Collection/Api/Mastodon/Fields.php b/src/Collection/Api/Mastodon/Fields.php
new file mode 100644 (file)
index 0000000..2bc549d
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+
+namespace Friendica\Collection\Api\Mastodon;
+
+use Friendica\Api\Entity\Mastodon\Field;
+use Friendica\BaseCollection;
+
+class Fields extends BaseCollection
+{
+       /**
+        * @param Field[]  $entities
+        * @param int|null $totalCount
+        */
+       public function __construct(array $entities = [], int $totalCount = null)
+       {
+               parent::__construct($entities);
+
+               $this->totalCount = $totalCount ?? count($entities);
+       }
+}
index 65c8222..83351d3 100644 (file)
@@ -236,6 +236,14 @@ abstract class DI
                return self::$dice->create(Factory\Api\Mastodon\Emoji::class);
        }
 
+       /**
+        * @return Factory\Api\Mastodon\Field
+        */
+       public static function mstdnField()
+       {
+               return self::$dice->create(Factory\Api\Mastodon\Field::class);
+       }
+
        /**
         * @return Factory\Api\Mastodon\FollowRequest
         */
diff --git a/src/Factory/Api/Mastodon/Field.php b/src/Factory/Api/Mastodon/Field.php
new file mode 100644 (file)
index 0000000..10bb269
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+namespace Friendica\Factory\Api\Mastodon;
+
+use Friendica\BaseFactory;
+use Friendica\Collection\Api\Mastodon\Fields;
+use Friendica\Collection\ProfileFields;
+use Friendica\Content\Text\BBCode;
+use Friendica\Model\ProfileField;
+use Friendica\Network\HTTPException;
+
+class Field extends BaseFactory
+{
+       /**
+        * @param ProfileField $profileField
+        * @return \Friendica\Api\Entity\Mastodon\Field
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public function createFromProfileField(ProfileField $profileField)
+       {
+               return new \Friendica\Api\Entity\Mastodon\Field($profileField->label, BBCode::convert($profileField->value, false, 9));
+       }
+
+       /**
+        * @param ProfileFields $profileFields
+        * @return Fields
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public function createFromProfileFields(ProfileFields $profileFields)
+       {
+               $fields = [];
+
+               foreach ($profileFields as $profileField) {
+                       $fields[] = $this->createFromProfileField($profileField);
+               }
+
+               return new Fields($fields);
+       }
+}
index 07cd2e5..a32eae0 100644 (file)
@@ -7,7 +7,7 @@ use Friendica\BaseEntity;
 /**
  * Class Field
  *
- * @see https://docs.joinmastodon.org/api/entities/#field
+ * @see https://docs.joinmastodon.org/entities/field/
  */
 class Field extends BaseEntity
 {
@@ -17,4 +17,12 @@ class Field extends BaseEntity
        protected $value;
        /** @var string (Datetime)*/
        protected $verified_at;
+
+       public function __construct(string $name, string $value)
+       {
+               $this->name = $name;
+               $this->value = $value;
+               // Link verification unsupported
+               $this->verified_at = null;
+       }
 }