ActionKey.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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 array('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  $result = null;
57  $hasResource = strlen($resource) > 0;
58  $hasContext = strlen($context) > 0;
59  $hasAction = strlen($action) > 0;
60 
61  // check resource?context?action
62  if ($hasResource && $hasContext && $hasAction) {
63  $key = self::createKey($resource, $context, $action);
64  if ($actionKeyProvider->containsKey($key)) {
65  return $key;
66  }
67  }
68 
69  // check resource??action
70  if ($hasResource && $hasAction) {
71  $key = self::createKey($resource, '', $action);
72  if ($actionKeyProvider->containsKey($key)) {
73  return $key;
74  }
75  }
76 
77  // check resource?context?
78  if ($hasResource && $hasContext) {
79  $key = self::createKey($resource, $context, '');
80  if ($actionKeyProvider->containsKey($key)) {
81  return $key;
82  }
83  }
84 
85  // check ?context?action
86  if ($hasContext && $hasAction) {
87  $key = self::createKey('', $context, $action);
88  if ($actionKeyProvider->containsKey($key)) {
89  return $key;
90  }
91  }
92 
93  // check ??action
94  if ($hasAction) {
95  $key = self::createKey('', '', $action);
96  if ($actionKeyProvider->containsKey($key)) {
97  return $key;
98  }
99  }
100 
101  // check resource??
102  if ($hasResource) {
103  $key = self::createKey($resource, '', '');
104  if ($actionKeyProvider->containsKey($key)) {
105  return $key;
106  }
107  }
108 
109  // check ?context?
110  if ($hasContext) {
111  $key = self::createKey('', $context, '');
112  if ($actionKeyProvider->containsKey($key)) {
113  return $key;
114  }
115  }
116 
117  // check ??
118  $key = self::createKey('', '', '');
119  if ($actionKeyProvider->containsKey($key)) {
120  return $key;
121  }
122 
123  // no key found for requested key
124  return '';
125  }
126 }
127 ?>
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
Implementations of ActionKeyProvider search for action keys.
Configuration related interfaces and classes.
Definition: namespaces.php:6
static parseKey($actionKey)
Parse an action.
Definition: ActionKey.php:42
static createKey($resource, $context, $action)
Create an action key from the given values.
Definition: ActionKey.php:33
containsKey($actionKey)
Check if the given action key is existing.
An action key is a combination of a resource, context and action that is represented as a string...
Definition: ActionKey.php:22