Skip to content

Commit

Permalink
Merge pull request #313 from oat-sa/feature/legacy/TR-1499/support_qt…
Browse files Browse the repository at this point in the history
…i_figure_figcaption_elements

feature: legacy TR-1499 html5+figure/figcaption support
  • Loading branch information
pnal authored Jun 10, 2022
2 parents a78b864 + c31d336 commit 798edd1
Show file tree
Hide file tree
Showing 19 changed files with 1,262 additions and 20 deletions.
93 changes: 93 additions & 0 deletions qtism/common/enums/AbstractEnumeration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* 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; under version 2
* of the License (non-upgradable).
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2022 (original work) Open Assessment Technologies SA;
*/

namespace qtism\common\enums;

use InvalidArgumentException;

/**
* Abstract enumeration automatizes the translation from constant name to
* value and from value to name.
*/
abstract class AbstractEnumeration implements Enumeration
{
abstract public static function asArray(): array;

/**
* @param $name
* @return false|int
*/
public static function getConstantByName($name)
{
return static::asArray()[$name] ?? false;
}

/**
* @param $constant
* @return false|string
*/
public static function getNameByConstant($constant)
{
$constants = array_flip(static::asArray());

return $constants[$constant] ?? false;
}

/**
* Returns the default constant for the enumeration.
*
* @return int|null
*/
abstract public static function getDefault(): ?int;

/**
* Checks that the given value is null or one of the enumeration constants.
*
* @param int|string|null $value
* @param string $argumentName
* @return int|null
* @throws InvalidArgumentException when $value is not in the enumeration.
*/
public static function accept($value, string $argumentName): ?int
{
$enumValues = static::asArray();
$nameExistsInEnum = array_key_exists($value, $enumValues);

if ($value !== null
&& !$nameExistsInEnum
&& !in_array($value, $enumValues, true)
) {
throw new InvalidArgumentException(
sprintf(
'The "%s" argument must be a value from the %s enumeration, "%s" given.',
$argumentName,
basename(str_replace('\\', DIRECTORY_SEPARATOR, static::class)),
$value
)
);
}

if ($nameExistsInEnum) {
$value = $enumValues[$value];
}

return $value ?? static::getDefault();
}
}
13 changes: 13 additions & 0 deletions qtism/common/utils/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,17 @@ public static function isAriaIdRefs($ariaIdRefs)

return true;
}

/**
* Is the given string a normalized string (no line break nor tabulation)?
*
* @param string $string
* @return bool
*/
public static function isNormalizedString(string $string): bool
{
return strpos($string, "\n") === false
&& strpos($string, "\r") === false
&& strpos($string, "\t") === false;
}
}
Loading

0 comments on commit 798edd1

Please sign in to comment.