]> git.mxchange.org Git - friendica.git/commitdiff
Use ProfileField::selectPublicFieldsByUserId
authorPhilipp <admin@philipp.info>
Fri, 8 Oct 2021 17:45:20 +0000 (19:45 +0200)
committerPhilipp <admin@philipp.info>
Mon, 18 Oct 2021 21:32:39 +0000 (23:32 +0200)
src/Collection/ProfileFields.php [deleted file]
src/DI.php
src/Factory/Api/Mastodon/Field.php
src/Module/Api/Friendica/Profile/Show.php
src/Profile/ProfileField/Collection/ProfileFields.php [new file with mode: 0644]
src/Profile/ProfileField/Depository/ProfileField.php [new file with mode: 0644]
src/Profile/ProfileField/Entity/ProfileField.php
src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php [new file with mode: 0644]
src/Profile/ProfileField/Factory/ProfileField.php [new file with mode: 0644]
src/Repository/ProfileField.php

diff --git a/src/Collection/ProfileFields.php b/src/Collection/ProfileFields.php
deleted file mode 100644 (file)
index 9e706da..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Collection;
-
-use Friendica\BaseCollection;
-
-class ProfileFields extends BaseCollection
-{
-       /**
-        * @param callable $callback
-        * @return ProfileFields
-        */
-       public function map(callable $callback)
-       {
-               return parent::map($callback);
-       }
-
-       /**
-        * @param callable|null $callback
-        * @param int           $flag
-        * @return ProfileFields
-        */
-       public function filter(callable $callback = null, int $flag = 0)
-       {
-               return parent::filter($callback, $flag);
-       }
-}
index 9025feb470983355f0c19f73ed5102b8726042a1..e64c5cef2deb527613cd7e9fccd0683c7821c5f5 100644 (file)
@@ -460,6 +460,11 @@ abstract class DI
                return self::$dice->create(Repository\ProfileField::class);
        }
 
