Add corresponding interface to trait

Since we can't specify that we want a class implementing a trait yet in
PHP

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
This commit is contained in:
Carl Schwan 2022-10-17 00:55:48 +02:00
parent 2d75321c23
commit c94f9f5e5f
7 changed files with 112 additions and 80 deletions

View file

@ -29,6 +29,8 @@ use OCA\Files_External\Lib\IdentifierTrait;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Lib\StorageModifierTrait;
use OCA\Files_External\Lib\VisibilityTrait;
use OCA\Files_External\Lib\IIdentifier;
use OCA\Files_External\Lib\IFrontendDefintion;
/**
* Authentication mechanism
@ -50,7 +52,7 @@ use OCA\Files_External\Lib\VisibilityTrait;
* - StorageModifierTrait
* Object can affect storage mounting
*/
class AuthMechanism implements \JsonSerializable {
class AuthMechanism implements \JsonSerializable, IIdentifier, IFrontendDefintion {
/** Standard authentication schemes */
public const SCHEME_NULL = 'null';
public const SCHEME_BUILTIN = 'builtin';

View file

@ -30,6 +30,8 @@ use OCA\Files_External\Lib\PriorityTrait;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Lib\StorageModifierTrait;
use OCA\Files_External\Lib\VisibilityTrait;
use OCA\Files_External\Lib\IIdentifier;
use OCA\Files_External\Lib\IFrontendDefintion;
/**
* Storage backend
@ -55,7 +57,7 @@ use OCA\Files_External\Lib\VisibilityTrait;
* - StorageModifierTrait
* Object can affect storage mounting
*/
class Backend implements \JsonSerializable {
class Backend implements \JsonSerializable, IIdentifier, IFrontendDefintion {
use VisibilityTrait;
use FrontendDefinitionTrait;
use PriorityTrait;

View file

@ -29,62 +29,45 @@ namespace OCA\Files_External\Lib;
trait FrontendDefinitionTrait {
/** @var string human-readable mechanism name */
private $text;
private string $text = "";
/** @var DefinitionParameter[] parameters for mechanism */
private $parameters = [];
private array $parameters = [];
/** @var string[] custom JS */
private $customJs = [];
private array $customJs = [];
/**
* @return string
*/
public function getText() {
public function getText(): string {
return $this->text;
}
/**
* @param string $text
* @return $this
*/
public function setText($text) {
public function setText(string $text): self {
$this->text = $text;
return $this;
}
/**
* @param FrontendDefinitionTrait $a
* @param FrontendDefinitionTrait $b
* @return int
*/
public static function lexicalCompare(FrontendDefinitionTrait $a, FrontendDefinitionTrait $b) {
public static function lexicalCompare(IFrontendDefinition $a, IFrontendDefinition $b): int {
return strcmp($a->getText(), $b->getText());
}
/**
* @return DefinitionParameter[]
*/
public function getParameters() {
public function getParameters(): array {
return $this->parameters;
}
/**
* @param DefinitionParameter[] $parameters
* @return self
*/
public function addParameters(array $parameters) {
public function addParameters(array $parameters): self {
foreach ($parameters as $parameter) {
$this->addParameter($parameter);
}
return $this;
}
/**
* @param DefinitionParameter $parameter
* @return self
*/
public function addParameter(DefinitionParameter $parameter) {
public function addParameter(DefinitionParameter $parameter): self {
$this->parameters[$parameter->getName()] = $parameter;
return $this;
}
@ -92,7 +75,7 @@ trait FrontendDefinitionTrait {
/**
* @return string[]
*/
public function getCustomJs() {
public function getCustomJs(): array {
return $this->customJs;
}
@ -100,17 +83,15 @@ trait FrontendDefinitionTrait {
* @param string $custom
* @return self
*/
public function addCustomJs($custom) {
public function addCustomJs(string $custom): self {
$this->customJs[] = $custom;
return $this;
}
/**
* Serialize into JSON for client-side JS
*
* @return array
*/
public function jsonSerializeDefinition() {
public function jsonSerializeDefinition(): array {
$configuration = [];
foreach ($this->getParameters() as $parameter) {
$configuration[$parameter->getName()] = $parameter;
@ -126,11 +107,8 @@ trait FrontendDefinitionTrait {
/**
* Check if parameters are satisfied in a StorageConfig
*
* @param StorageConfig $storage
* @return bool
*/
public function validateStorageDefinition(StorageConfig $storage) {
public function validateStorageDefinition(StorageConfig $storage): bool {
foreach ($this->getParameters() as $name => $parameter) {
$value = $storage->getBackendOption($name);
if (!is_null($value) || !$parameter->isOptional()) {

View file

@ -0,0 +1,56 @@
<?php
/**
* @copyright 2022 Carl Schwan <carl@carlschwan.eu>
*
* @license AGPL-3.0-or-later
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Lib;
interface IFrontendDefintion {
public function getText(): string;
public function setText(string $text): self;
/**
* @return list<DefinitionParameter>
*/
public function getParameters(): array;
/**
* @param DefinitionParameter[] $parameters
*/
public function addParameters(array $parameters): self;
public function addParameter(DefinitionParameter $parameter): self;
/**
* @return string[]
*/
public function getCustomJs(): array;
public function addCustomJs(string $custom): self;
/**
* Serialize into JSON for client-side JS
*/
public function jsonSerializeDefinition(): array;
/**
* Check if parameters are satisfied in a StorageConfig
*/
public function validateStorageDefinition(StorageConfig $storage): bool;
}

View file

@ -0,0 +1,27 @@
<?php
/**
* @copyright 2022 Carl Schwan <carl@carlschwan.eu>
*
* @license AGPL-3.0-or-later
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Lib;
interface IIdentifier {
public function getIdentifier(): string;
public function setIdentifier(string $identifier): self;
}

View file

@ -28,27 +28,17 @@ namespace OCA\Files_External\Lib;
*/
trait IdentifierTrait {
/** @var string */
protected $identifier;
protected string $identifier = '';
/** @var string[] */
protected $identifierAliases = [];
protected array $identifierAliases = [];
protected ?IIdentifier $deprecateTo = null;
/** @var IdentifierTrait */
protected $deprecateTo = null;
/**
* @return string
*/
public function getIdentifier() {
public function getIdentifier(): string {
return $this->identifier;
}
/**
* @param string $identifier
* @return $this
*/
public function setIdentifier($identifier) {
public function setIdentifier(string $identifier): self {
$this->identifier = $identifier;
$this->identifierAliases[] = $identifier;
return $this;
@ -57,39 +47,25 @@ trait IdentifierTrait {
/**
* @return string[]
*/
public function getIdentifierAliases() {
public function getIdentifierAliases(): array {
return $this->identifierAliases;
}
/**
* @param string $alias
* @return $this
*/
public function addIdentifierAlias($alias) {
public function addIdentifierAlias(string $alias): self {
$this->identifierAliases[] = $alias;
return $this;
}
/**
* @return object|null
*/
public function getDeprecateTo() {
public function getDeprecateTo(): ?IIdentifier {
return $this->deprecateTo;
}
/**
* @param object $destinationObject
* @return self
*/
public function deprecateTo($destinationObject) {
public function deprecateTo(IIdentifier $destinationObject): self {
$this->deprecateTo = $destinationObject;
return $this;
}
/**
* @return array
*/
public function jsonSerializeIdentifier() {
public function jsonSerializeIdentifier(): array {
$data = [
'identifier' => $this->identifier,
'identifierAliases' => $this->identifierAliases,

View file

@ -47,13 +47,4 @@ trait PriorityTrait {
$this->priority = $priority;
return $this;
}
/**
* @param PriorityTrait $a
* @param PriorityTrait $b
* @return int
*/
public static function priorityCompare(PriorityTrait $a, PriorityTrait $b) {
return ($a->getPriority() - $b->getPriority());
}
}