ActionKey.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\config;
12 
14 
15 /**
16  * An action key is a combination of a resource, context and action that is
17  * represented as a string. ActionKey is a helper class for handling
18  * action keys.
19  *
20  * @author ingo herwig <ingo@wemove.com>
21  */
22 class ActionKey {
23 
24  private static $actionDelimiter = '?';
25 
26  /**
27  * Create an action key from the given values
28  * @param $resource The resource
29  * @param $context The context
30  * @param $action The action
31  * @return String
32  */
33  public static function createKey($resource, $context, $action) {
34  return $resource.self::$actionDelimiter.$context.self::$actionDelimiter.$action;
35  }
36 
37  /**
38  * Parse an action
39  * @param $actionKey The action key
40  * @return Associative array with keys 'resouce', 'context', 'action'
41  */
42  public static function parseKey($actionKey) {
43  list($resource, $context, $action) = explode(self::$actionDelimiter, $actionKey);
44  return ['resource' => $resource, 'context' => $context, 'action' => $action];
45  }
46 
47  /**
48  * Get an action key that matches a given combination of resource, context, action best.
49  * @param $actionKeyProvider ActionKeyProvider instance used to search action keys
50  * @param $resource The given resource
51  * @param $context The given context
52  * @param $action The given action
53  * @return The best matching key or an empty string if nothing matches.
54  */
55  public static function getBestMatch(ActionKeyProvider $actionKeyProvider, $resource, $context, $action) {
56  $hasResource = strlen($resource) > 0;
57  $hasContext = strlen($context) > 0;
58  $hasAction = strlen($action) > 0;
59 
60  // check resource?context?action
61  if ($hasResource && $hasContext && $hasAction) {
62  $key = self::createKey($resource, $context, $action);
63  if ($actionKeyProvider->containsKey($key)) {
64  return $key;
65  }
66  }
67 
68  // check resource??action
69  if ($hasResource && $hasAction) {
70  $key = self::createKey($resource, '', $action);
71  if ($actionKeyProvider->containsKey($key)) {
72  return $key;
73  }
74  }
75 
76  // check resource?context?
77  if ($hasResource && $hasContext) {
78  $key = self::createKey($resource, $context, '');
79  if ($actionKeyProvider->containsKey($key)) {
80  return $key;
81  }
82  }
83 
84  // check ?context?action
85  if ($hasContext && $hasAction) {
86  $key = self::createKey('', $context, $action);
87  if ($actionKeyProvider->containsKey($key)) {
88  return $key;
89  }
90  }
91 
92  // check ??action
93  if ($hasAction) {
94  $key = self::createKey('', '', $action);
95  if ($actionKeyProvider->containsKey($key)) {
96  return $key;
97  }
98  }
99 
100  // check resource??
101  if ($hasResource) {
102  $key = self::createKey($resource, '', '');
103  if ($actionKeyProvider->containsKey($key)) {
104  return $key;
105  }
106  }
107 
108  // check ?context?
109  if ($hasContext) {
110  $key = self::createKey('', $context, '');
111  if ($actionKeyProvider->containsKey($key)) {
112  return $key;
113  }
114  }
115 
116  // check ??
117  $key = self::createKey('', '', '');
118  if ($actionKeyProvider->containsKey($key)) {
119  return $key;
120  }
121 
122  // no key found for requested key
123  return '';
124  }
125 }
126 ?>
containsKey($actionKey)
Check if the given action key is existing.
static getBestMatch(ActionKeyProvider $actionKeyProvider, $resource, $context, $action)
Get an action key that matches a given combination of resource, context, action best.
Definition: ActionKey.php:55
static parseKey($actionKey)
Parse an action.
Definition: ActionKey.php:42
Implementations of ActionKeyProvider search for action keys.
static createKey($resource, $context, $action)
Create an action key from the given values.
Definition: ActionKey.php:33
Configuration related interfaces and classes.
Definition: namespaces.php:6
An action key is a combination of a resource, context and action that is represented as a string.
Definition: ActionKey.php:22