+       public static function profileFieldNew(): Profile\ProfileField\Depository\ProfileField
+       {
+               return self::$dice->create(Profile\ProfileField\Depository\ProfileField::class);
+       }
+
        public static function notification(): Navigation\Notifications\Depository\Notification
        {
                return self::$dice->create(Navigation\Notifications\Depository\Notification::class);
index 2e330224250e1b3feee49f63a24ef4e0304dd6ee..aade421bb1bd3bea81f9687e680a174691ae727a 100644 (file)
@@ -23,7 +23,7 @@ namespace Friendica\Factory\Api\Mastodon;
 
 use Friendica\BaseFactory;
 use Friendica\Collection\Api\Mastodon\Fields;
-use Friendica\Collection\ProfileFields;
+use Friendica\Profile\ProfileField\Collection\ProfileFields;
 use Friendica\Content\Text\BBCode;
 use Friendica\Profile\ProfileField\Entity\ProfileField;
 use Friendica\Network\HTTPException;
index b1d4314b7ff79ac70111ba34bbe1139461056446..c0f3a3f8dc544e5c4fa04da28236d668328d5b72 100644 (file)
@@ -21,7 +21,7 @@
 
 namespace Friendica\Module\Api\Friendica\Profile;
 
-use Friendica\Collection\ProfileFields;
+use Friendica\Profile\ProfileField\Collection\ProfileFields;
 use Friendica\Content\Text\BBCode;
 use Friendica\DI;
 use Friendica\Model\Contact;
@@ -45,7 +45,7 @@ class Show extends BaseApi
 
                $profile = Profile::getByUID($uid);
                
-               $profileFields = DI::profileField()->select(['uid' => $uid, 'psid' => PermissionSet::PUBLIC]);
+               $profileFields = DI::profileFieldNew()->selectPublicFieldsByUserId($uid);
 
                $profile = self::formatProfile($profile, $profileFields);
 
diff --git a/src/Profile/ProfileField/Collection/ProfileFields.php b/src/Profile/ProfileField/Collection/ProfileFields.php
new file mode 100644 (file)
index 0000000..b235ad2
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Profile\ProfileField\Collection;
+
+use Friendica\BaseCollection;
+
+class ProfileFields extends BaseCollection
+{
+       /**
+        * @param callable $callback
+        * @return ProfileFields
+        */
+       public function map(callable $callback)
+       {
+               return parent::map($callback);
+       }
+
+       /**
+        * @param callable|null $callback
+        * @param int           $flag
+        * @return ProfileFields
+        */
+       public function filter(callable $callback = null, int $flag = 0)
+       {
+               return parent::filter($callback, $flag);
+       }
+}
diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php
new file mode 100644 (file)
index 0000000..47abcbd
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+namespace Friendica\Profile\ProfileField\Depository;
+
+use Friendica\BaseDepository;
+use Friendica\Database\Database;
+use Friendica\Network\HTTPException\NotFoundException;
+use Friendica\Profile\ProfileField\Factory;
+use Friendica\Profile\ProfileField\Entity;
+use Friendica\Profile\ProfileField\Collection;
+use Friendica\Security\PermissionSet\Depository\PermissionSet;
+use Psr\Log\LoggerInterface;
+
+class ProfileField extends BaseDepository
+{
+       /** @var  Factory\ProfileField */
+       protected $factory;
+
+       protected static $table_name = 'profile_field';
+
+       public function __construct(Database $database, LoggerInterface $logger, Factory\ProfileField $factory)
+       {
+               parent::__construct($database, $logger, $factory);
+       }
+
+       /**
+        * @param array $condition
+        * @param array $params
+        * @return Entity\ProfileField
+        * @throws NotFoundException
+        */
+       private function selectOne(array $condition, array $params = []): Entity\ProfileField
+       {
+               return parent::_selectOne($condition, $params);
+       }
+
+       private function select(array $condition, array $params = []): Collection\ProfileFields
+       {
+               return new Collection\ProfileFields(parent::_select($condition, $params)->getArrayCopy());
+       }
+
+       /**
+        * Returns all public available ProfileFields for a specific user
+        *
+        * @param int $uid the user id
+        *
+        * @return Collection\ProfileFields
+        */
+       public function selectPublicFieldsByUserId(int $uid): Collection\ProfileFields
+       {
+               return $this->select([
+                       'uid'  => $uid,
+                       'psid' => PermissionSet::PUBLIC,
+               ]);
+       }
+}
index 6703615b0bd20f8145c6f13d6b221c952490ed98..ad122344f14405c1c5d69dfcb637f23e6346cf09 100644 (file)
 
 namespace Friendica\Profile\ProfileField\Entity;
 
-use Friendica\BaseModel;
-use Friendica\Database\Database;
-use Friendica\Network\HTTPException\NotFoundException;
+use Friendica\BaseEntity;
+use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
 use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
 use Friendica\Security\PermissionSet\Entity\PermissionSet;
-use Psr\Log\LoggerInterface;
 
 /**
  * Custom profile field model class.
@@ -34,40 +32,61 @@ use Psr\Log\LoggerInterface;
  * Custom profile fields are user-created arbitrary profile fields that can be assigned a permission set to restrict its
  * display to specific Friendica contacts as it requires magic authentication to work.
  *
- * @property int    uid
- * @property int    order
- * @property int    psid
- * @property string label
- * @property string value
- * @property string created
- * @property string edited
- * @property PermissionSet permissionSet
+ * @property-read int|null  $id
+ * @property-read int       $uid
+ * @property-read int       $order
+ * @property-read int       $permissionSetId
+ * @property-read string    $label
+ * @property-read string    $value
+ * @property-read \DateTime $created
+ * @property-read \DateTime $edited
+ * @property PermissionSet  $permissionSet
  */
-class ProfileField extends BaseModel
+class ProfileField extends BaseEntity
 {
+       /** @var int|null */
+       protected $id;
        /** @var PermissionSet */
-       private $permissionSet;
-
+       protected $permissionSet;
        /** @var PermissionSetDepository */
-       private $permissionSetDepository;
+       protected $permissionSetDepository;
+       /** @var int */
+       protected $uid;
+       /** @var int */
+       protected $order;
+       /** @var int */
+       protected $psid;
+       /** @var string */
+       protected $label;
+       /** @var string */
+       protected $value;
+       /** @var \DateTime */
+       protected $created;
+       /** @var \DateTime */
+       protected $edited;
 
-       public function __construct(Database $dba, LoggerInterface $logger, PermissionSetDepository $permissionSetDepository, array $data = [])
+       public function __construct(PermissionSetDepository $permissionSetDepository, int $uid, int $order, int $permissionSetId, string $label, string $value, \DateTime $created, \DateTime $edited, int $id = null)
        {
-               parent::__construct($dba, $logger, $data);
-
                $this->permissionSetDepository = $permissionSetDepository;
+
+               $this->uid     = $uid;
+               $this->order   = $order;
+               $this->psid    = $permissionSetId;
+               $this->label   = $label;
+               $this->value   = $value;
+               $this->created = $created;
+               $this->edited  = $edited;
+               $this->id      = $id;
        }
 
        public function __get($name)
        {
-               $this->checkValid();
-
                switch ($name) {
                        case 'permissionSet':
                                if (empty($this->permissionSet)) {
                                        $permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $this->uid);
                                        if ($permissionSet->uid !== $this->uid) {
-                                               throw new NotFoundException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $this->uid));
+                                               throw new UnexpectedPermissionSetException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $this->uid));
                                        }
 
                                        $this->permissionSet = $permissionSet;
