From 0fc5f26ff7c0301da42bfc360b1ad70359184e5c Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 17 Jan 2021 16:04:00 -0500 Subject: [PATCH] Replace BaseEntity with BaseDataTransferObject class for API representation classes --- ...seEntity.php => BaseDataTransferObject.php} | 8 ++++---- src/BaseModel.php | 18 +++++++++++------- src/Object/Api/Friendica/Notification.php | 4 ++-- src/Object/Api/Mastodon/Account.php | 6 +++--- src/Object/Api/Mastodon/Activity.php | 4 ++-- src/Object/Api/Mastodon/Application.php | 4 ++-- src/Object/Api/Mastodon/Attachment.php | 6 +++--- src/Object/Api/Mastodon/Card.php | 8 ++++---- src/Object/Api/Mastodon/Emoji.php | 4 ++-- src/Object/Api/Mastodon/Error.php | 6 +++--- src/Object/Api/Mastodon/Field.php | 4 ++-- src/Object/Api/Mastodon/Instance.php | 4 ++-- src/Object/Api/Mastodon/Mention.php | 4 ++-- src/Object/Api/Mastodon/Relationship.php | 4 ++-- src/Object/Api/Mastodon/Stats.php | 4 ++-- src/Object/Api/Mastodon/Status.php | 6 +++--- src/Object/Api/Mastodon/Tag.php | 4 ++-- src/Object/Api/Twitter/User.php | 4 ++-- 18 files changed, 53 insertions(+), 49 deletions(-) rename src/{BaseEntity.php => BaseDataTransferObject.php} (83%) diff --git a/src/BaseEntity.php b/src/BaseDataTransferObject.php similarity index 83% rename from src/BaseEntity.php rename to src/BaseDataTransferObject.php index 1ea3f8a16..5736cdf32 100644 --- a/src/BaseEntity.php +++ b/src/BaseDataTransferObject.php @@ -22,20 +22,20 @@ namespace Friendica; /** - * The API entity classes are meant as data transfer objects. As such, their member should be protected. + * These data transfer object classes are meant for API representations. As such, their members should be protected. * Then the JsonSerializable interface ensures the protected members will be included in a JSON encode situation. * * Constructors are supposed to take as arguments the Friendica dependencies/model/collection/data it needs to * populate the class members. */ -abstract class BaseEntity implements \JsonSerializable +abstract class BaseDataTransferObject implements \JsonSerializable { /** * Returns the current entity as an json array * * @return array */ - public function jsonSerialize() + public function jsonSerialize(): array { return $this->toArray(); } @@ -45,7 +45,7 @@ abstract class BaseEntity implements \JsonSerializable * * @return array */ - public function toArray() + public function toArray(): array { return get_object_vars($this); } diff --git a/src/BaseModel.php b/src/BaseModel.php index 41320d8bd..a2750d568 100644 --- a/src/BaseModel.php +++ b/src/BaseModel.php @@ -31,7 +31,7 @@ use Psr\Log\LoggerInterface; * * @property int id */ -abstract class BaseModel extends BaseEntity +abstract class BaseModel extends BaseDataTransferObject { /** @var Database */ protected $dba; @@ -67,7 +67,7 @@ abstract class BaseModel extends BaseEntity $this->originalData = $data; } - public function getOriginalData() + public function getOriginalData(): array { return $this->originalData; } @@ -84,7 +84,7 @@ abstract class BaseModel extends BaseEntity * @param array $data * @return BaseModel */ - public static function createFromPrototype(BaseModel $prototype, array $data) + public static function createFromPrototype(BaseModel $prototype, array $data): BaseModel { $model = clone $prototype; $model->data = $data; @@ -100,7 +100,7 @@ abstract class BaseModel extends BaseEntity * @param $name * @return bool */ - public function __isset($name) + public function __isset($name): bool { return in_array($name, array_merge(array_keys($this->data), array_keys(get_object_vars($this)))); } @@ -126,15 +126,19 @@ abstract class BaseModel extends BaseEntity } /** + * * Magic setter. This allows to set model fields with the following syntax: + * - $model->field = $value (outside of class) + * - $this->field = $value (inside of class) + * * @param string $name - * @param mixed $value + * @param mixed $value */ - public function __set($name, $value) + public function __set(string $name, $value) { $this->data[$name] = $value; } - public function toArray() + public function toArray(): array { return $this->data; } diff --git a/src/Object/Api/Friendica/Notification.php b/src/Object/Api/Friendica/Notification.php index aee787da1..60c824a56 100644 --- a/src/Object/Api/Friendica/Notification.php +++ b/src/Object/Api/Friendica/Notification.php @@ -21,7 +21,7 @@ namespace Friendica\Object\Api\Friendica; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; use Friendica\Model\Notify; @@ -33,7 +33,7 @@ use Friendica\Util\Temporal; * * @see https://github.com/friendica/friendica/blob/develop/doc/API-Entities.md#notification */ -class Notification extends BaseEntity +class Notification extends BaseDataTransferObject { /** @var integer */ protected $id; diff --git a/src/Object/Api/Mastodon/Account.php b/src/Object/Api/Mastodon/Account.php index 587c6ce6d..e74339563 100644 --- a/src/Object/Api/Mastodon/Account.php +++ b/src/Object/Api/Mastodon/Account.php @@ -22,7 +22,7 @@ namespace Friendica\Object\Api\Mastodon; use Friendica\App\BaseURL; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; use Friendica\Collection\Api\Mastodon\Fields; use Friendica\Content\Text\BBCode; use Friendica\Database\DBA; @@ -34,7 +34,7 @@ use Friendica\Util\DateTimeFormat; * * @see https://docs.joinmastodon.org/entities/account */ -class Account extends BaseEntity +class Account extends BaseDataTransferObject { /** @var string */ protected $id; @@ -138,7 +138,7 @@ class Account extends BaseEntity * * @return array */ - public function toArray() + public function toArray(): array { $account = parent::toArray(); diff --git a/src/Object/Api/Mastodon/Activity.php b/src/Object/Api/Mastodon/Activity.php index a73307eb4..73bde98a4 100644 --- a/src/Object/Api/Mastodon/Activity.php +++ b/src/Object/Api/Mastodon/Activity.php @@ -21,14 +21,14 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Activity * * @see https://docs.joinmastodon.org/entities/activity */ -class Activity extends BaseEntity +class Activity extends BaseDataTransferObject { /** @var string (UNIX Timestamp) */ protected $week; diff --git a/src/Object/Api/Mastodon/Application.php b/src/Object/Api/Mastodon/Application.php index d26d270d9..8cac4aaf7 100644 --- a/src/Object/Api/Mastodon/Application.php +++ b/src/Object/Api/Mastodon/Application.php @@ -21,14 +21,14 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Application * * @see https://docs.joinmastodon.org/entities/application */ -class Application extends BaseEntity +class Application extends BaseDataTransferObject { /** @var string */ protected $name; diff --git a/src/Object/Api/Mastodon/Attachment.php b/src/Object/Api/Mastodon/Attachment.php index 1651e9c40..cc55b3715 100644 --- a/src/Object/Api/Mastodon/Attachment.php +++ b/src/Object/Api/Mastodon/Attachment.php @@ -21,14 +21,14 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Attachment * * @see https://docs.joinmastodon.org/entities/attachment */ -class Attachment extends BaseEntity +class Attachment extends BaseDataTransferObject { /** @var string */ protected $id; @@ -67,7 +67,7 @@ class Attachment extends BaseEntity * * @return array */ - public function toArray() + public function toArray(): array { $attachment = parent::toArray(); diff --git a/src/Object/Api/Mastodon/Card.php b/src/Object/Api/Mastodon/Card.php index 2f46e4779..33e3c1059 100644 --- a/src/Object/Api/Mastodon/Card.php +++ b/src/Object/Api/Mastodon/Card.php @@ -21,14 +21,14 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Card * * @see https://docs.joinmastodon.org/entities/card */ -class Card extends BaseEntity +class Card extends BaseDataTransferObject { /** @var string */ protected $url; @@ -67,10 +67,10 @@ class Card extends BaseEntity * * @return array */ - public function toArray() + public function toArray(): array { if (empty($this->url)) { - return null; + return []; } return parent::toArray(); diff --git a/src/Object/Api/Mastodon/Emoji.php b/src/Object/Api/Mastodon/Emoji.php index 1f6f12150..837b186b6 100644 --- a/src/Object/Api/Mastodon/Emoji.php +++ b/src/Object/Api/Mastodon/Emoji.php @@ -21,14 +21,14 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Emoji * * @see https://docs.joinmastodon.org/entities/emoji/ */ -class Emoji extends BaseEntity +class Emoji extends BaseDataTransferObject { //Required attributes /** @var string */ diff --git a/src/Object/Api/Mastodon/Error.php b/src/Object/Api/Mastodon/Error.php index 0bfd1826c..44573987f 100644 --- a/src/Object/Api/Mastodon/Error.php +++ b/src/Object/Api/Mastodon/Error.php @@ -21,14 +21,14 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Error * * @see https://docs.joinmastodon.org/entities/error */ -class Error extends BaseEntity +class Error extends BaseDataTransferObject { /** @var string */ protected $error; @@ -53,7 +53,7 @@ class Error extends BaseEntity * * @return array */ - public function toArray() + public function toArray(): array { $error = parent::toArray(); diff --git a/src/Object/Api/Mastodon/Field.php b/src/Object/Api/Mastodon/Field.php index 95cbc89df..91e8fb191 100644 --- a/src/Object/Api/Mastodon/Field.php +++ b/src/Object/Api/Mastodon/Field.php @@ -21,14 +21,14 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Field * * @see https://docs.joinmastodon.org/entities/field/ */ -class Field extends BaseEntity +class Field extends BaseDataTransferObject { /** @var string */ protected $name; diff --git a/src/Object/Api/Mastodon/Instance.php b/src/Object/Api/Mastodon/Instance.php index 6105a8bee..d08637509 100644 --- a/src/Object/Api/Mastodon/Instance.php +++ b/src/Object/Api/Mastodon/Instance.php @@ -21,7 +21,7 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\User; @@ -32,7 +32,7 @@ use Friendica\Module\Register; * * @see https://docs.joinmastodon.org/api/entities/#instance */ -class Instance extends BaseEntity +class Instance extends BaseDataTransferObject { /** @var string (URL) */ protected $uri; diff --git a/src/Object/Api/Mastodon/Mention.php b/src/Object/Api/Mastodon/Mention.php index 22e623e60..6de6f0048 100644 --- a/src/Object/Api/Mastodon/Mention.php +++ b/src/Object/Api/Mastodon/Mention.php @@ -22,14 +22,14 @@ namespace Friendica\Object\Api\Mastodon; use Friendica\App\BaseURL; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Mention * * @see https://docs.joinmastodon.org/entities/mention */ -class Mention extends BaseEntity +class Mention extends BaseDataTransferObject { /** @var string */ protected $id; diff --git a/src/Object/Api/Mastodon/Relationship.php b/src/Object/Api/Mastodon/Relationship.php index bb3aa5541..06f973788 100644 --- a/src/Object/Api/Mastodon/Relationship.php +++ b/src/Object/Api/Mastodon/Relationship.php @@ -21,7 +21,7 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; use Friendica\Model\Contact; use Friendica\Util\Network; @@ -30,7 +30,7 @@ use Friendica\Util\Network; * * @see https://docs.joinmastodon.org/api/entities/#relationship */ -class Relationship extends BaseEntity +class Relationship extends BaseDataTransferObject { /** @var int */ protected $id; diff --git a/src/Object/Api/Mastodon/Stats.php b/src/Object/Api/Mastodon/Stats.php index 6ead52672..398b7252d 100644 --- a/src/Object/Api/Mastodon/Stats.php +++ b/src/Object/Api/Mastodon/Stats.php @@ -21,7 +21,7 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; use Friendica\Core\Protocol; use Friendica\Database\DBA; use Friendica\DI; @@ -31,7 +31,7 @@ use Friendica\DI; * * @see https://docs.joinmastodon.org/api/entities/#stats */ -class Stats extends BaseEntity +class Stats extends BaseDataTransferObject { /** @var int */ protected $user_count = 0; diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index d14eb4efa..c4b7e397d 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -21,7 +21,7 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; use Friendica\Content\Text\BBCode; use Friendica\Object\Api\Mastodon\Status\Counts; use Friendica\Object\Api\Mastodon\Status\UserAttributes; @@ -32,7 +32,7 @@ use Friendica\Util\DateTimeFormat; * * @see https://docs.joinmastodon.org/entities/status */ -class Status extends BaseEntity +class Status extends BaseDataTransferObject { /** @var string */ protected $id; @@ -143,7 +143,7 @@ class Status extends BaseEntity * * @return array */ - public function toArray() + public function toArray(): array { $status = parent::toArray(); diff --git a/src/Object/Api/Mastodon/Tag.php b/src/Object/Api/Mastodon/Tag.php index 1e74eae00..c83c3bcf3 100644 --- a/src/Object/Api/Mastodon/Tag.php +++ b/src/Object/Api/Mastodon/Tag.php @@ -22,14 +22,14 @@ namespace Friendica\Object\Api\Mastodon; use Friendica\App\BaseURL; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; /** * Class Tag * * @see https://docs.joinmastodon.org/entities/tag */ -class Tag extends BaseEntity +class Tag extends BaseDataTransferObject { /** @var string */ protected $name; diff --git a/src/Object/Api/Twitter/User.php b/src/Object/Api/Twitter/User.php index 1cdd699de..7793fbc26 100644 --- a/src/Object/Api/Twitter/User.php +++ b/src/Object/Api/Twitter/User.php @@ -21,14 +21,14 @@ namespace Friendica\Object\Api\Twitter; -use Friendica\BaseEntity; +use Friendica\BaseDataTransferObject; use Friendica\Content\ContactSelector; use Friendica\Content\Text\BBCode; /** * @see https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object */ -class User extends BaseEntity +class User extends BaseDataTransferObject { /** @var int */ protected $id;