Skip to content

Commit

Permalink
add Github && Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
kerogs committed Sep 22, 2024
1 parent 175ad21 commit eb89caf
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"Kerogs\\KerogsPhp\\": "src/"
}
},
"version": "1.3"
"version": "1.4"
}
59 changes: 57 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ encryptDecryptFile(string $filePath, bool $encrypt): void
### Kerogs\KerogsPhp\Sendmail
You can send your e-mails directly without rewriting the code.

#### Example of us
#### Example of use
```php
require_once __DIR__.'/vendor/autoload.php';

Expand All @@ -129,4 +129,59 @@ if($sendmail->sendMail(to, "Subject test", "hello world")))
else
echo "Email not sent";
```
The default content type is ``text/HTML``
The default content type is ``text/HTML``

### Kerogs\KerogsPhp\Github
allows you to retrieve information from a GITHUB repository and compare versions.

#### Function list

|function|description|
|--------|-----------|
|``Retrieves all repository info (JSON format)``|Retrieves all repository info (JSON format)|
|``getLatestRelease($owner, $repo, $onlyName = true)``|Retrieves only the name of the latest repository version (or all information on the latest release).|
|``compareVersions($versionActual, $versionLatest)``|Compares 2 versions (not only works for GITHUB) (its format must be X/X.Y/X.Y.Z/X.Y.Z.F, if there is content after a “-” it will not be taken into account). (will return ``true`` if same version (if not, will return ``above`` or ``below`` the current version.))|

#### Example of use
```php
require_once __DIR__.'/vendor/autoload.php';

use Kerogs\KerogsPhp\Github;

$github = new Github();

$lastRelease = $github->compareVersions("1.3.17", $github->getLatestRelease("KSLaboratories", "kerogsPHP", false)['name']);

if($lastRelease['same']) {
echo "KerogsPHP is up to date !";
} else{
if($lastRelease['comparison'] === 'above') {
echo "KerogsPHP is outdated ! (above)";
} else {
echo "KerogsPHP is outdated ! (below)";
}
}
```

### Kerogs\KerogsPhp\Algorithm
Allows you to manage an algorythm

#### Function list

|function|description|
|--------|-----------|
|``similarityPercentage($str1, $str2)``|Returns the similarity percentage|
|``searchEngine(array $values, $query)``|Returns an array of values. Sort from most similar to least similar|

#### Example of use
```php
require_once __DIR__.'/vendor/autoload.php';

use Kerogs\KerogsPhp\Algorithm;

$algo = new Algorithm();

$searchBanana = $algo->searchEngine(['banana', 'apple', 'orange', 'pineapple'], 'banana');

print_r($searchBanana);
```
60 changes: 60 additions & 0 deletions src/algorithm.php
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;
}
}
86 changes: 86 additions & 0 deletions src/github.php
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];
}
}

0 comments on commit eb89caf

Please sign in to comment.