-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
204 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,5 @@ | |
"Kerogs\\KerogsPhp\\": "src/" | ||
} | ||
}, | ||
"version": "1.3" | ||
"version": "1.4" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/* | ||
* (c) Kerogs [email protected] | ||
* | ||
* This source file is subject to the Mozilla Public License Version 2.0 license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Kerogs\KerogsPhp; | ||
|
||
class Algorithm | ||
{ | ||
public function __construct() {} | ||
|
||
|
||
/** | ||
* Returns the percentage of similarity between two strings. This function returns | ||
* the percentage of similarity as an integer between 0 and 100, where 0 means | ||
* the strings are completely different and 100 means they are identical. | ||
* | ||
* @param string $str1 | ||
* @param string $str2 | ||
* @return int | ||
*/ | ||
public function similarityPercentage($str1, $str2) | ||
{ | ||
$str1 = basename($str1); | ||
|
||
similar_text($str1, $str2, $percent); | ||
|
||
return $percent; | ||
} | ||
|
||
/** | ||
* Search engine that returns a relevance-based sorted array of values from $values that match the given query. | ||
* | ||
* @param array $values The array of values to search from. | ||
* @param string $query The query to search for. | ||
* | ||
* @return array The sorted array of values that match the query, with the highest relevance first. | ||
*/ | ||
public function searchEngine(array $values, $query) | ||
{ | ||
$result = []; | ||
|
||
$lowercaseQuery = strtolower($query); | ||
|
||
foreach ($values as $value) { | ||
$lowercaseValue = strtolower($value); | ||
|
||
$similarity = $this->similarityPercentage($lowercaseValue, $lowercaseQuery); | ||
|
||
$result[$value] = round($similarity, 2); | ||
} | ||
|
||
arsort($result); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/* | ||
* (c) Kerogs [email protected] | ||
* | ||
* This source file is subject to the Mozilla Public License Version 2.0 license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Kerogs\KerogsPhp; | ||
|
||
class Github | ||
{ | ||
private $baseUrl = 'https://api.github.com/repos/'; | ||
|
||
public function __construct() {} | ||
|
||
public function getRepositoryInfo($owner, $repo) | ||
{ | ||
$url = $this->baseUrl . "$owner/$repo"; | ||
|
||
$options = [ | ||
"http" => [ | ||
"header" => "User-Agent: $owner\r\n" | ||
] | ||
]; | ||
|
||
$context = stream_context_create($options); | ||
$response = file_get_contents($url, false, $context); | ||
|
||
if ($response === false) { | ||
return ['error' => 'Unable to fetch repository info.']; | ||
} | ||
|
||
return json_decode($response, true); | ||
} | ||
|
||
public function getLatestRelease($owner, $repo, $onlyName = true) | ||
{ | ||
$url = $this->baseUrl . "$owner/$repo/releases/latest"; | ||
|
||
$options = [ | ||
"http" => [ | ||
"header" => "User-Agent: Kerogs\r\n" | ||
] | ||
]; | ||
|
||
$context = stream_context_create($options); | ||
$response = file_get_contents($url, false, $context); | ||
|
||
if ($response === false) { | ||
return ['error' => 'Unable to fetch latest release.']; | ||
} | ||
|
||
if ($onlyName) { | ||
return json_decode($response, true)['name']; | ||
} else { | ||
return json_decode($response, true); | ||
} | ||
} | ||
|
||
public function compareVersions($versionActual, $versionLatest) | ||
{ | ||
// Retire tout ce qui se trouve après le "-" | ||
$version1 = explode('-', $versionActual)[0]; | ||
$version2 = explode('-', $versionLatest)[0]; | ||
|
||
// Convertit les versions en tableaux | ||
$v1 = array_map('intval', explode('.', $version1)); | ||
$v2 = array_map('intval', explode('.', $version2)); | ||
|
||
// Compare les versions | ||
$length = max(count($v1), count($v2)); | ||
for ($i = 0; $i < $length; $i++) { | ||
$v1Part = $v1[$i] ?? 0; // Défaut à 0 si la version n'a pas cette partie | ||
$v2Part = $v2[$i] ?? 0; // Défaut à 0 si la version n'a pas cette partie | ||
|
||
if ($v1Part < $v2Part) { | ||
return ['same' => false, 'comparison' => 'below']; | ||
} elseif ($v1Part > $v2Part) { | ||
return ['same' => false, 'comparison' => 'above']; | ||
} | ||
} | ||
|
||
return ['same' => true, 'comparison' => null]; | ||
} | ||
} |