Skip to content

Commit

Permalink
feat: Adding a new abstract class as base class for form question types
Browse files Browse the repository at this point in the history
  • Loading branch information
ccailly authored and cedric-anne committed Apr 25, 2024
1 parent 72b2690 commit aaedd26
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 126 deletions.
77 changes: 77 additions & 0 deletions src/Form/QuestionType/AbstractQuestionType.php
@@ -0,0 +1,77 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Form\QuestionType;

use Glpi\Form\Question;
use Override;

abstract class AbstractQuestionType implements QuestionTypeInterface
{
#[Override]
public function __construct()
{
}

#[Override]
public static function formatDefaultValueForDB(mixed $value): ?string
{
return $value; // Default value is already formatted
}

#[Override]
public static function validateExtraDataInput(array $input): bool
{
return empty($input); // No extra data by default
}

#[Override]
public function renderAdministrationOptionsTemplate(?Question $question): string
{
return ''; // No options by default
}

#[Override]
public function getName(): string
{
return $this->getCategory()->getLabel();
}

#[Override]
public function getWeight(): int
{
return 10;
}
}
7 changes: 1 addition & 6 deletions src/Form/QuestionType/AbstractQuestionTypeActors.php
Expand Up @@ -42,13 +42,8 @@
/**
* "Actors" questions represent an input field for actors (requesters, ...)
*/
abstract class AbstractQuestionTypeActors implements QuestionTypeInterface
abstract class AbstractQuestionTypeActors extends AbstractQuestionType
{
#[Override]
public function __construct()
{
}

/**
* Retrieve the allowed actor types
*
Expand Down
25 changes: 1 addition & 24 deletions src/Form/QuestionType/AbstractQuestionTypeShortAnswer.php
Expand Up @@ -42,32 +42,15 @@
/**
* Short answers are single line inputs used to answer simple questions.
*/
abstract class AbstractQuestionTypeShortAnswer implements QuestionTypeInterface
abstract class AbstractQuestionTypeShortAnswer extends AbstractQuestionType
{
#[Override]
public function __construct()
{
}

/**
* Specific input type for child classes
*
* @return string
*/
abstract public function getInputType(): string;

#[Override]
public static function formatDefaultValueForDB(mixed $value): ?string
{
return $value;
}

#[Override]
public static function validateExtraDataInput(array $input): bool
{
return empty($input); // No extra data for this question type
}

#[Override]
public function renderAdministrationTemplate(
?Question $question = null,
Expand All @@ -91,12 +74,6 @@ class="form-control mb-2"
]);
}

#[Override]
public function renderAdministrationOptionsTemplate(?Question $question): string
{
return '';
}

#[Override]
public function renderEndUserTemplate(
Question $question,
Expand Down
25 changes: 1 addition & 24 deletions src/Form/QuestionType/QuestionTypeDateTime.php
Expand Up @@ -43,37 +43,14 @@
/**
* Short answers are single line inputs used to answer simple questions.
*/
class QuestionTypeDateTime implements QuestionTypeInterface
class QuestionTypeDateTime extends AbstractQuestionType
{
#[Override]
public function __construct()
{
}

#[Override]
public static function formatDefaultValueForDB(mixed $value): ?string
{
return $value;
}

#[Override]
public function getCategory(): QuestionTypeCategory
{
return QuestionTypeCategory::DATE_AND_TIME;
}

#[Override]
public function getName(): string
{
return 'Date and time';
}

#[Override]
public function getWeight(): int
{
return 10;
}

public function getInputType(?Question $question, bool $ignoreDefaultValueIsCurrentTime = false): string
{
if (
Expand Down
37 changes: 1 addition & 36 deletions src/Form/QuestionType/QuestionTypeLongText.php
Expand Up @@ -42,25 +42,8 @@
/**
* Long answers are multiple lines inputs used to answer questions with as much details as needed.
*/
final class QuestionTypeLongText implements QuestionTypeInterface
final class QuestionTypeLongText extends AbstractQuestionType
{
#[Override]
public function __construct()
{
}

#[Override]
public static function formatDefaultValueForDB(mixed $value): ?string
{
return $value;
}

#[Override]
public static function validateExtraDataInput(array $input): bool
{
return empty($input); // No extra data for this question type
}

#[Override]
public function renderAdministrationTemplate(?Question $question): string
{
Expand Down Expand Up @@ -91,12 +74,6 @@ public function renderAdministrationTemplate(?Question $question): string
]);
}

#[Override]
public function renderAdministrationOptionsTemplate(?Question $question): string
{
return '';
}

#[Override]
public function renderEndUserTemplate(Question $question): string
{
Expand Down Expand Up @@ -139,21 +116,9 @@ public function renderAnswerTemplate($answer): string
]);
}

#[Override]
public function getName(): string
{
return __("Long answer");
}

#[Override]
public function getCategory(): QuestionTypeCategory
{
return QuestionTypeCategory::LONG_ANSWER;
}

#[Override]
public function getWeight(): int
{
return 10;
}
}
37 changes: 1 addition & 36 deletions src/Form/QuestionType/QuestionTypeUrgency.php
Expand Up @@ -40,25 +40,8 @@
use Glpi\Form\Question;
use Override;

final class QuestionTypeUrgency implements QuestionTypeInterface
final class QuestionTypeUrgency extends AbstractQuestionType
{
#[Override]
public function __construct()
{
}

#[Override]
public static function formatDefaultValueForDB(mixed $value): ?string
{
return $value;
}

#[Override]
public static function validateExtraDataInput(array $input): bool
{
return empty($input); // No extra data for this question type
}

/**
* Retrieve the default value for the urgency question type
*
Expand Down Expand Up @@ -111,12 +94,6 @@ public function renderAdministrationTemplate(?Question $question): string
]);
}

#[Override]
public function renderAdministrationOptionsTemplate(?Question $question): string
{
return '';
}

#[Override]
public function renderEndUserTemplate(Question $question): string
{
Expand Down Expand Up @@ -159,21 +136,9 @@ public function renderAnswerTemplate($answer): string
]);
}

#[Override]
public function getName(): string
{
return __("Urgency");
}

#[Override]
public function getCategory(): QuestionTypeCategory
{
return QuestionTypeCategory::URGENCY;
}

#[Override]
public function getWeight(): int
{
return 10;
}
}

0 comments on commit aaedd26

Please sign in to comment.