diff --git a/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php b/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php
new file mode 100644 (file)
index 0000000..677a75b
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+
+namespace Friendica\Profile\ProfileField\Exception;
+
+class UnexpectedPermissionSetException extends \Exception
+{
+}
diff --git a/src/Profile/ProfileField/Factory/ProfileField.php b/src/Profile/ProfileField/Factory/ProfileField.php
new file mode 100644 (file)
index 0000000..ad0ebd6
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+namespace Friendica\Profile\ProfileField\Factory;
+
+use Friendica\BaseFactory;
+use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
+use Friendica\Profile\ProfileField\Entity;
+use Friendica\Capabilities\ICanCreateFromTableRow;
+use Psr\Log\LoggerInterface;
+
+class ProfileField extends BaseFactory implements ICanCreateFromTableRow
+{
+       /** @var PermissionSetDepository */
+       private $permissionSetDepository;
+
+       public function __construct(LoggerInterface $logger, PermissionSetDepository $permissionSetDepository)
+       {
+               parent::__construct($logger);
+
+               $this->permissionSetDepository = $permissionSetDepository;
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function createFromTableRow(array $row): Entity\ProfileField
+       {
+               return new Entity\ProfileField(
+                       $this->permissionSetDepository,
+                       $row['uid'],
+                       $row['order'],
+                       $row['psid'],
+                       $row['label'],
+                       $row['value'],
+                       new \DateTime($row['created'], new \DateTimeZone('UTC')),
+                       new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC'))
+               );
+       }
+}
index 75221933b3605bfb349a71a70f21ba3b6c30f8d5..878c6b2b4b68a36ad026b546f5d39210394178a8 100644 (file)
@@ -23,7 +23,6 @@ namespace Friendica\Repository;
 
 use Friendica\BaseModel;
 use Friendica\BaseRepository;
-use Friendica\Collection;
 use Friendica\Core\L10n;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
@@ -38,7 +37,7 @@ class ProfileField extends BaseRepository
 
        protected static $model_class = \Friendica\Profile\ProfileField\Entity\ProfileField::class;
 
-       protected static $collection_class = Collection\ProfileFields::class;
+       protected static $collection_class = \Friendica\Profile\ProfileField\Collection\ProfileFields::class;
 
        /** @var PermissionSet */
        private $permissionSet;
@@ -80,7 +79,8 @@ class ProfileField extends BaseRepository
        /**
         * @param array $condition
         * @param array $params
-        * @return Collection\ProfileFields
+        *
+        * @return \Friendica\Profile\ProfileField\Collection\ProfileFields
         * @throws \Exception
         */
        public function select(array $condition = [], array $params = [])
@@ -94,7 +94,8 @@ class ProfileField extends BaseRepository
         * @param int|null $min_id
         * @param int|null $max_id
         * @param int      $limit
-        * @return Collection\ProfileFields
+        *
+        * @return \Friendica\Profile\ProfileField\Collection\ProfileFields
         * @throws \Exception
         */
        public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
@@ -104,7 +105,8 @@ class ProfileField extends BaseRepository
 
        /**
         * @param int $uid Field owner user Id
-        * @return Collection\ProfileFields
+        *
+        * @return \Friendica\Profile\ProfileField\Collection\ProfileFields
         * @throws \Exception
         */
        public function selectByUserId(int $uid)
@@ -120,7 +122,8 @@ class ProfileField extends BaseRepository
         *
         * @param int $cid Private contact id, must be owned by $uid
         * @param int $uid Field owner user id
-        * @return Collection\ProfileFields
+        *
+        * @return \Friendica\Profile\ProfileField\Collection\ProfileFields
         * @throws \Exception
         */
        public function selectByContactId(int $cid, int $uid)
@@ -166,14 +169,15 @@ class ProfileField extends BaseRepository
        }
 
        /**
-        * @param int                      $uid                User Id
-        * @param Collection\ProfileFields $profileFields      Collection of existing profile fields
-        * @param array                    $profileFieldInputs Array of profile field form inputs indexed by profile field id
-        * @param array                    $profileFieldOrder  List of profile field id in order
-        * @return Collection\ProfileFields
+        * @param int                                                      $uid                User Id
+        * @param \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields      Collection of existing profile fields
+        * @param array                                                    $profileFieldInputs Array of profile field form inputs indexed by profile field id
+        * @param array                                                    $profileFieldOrder  List of profile field id in order
+        *
+        * @return \Friendica\Profile\ProfileField\Collection\ProfileFields
         * @throws \Exception
         */
-       public function updateCollectionFromForm(int $uid, Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder)
+       public function updateCollectionFromForm(int $uid, \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder)
        {
                // Returns an associative array of id => order values
                $profileFieldOrder = array_flip($profileFieldOrder);
@@ -232,8 +236,8 @@ class ProfileField extends BaseRepository
                                        $profileFieldInputs[$profileField->id]['group_deny'] ?? ''
                                ))->id;
 
-                               $profileField->psid = $psid;
-                               $profileField->label = $profileFieldInputs[$profileField->id]['label'];
+                               $profileField->permissionSetId = $psid;
+                               $profileField->label           = $profileFieldInputs[$profileField->id]['label'];
                                $profileField->value = $profileFieldInputs[$profileField->id]['value'];
                                $profileField->order = $profileFieldOrder[$profileField->id];