| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * ObjectMapHandler.php | |
| 4 | + * | |
| 5 | + * The ObjectMapHandler class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Object; |
| @@ -6,26 +19,64 @@ | ||
| 6 | 19 | |
| 7 | 20 | use UserAccessManager\Cache\Cache; |
| 8 | 21 | use UserAccessManager\Database\Database; |
| 9 | 22 | |
| 23 | +/** | |
| 24 | + * Class ObjectMapHandler | |
| 25 | + * | |
| 26 | + * @package UserAccessManager\Object | |
| 27 | + */ | |
| 10 | 28 | class ObjectMapHandler |
| 11 | 29 | { |
| 12 | - public const TREE_MAP_PARENTS = 'PARENT'; | |
| 13 | - public const TREE_MAP_CHILDREN = 'CHILDREN'; | |
| 14 | - public const POST_TREE_MAP_CACHE_KEY = 'uamPostTreeMap'; | |
| 15 | - public const TERM_TREE_MAP_CACHE_KEY = 'uamTermTreeMap'; | |
| 16 | - public const TERM_POST_MAP_CACHE_KEY = 'uamTermPostMap'; | |
| 17 | - public const POST_TERM_MAP_CACHE_KEY = 'uamPostTermMap'; | |
| 30 | + const TREE_MAP_PARENTS = 'PARENT'; | |
| 31 | + const TREE_MAP_CHILDREN = 'CHILDREN'; | |
| 32 | + const POST_TREE_MAP_CACHE_KEY = 'uamPostTreeMap'; | |
| 33 | + const TERM_TREE_MAP_CACHE_KEY = 'uamTermTreeMap'; | |
| 34 | + const TERM_POST_MAP_CACHE_KEY = 'uamTermPostMap'; | |
| 35 | + const POST_TERM_MAP_CACHE_KEY = 'uamPostTermMap'; | |
| 18 | 36 | |
| 19 | - private ?array $termPostMap = null; | |
| 20 | - private ?array $postTermMap = null; | |
| 21 | - private ?array $termTreeMap = null; | |
| 22 | - private ?array $postTreeMap = null; | |
| 37 | + /** | |
| 38 | + * @var Database | |
| 39 | + */ | |
| 40 | + private $database; | |
| 23 | 41 | |
| 42 | + /** | |
| 43 | + * @var Cache | |
| 44 | + */ | |
| 45 | + private $cache; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * @var null|array | |
| 49 | + */ | |
| 50 | + private $termPostMap = null; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * @var null|array | |
| 54 | + */ | |
| 55 | + private $postTermMap = null; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @var null|array | |
| 59 | + */ | |
| 60 | + private $termTreeMap = null; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * @var null|array | |
| 64 | + */ | |
| 65 | + private $postTreeMap = null; | |
| 66 | + | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * ObjectMapHandler constructor. | |
| 70 | + * @param Database $database | |
| 71 | + * @param Cache $cache | |
| 72 | + */ | |
| 24 | 73 | public function __construct( |
| 25 | - private Database $database, | |
| 26 | - private Cache $cache | |
| 74 | + Database $database, | |
| 75 | + Cache $cache | |
| 27 | 76 | ) { |
| 77 | + $this->database = $database; | |
| 78 | + $this->cache = $cache; | |
| 28 | 79 | } |
| 29 | 80 | |
| 30 | 81 | /** |
| 31 | 82 | * Resolves all tree map elements |
| @@ -33,9 +84,9 @@ | ||
| 33 | 84 | * @param array|null $subMap |
| 34 | 85 | * @param array $processed |
| 35 | 86 | * @return array |
| 36 | 87 | */ |
| 37 | - private function processTreeMapElements(array &$map, ?array $subMap = null, array &$processed = []): array | |
| 88 | + private function processTreeMapElements(array &$map, array $subMap = null, array &$processed = []): array | |
| 38 | 89 | { |
| 39 | 90 | $processMap = ($subMap === null) ? $map : $subMap; |
| 40 | 91 | |
| 41 | 92 | foreach ($processMap as $id => $subIds) { |
| @@ -49,8 +100,14 @@ | ||
| 49 | 100 | |
| 50 | 101 | return $map; |
| 51 | 102 | } |
| 52 | 103 | |
| 104 | + /** | |
| 105 | + * Returns the tree map for the query. | |
| 106 | + * @param string $select | |
| 107 | + * @param string $generalType | |
| 108 | + * @return array | |
| 109 | + */ | |
| 53 | 110 | private function getTreeMap(string $select, string $generalType): array |
| 54 | 111 | { |
| 55 | 112 | $treeMap = [ |
| 56 | 113 | self::TREE_MAP_CHILDREN => [ |
| @@ -71,9 +128,9 @@ | ||
| 71 | 128 | |
| 72 | 129 | //Process elements |
| 73 | 130 | foreach ($treeMap as $mapType => $mayTypeMap) { |
| 74 | 131 | foreach ($mayTypeMap as $objectType => $map) { |
| 75 | - $treeMap[$mapType][(string) $objectType] = $this->processTreeMapElements($map); | |
| 132 | + $treeMap[$mapType][$objectType] = $this->processTreeMapElements($map); | |
| 76 | 133 | } |
| 77 | 134 | } |
| 78 | 135 | |
| 79 | 136 | return $treeMap; |
| @@ -78,8 +135,15 @@ | ||
| 78 | 135 | |
| 79 | 136 | return $treeMap; |
| 80 | 137 | } |
| 81 | 138 | |
| 139 | + /** | |
| 140 | + * Checks if a cache key exists and returns the map. | |
| 141 | + * @param string $cacheKey | |
| 142 | + * @param string $generalType | |
| 143 | + * @param string $query | |
| 144 | + * @return array | |
| 145 | + */ | |
| 82 | 146 | private function getCachedTreeMap(string $cacheKey, string $generalType, string $query): array |
| 83 | 147 | { |
| 84 | 148 | $map = $this->cache->get($cacheKey); |
| 85 | 149 | |
| @@ -91,8 +155,12 @@ | ||
| 91 | 155 | |
| 92 | 156 | return $map; |
| 93 | 157 | } |
| 94 | 158 | |
| 159 | + /** | |
| 160 | + * Returns the post tree map. | |
| 161 | + * @return array | |
| 162 | + */ | |
| 95 | 163 | public function getPostTreeMap(): ?array |
| 96 | 164 | { |
| 97 | 165 | if ($this->postTreeMap === null) { |
| 98 | 166 | $query = "SELECT ID AS id, post_parent AS parentId, post_type AS type |
| @@ -108,8 +176,12 @@ | ||
| 108 | 176 | |
| 109 | 177 | return $this->postTreeMap; |
| 110 | 178 | } |
| 111 | 179 | |
| 180 | + /** | |
| 181 | + * Returns the term tree map. | |
| 182 | + * @return array | |
| 183 | + */ | |
| 112 | 184 | public function getTermTreeMap(): ?array |
| 113 | 185 | { |
| 114 | 186 | if ($this->termTreeMap === null) { |
| 115 | 187 | $query = "SELECT term_id AS id, parent AS parentId, taxonomy AS type |
| @@ -125,8 +197,14 @@ | ||
| 125 | 197 | |
| 126 | 198 | return $this->termTreeMap; |
| 127 | 199 | } |
| 128 | 200 | |
| 201 | + /** | |
| 202 | + * Returns the cached map. | |
| 203 | + * @param string $cacheKey | |
| 204 | + * @param string $query | |
| 205 | + * @return array | |
| 206 | + */ | |
| 129 | 207 | private function getCachedMap(string $cacheKey, string $query): array |
| 130 | 208 | { |
| 131 | 209 | $map = $this->cache->get($cacheKey); |
| 132 | 210 | |
| @@ -143,8 +221,12 @@ | ||
| 143 | 221 | |
| 144 | 222 | return $map; |
| 145 | 223 | } |
| 146 | 224 | |
| 225 | + /** | |
| 226 | + * Returns the term post map. | |
| 227 | + * @return array | |
| 228 | + */ | |
| 147 | 229 | public function getTermPostMap(): ?array |
| 148 | 230 | { |
| 149 | 231 | if ($this->termPostMap === null) { |
| 150 | 232 | $select = " |
| @@ -160,8 +242,12 @@ | ||
| 160 | 242 | |
| 161 | 243 | return $this->termPostMap; |
| 162 | 244 | } |
| 163 | 245 | |
| 246 | + /** | |
| 247 | + * Returns the post term map. | |
| 248 | + * @return array | |
| 249 | + */ | |
| 164 | 250 | public function getPostTermMap(): ?array |
| 165 | 251 | { |
| 166 | 252 | if ($this->postTermMap === null) { |
| 167 | 253 | $select = " |