Implemented AbstractMap and ActionHttpMethodMap

This commit is contained in:
Kevin Frantz 2019-01-29 20:02:36 +01:00
parent d50a79f912
commit 8e2d79d236
5 changed files with 127 additions and 14 deletions

View File

@ -4,11 +4,12 @@ namespace App\Domain\LayerManagement;
use App\DBAL\Types\Meta\Right\LayerType; use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\ActionType; use App\DBAL\Types\ActionType;
use App\Domain\MapManagement\AbstractMap;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
final class LayerActionMap implements LayerActionMapInterface final class LayerActionMap extends AbstractMap implements LayerActionMapInterface
{ {
/** /**
* @var array Add new combination possibilities to this map! * @var array Add new combination possibilities to this map!
@ -30,14 +31,7 @@ final class LayerActionMap implements LayerActionMapInterface
*/ */
public static function getLayers(string $action): array public static function getLayers(string $action): array
{ {
$layers = []; return parent::getIndizesByValue($action, self::LAYER_ACTION_MAP);
foreach (self::LAYER_ACTION_MAP as $layer => $actions) {
if (in_array($action, $actions)) {
$layers[] = $layer;
}
}
return $layers;
} }
/** /**
@ -47,10 +41,6 @@ final class LayerActionMap implements LayerActionMapInterface
*/ */
public static function getActions(string $layer): array public static function getActions(string $layer): array
{ {
if (array_key_exists($layer, self::LAYER_ACTION_MAP)) { return parent::getValuesByIndex($layer, self::LAYER_ACTION_MAP);
return self::LAYER_ACTION_MAP[$layer];
}
return [];
} }
} }

View File

@ -0,0 +1,44 @@
<?php
namespace App\Domain\MapManagement;
/**
* This class offers the basic functions for managing an 2 dimensional map.
*
* @author kevinfrantz
*/
abstract class AbstractMap implements MapInterface
{
/**
* @param string $index
* @param array|string[] $map
*
* @return array|string[]
*/
protected static function getValuesByIndex(string $index, array $map): array
{
if (array_key_exists($index, $map)) {
return $map[$index];
}
return [];
}
/**
* @param string $value
* @param array|string[] $map
*
* @return array|string[]
*/
protected static function getIndizesByValue(string $value, array $map): array
{
$result = [];
foreach ($map as $index => $values) {
if (in_array($value, $values)) {
$result[] = $index;
}
}
return $result;
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Domain\MapManagement;
/**
* @author kevinfrantz
*/
interface MapInterface
{
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Domain\ResponseManagement;
use App\Domain\MapManagement\AbstractMap;
use App\DBAL\Types\ActionType;
use Symfony\Component\HttpFoundation\Request;
/**
* @author kevinfrantz
*/
final class ActionHttpMethodMap extends AbstractMap implements ActionHttpMethodMapInterface
{
const ACTION_HTTP_METHOD_MAP = [
ActionType::READ => [
Request::METHOD_GET,
],
ActionType::CREATE => [
Request::METHOD_POST,
Request::METHOD_GET,
],
ActionType::UPDATE => [
Request::METHOD_PUT,
Request::METHOD_GET,
],
ActionType::DELETE => [
Request::METHOD_GET,
Request::METHOD_DELETE,
],
ActionType::THREAD => [
Request::METHOD_GET,
],
];
public static function getActions(string $httpMethod): array
{
return parent::getIndizesByValue($httpMethod, self::ACTION_HTTP_METHOD_MAP);
}
public static function getHttpMethods(string $action): array
{
return parent::getValuesByIndex($action, self::ACTION_HTTP_METHOD_MAP);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Domain\ResponseManagement;
/**
* This class offers a map for ActionTypes to HttpMethods.
*
* @author kevinfrantz
*/
interface ActionHttpMethodMapInterface
{
/**
* @param string $httpMethod
*
* @return array|string[] The Http-Methods which belong to an action
*/
public static function getActions(string $httpMethod): array;
/**
* @param string $action
*
* @return array|string[] The Http-Methods which are possible for an action
*/
public static function getHttpMethods(string $action): array;
}