From f3f98a117cf55be85e0078766fac6879e914256e Mon Sep 17 00:00:00 2001 From: Bocki Date: Thu, 2 Mar 2023 13:25:57 +0100 Subject: [PATCH] [Core] Add getKey function (#3275) * [Core] Add getKey function --- bridges/JustWatchBridge.php | 18 +----------------- lib/BridgeAbstract.php | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/bridges/JustWatchBridge.php b/bridges/JustWatchBridge.php index 7aa68de3..59e60a16 100644 --- a/bridges/JustWatchBridge.php +++ b/bridges/JustWatchBridge.php @@ -204,26 +204,10 @@ class JustWatchBridge extends BridgeAbstract return 'https://www.justwatch.com/' . $this->getInput('country'); } - protected function getKeyName($input = '') - { - $parameters = $this->getParameters(); - if (strlen($input) < 2) { - return array_search((int)$input, $parameters[0]['mediatype']['values'], true); - } else { - $returnkey = ''; - foreach ($parameters[0]['country']['values'] as $valuearray) { - if (strlen($returnkey) < 2) { - $returnkey = array_search($input, $valuearray, true); - } - } - return $returnkey; - } - } - public function getName() { if (!is_null($this->getInput('country'))) { - return 'JustWatch - ' . $this->getKeyName($this->getInput('country')) . ' - ' . $this->getKeyName($this->getInput('mediatype')); + return 'JustWatch - ' . $this->getKey('country') . ' - ' . $this->getKey('mediatype'); } return parent::getName(); } diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index 5752f55b..ecbb2529 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -294,6 +294,32 @@ abstract class BridgeAbstract implements BridgeInterface return $this->inputs[$this->queriedContext][$input]['value']; } + /** + * Get the key name of a given input + * Can process multilevel arrays with two levels, the max level a list can have + * + * @param string $input The input name + * @return string|null The accompaning key to a given input or null if the input is not defined + */ + public function getKey($input) + { + if (!isset($this->inputs[$this->queriedContext][$input]['value'])) { + return null; + } + $needle = $this->inputs[$this->queriedContext][$input]['value']; + foreach (static::PARAMETERS[$this->queriedContext][$input]['values'] as $first_level_key => $first_level_value) { + if ($needle === (string)$first_level_value) { + return $first_level_key; + } elseif (is_array($first_level_value)) { + foreach ($first_level_value as $second_level_key => $second_level_value) { + if ($needle === (string)$second_level_value) { + return $second_level_key; + } + } + } + } + } + /** * Get bridge configuration value */