Skip to content

Commit

Permalink
[Various] getKey replacements and docu (#3283)
Browse files Browse the repository at this point in the history
* [Various] getKey replacements and docu

* more bridges and fix to the abstract

* linting

* revert bandcampdaily. doing more than i thought
  • Loading branch information
Bockiii authored Mar 6, 2023
1 parent b407140 commit f0e5ef0
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 100 deletions.
10 changes: 1 addition & 9 deletions bridges/AllocineFRBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ private function getLastSeasonURI($category)
public function getName()
{
if (!is_null($this->getInput('category'))) {
return self::NAME . ' : '
. array_search(
$this->getInput('category'),
self::PARAMETERS[$this->queriedContext]['category']['values']
);
return self::NAME . ' : ' . $this->getKey('category');
}

return parent::getName();
Expand All @@ -89,10 +85,6 @@ public function collectData()
{
$html = getSimpleHTMLDOM($this->getURI());

$category = array_search(
$this->getInput('category'),
self::PARAMETERS[$this->queriedContext]['category']['values']
);
foreach ($html->find('div[class=gd-col-left]', 0)->find('div[class*=video-card]') as $element) {
$item = [];

Expand Down
12 changes: 2 additions & 10 deletions bridges/AnimeUltimeBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ class AnimeUltimeBridge extends BridgeAbstract
public function collectData()
{
//Add type filter if provided
$typeFilter = array_search(
$this->getInput('type'),
self::PARAMETERS[$this->queriedContext]['type']['values']
);
$typeFilter = $this->getKey('type');

//Build date and filters for making requests
$thismonth = date('mY') . $typeFilter;
Expand Down Expand Up @@ -128,12 +125,7 @@ public function collectData()
public function getName()
{
if (!is_null($this->getInput('type'))) {
$typeFilter = array_search(
$this->getInput('type'),
self::PARAMETERS[$this->queriedContext]['type']['values']
);

return 'Latest ' . $typeFilter . ' - Anime-Ultime Bridge';
return 'Latest ' . $this->getKey('type') . ' - Anime-Ultime Bridge';
}

return parent::getName();
Expand Down
9 changes: 2 additions & 7 deletions bridges/BrutBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,8 @@ public function getURI()
public function getName()
{
if (!is_null($this->getInput('edition')) && !is_null($this->getInput('category'))) {
$parameters = $this->getParameters();

$editionValues = array_flip($parameters[0]['edition']['values']);
$categoryValues = array_flip($parameters[0]['category']['values']);

return $categoryValues[$this->getInput('category')] . ' - ' .
$editionValues[$this->getInput('edition')] . ' - Brut.';
return $this->getKey('category') . ' - ' .
$this->getKey('edition') . ' - Brut.';
}

return parent::getName();
Expand Down
6 changes: 1 addition & 5 deletions bridges/GBAtempBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,7 @@ private function findItemImage($item, $selector)
public function getName()
{
if (!is_null($this->getInput('type'))) {
$type = array_search(
$this->getInput('type'),
self::PARAMETERS[$this->queriedContext]['type']['values']
);
return 'GBAtemp ' . $type . ' Bridge';
return 'GBAtemp ' . $this->getKey('type') . ' Bridge';
}

return parent::getName();
Expand Down
3 changes: 1 addition & 2 deletions bridges/GithubTrendingBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,7 @@ public function collectData()
public function getName()
{
if (!is_null($this->getInput('language'))) {
$language = array_search($this->getInput('language'), self::PARAMETERS['By language']['language']['values']);
return self::NAME . ': ' . $language;
return self::NAME . ': ' . $this->getKey('language');
}

return parent::getName();
Expand Down
4 changes: 1 addition & 3 deletions bridges/InternetArchiveBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ public function getURI()
public function getName()
{
if (!is_null($this->getInput('username')) && !is_null($this->getInput('content'))) {
$contentValues = array_flip(self::PARAMETERS['Account']['content']['values']);

return $contentValues[$this->getInput('content')] . ' - '
return $this->getKey('content') . ' - '
. $this->processUsername() . ' - Internet Archive';
}

Expand Down
23 changes: 3 additions & 20 deletions bridges/NineGagBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ public function collectData()
public function getName()
{
if ($this->getInput('d')) {
$name = sprintf('%s - %s', '9GAG', $this->getParameterKey('d'));
$name = sprintf('%s - %s', '9GAG', $this->getKey('d'));
} elseif ($this->getInput('g')) {
$name = sprintf('%s - %s', '9GAG', $this->getParameterKey('g'));
$name = sprintf('%s - %s', '9GAG', $this->getKey('g'));
if ($this->getInput('t')) {
$name = sprintf('%s [%s]', $name, $this->getParameterKey('t'));
$name = sprintf('%s [%s]', $name, $this->getKey('t'));
}
}
if (!empty($name)) {
Expand Down Expand Up @@ -236,23 +236,6 @@ protected function getPages()
return $this->p;
}

protected function getParameterKey($input = '')
{
$params = $this->getParameters();
$tab = 'Sections';
if ($input === 'd') {
$tab = 'Popular';
}
if (!isset($params[$tab][$input])) {
return '';
}

return array_search(
$this->getInput($input),
$params[$tab][$input]['values']
);
}

protected static function getContent($post)
{
if ($post['type'] === 'Animated') {
Expand Down
8 changes: 2 additions & 6 deletions bridges/NpciBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ class NpciBridge extends BridgeAbstract

public function getName()
{
$product = $this->getInput('product');
if ($product) {
$productNameMap = array_flip(self::PARAMETERS[0]['product']['values']);
$productName = $productNameMap[$product];
return "NPCI Circulars: $productName";
if ($this->getInput('product')) {
return 'NPCI Circulars: ' . $this->getKey('product');
}

return 'NPCI Circulars';
}

Expand Down
4 changes: 1 addition & 3 deletions bridges/PepperBridgeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,7 @@ public function getName()
return $this->i8n('bridge-name') . ' - ' . $this->i8n('title-keyword') . ' : ' . $this->getInput('q');
break;
case $this->i8n('context-group'):
$values = $this->getParameters()[$this->i8n('context-group')]['group']['values'];
$group = array_search($this->getInput('group'), $values);
return $this->i8n('bridge-name') . ' - ' . $this->i8n('title-group') . ' : ' . $group;
return $this->i8n('bridge-name') . ' - ' . $this->i8n('title-group') . ' : ' . $this->getKey('group');
break;
case $this->i8n('context-talk'):
return $this->i8n('bridge-name') . ' - ' . $this->i8n('title-talk') . ' : ' . $this->getTalkTitle();
Expand Down
7 changes: 1 addition & 6 deletions bridges/PixivBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ public function getName()
default:
return parent::getName();
}
$mode = array_search(
$this->getInput('mode'),
self::PARAMETERS['global']['mode']['values']
);
return "Pixiv ${mode} from ${context} ${query}";
return 'Pixiv ' . $this->getKey('mode') . " from ${context} ${query}";
}

public function getURI()
Expand Down Expand Up @@ -149,7 +145,6 @@ private function collectWorksArray()
public function collectData()
{
$content = $this->collectWorksArray();

$content = array_filter($content, function ($v, $k) {
return !array_key_exists('isAdContainer', $v);
}, ARRAY_FILTER_USE_BOTH);
Expand Down
6 changes: 1 addition & 5 deletions bridges/SplCenterBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ public function getURI()
public function getName()
{
if (!is_null($this->getInput('content'))) {
$parameters = $this->getParameters();

$contentValues = array_flip($parameters[0]['content']['values']);

return $contentValues[$this->getInput('content')] . ' - Southern Poverty Law Center';
return $this->getKey('content') . ' - Southern Poverty Law Center';
}

return parent::getName();
Expand Down
5 changes: 1 addition & 4 deletions bridges/TwitScoopBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ public function getURI()
public function getName()
{
if (!is_null($this->getInput('country'))) {
$parameters = $this->getParameters();
$values = array_flip($parameters[0]['country']['values']);

return $values[$this->getInput('country')] . ' - TwitScoop';
return $this->getKey('country') . ' - TwitScoop';
}

return parent::getName();
Expand Down
16 changes: 1 addition & 15 deletions bridges/UnogsBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getName()
if ($this->queriedContext == 'Global') {
$feedName .= 'Netflix Global - ';
} elseif ($this->queriedContext == 'Country') {
$feedName .= 'Netflix ' . $this->getParametersKey('country_code') . ' - ';
$feedName .= 'Netflix ' . $this->getKey('country_code') . ' - ';
}
if ($this->getInput('feed') == 'expiring') {
$feedName .= 'Expiring title';
Expand All @@ -88,20 +88,6 @@ public function getName()
return $feedName;
}

private function getParametersKey($input = '')
{
$params = $this->getParameters();
$tab = 'Country';
if (!isset($params[$tab][$input])) {
return '';
}

return array_search(
$this->getInput($input),
$params[$tab][$input]['values']
);
}

private function getJSON($url)
{
$header = [
Expand Down
5 changes: 1 addition & 4 deletions bridges/WebfailBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public function collectData()
{
$html = getSimpleHTMLDOM($this->getURI() . $this->getInput('type'));

$type = array_search(
$this->getInput('type'),
self::PARAMETERS[$this->queriedContext]['type']['values']
);
$type = $this->getKey('type');

switch (strtolower($type)) {
case 'facebook':
Expand Down
29 changes: 29 additions & 0 deletions docs/06_Helper_functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ $this->getInput('your input name here');

`getInput` will either return the value for your parameter or `null` if the parameter is unknown or not specified.

# getKey
The `getKey` function is used to receive the key name to a selected list value given the name of the list, specified in `const PARAMETERS`
Is able to work with multidimensional list arrays.

```PHP
// Given a multidimensional array like this
const PARAMETERS = [[
'country' => [
'name' => 'Country',
'type' => 'list',
'values' => [
'North America' => [
'Mexico' => 'mx',
'United States' => 'us'
],
'South America' => [
'Uruguay' => 'uy',
'Venezuela' => 've'
],
]
]
]],
// Provide the list name to the function
$this->getKey('country');
// if the selected value was "ve", this function will return "Venezuela"
```

`getKey` will either return the key name for your parameter or `null` if the parameter is unknown or not specified.

# getContents
The `getContents` function uses [cURL](https://secure.php.net/manual/en/book.curl.php) to acquire data from the specified URI while respecting the various settings defined at a global level by RSS-Bridge (i.e., proxy host, user agent, etc.). This function accepts a few parameters:

Expand Down
10 changes: 9 additions & 1 deletion lib/BridgeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,16 @@ public function getKey($input)
if (!isset($this->inputs[$this->queriedContext][$input]['value'])) {
return null;
}
if (array_key_exists('global', static::PARAMETERS)) {
if (array_key_exists($input, static::PARAMETERS['global'])) {
$context = 'global';
}
}
if (!isset($context)) {
$context = $this->queriedContext;
}
$needle = $this->inputs[$this->queriedContext][$input]['value'];
foreach (static::PARAMETERS[$this->queriedContext][$input]['values'] as $first_level_key => $first_level_value) {
foreach (static::PARAMETERS[$context][$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)) {
Expand Down

0 comments on commit f0e5ef0

Please sign in to comment.