Skip to content

Commit 5b1c520

Browse files
committed
Converted The whole library from js to php
1 parent e82a6bf commit 5b1c520

11 files changed

+407
-16
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
}
99
},
1010
"require": {
11-
"php": ">=8.0.0"
11+
"php": ">=8.0.0",
12+
"ext-mbstring": "*"
1213
},
1314
"authors": [
1415
{

src/Functionality.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Functionality\FunctionalityPhp;
44

55

6+
use Functionality\FunctionalityPhp\Functions\Arrays;
7+
use Functionality\FunctionalityPhp\Functions\Numbers;
68
use Functionality\FunctionalityPhp\Functions\Randoms;
9+
use Functionality\FunctionalityPhp\Functions\Strings;
710

811
class Functionality
912
{
10-
use Randoms;
13+
use Randoms, Numbers, Strings, Arrays;
1114
}

src/Functions/Arrays.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
4+
namespace Functionality\FunctionalityPhp\Functions;
5+
6+
7+
trait Arrays
8+
{
9+
/**
10+
* Filter an array by a callback.
11+
*
12+
* @param array $array
13+
* @param callable $callback
14+
* @return array
15+
*/
16+
public static function filter(array $array, callable $callback): array
17+
{
18+
return array_values(array_filter($array, $callback));
19+
}
20+
21+
/**
22+
* returns the factors of the given number.
23+
*
24+
* @param int $number
25+
* @return array
26+
*/
27+
public static function getFactors(int $number): array
28+
{
29+
$factors = [];
30+
for ($i = 1; $i <= $number; $i++) {
31+
if ($number % $i === 0) {
32+
$factors[] = $i;
33+
}
34+
}
35+
return $factors;
36+
}
37+
38+
/**
39+
* Get maximum value from an array.
40+
*
41+
* @param array $array
42+
* @return float
43+
*/
44+
public static function maxArray(array $array): float
45+
{
46+
return max($array);
47+
}
48+
49+
/**
50+
* Get minimum value from an array.
51+
*
52+
* @param array $array
53+
* @return float
54+
*/
55+
public static function minArray(array $array): float
56+
{
57+
return min($array);
58+
}
59+
60+
/**
61+
* Get random value from an array.
62+
*
63+
* @param array $array
64+
* @return mixed
65+
*/
66+
public static function randomElementFromArray(array $array): mixed
67+
{
68+
return $array[array_rand($array)];
69+
}
70+
71+
/**
72+
* removes all duplicate values from an array.
73+
*
74+
* @param array $array
75+
* @return array
76+
*/
77+
public static function removeDuplicatesFromArray(array $array): array
78+
{
79+
return array_values(array_unique($array));
80+
}
81+
82+
/**
83+
* Sort an array randomly.
84+
*
85+
* @param array $array
86+
* @return array
87+
*/
88+
public static function shuffle(array $array): array
89+
{
90+
shuffle($array);
91+
return $array;
92+
}
93+
94+
/**
95+
* Get the sum of all values in an array.
96+
*
97+
* @param array $array
98+
* @return float
99+
*/
100+
public static function sumArray(array $array): float
101+
{
102+
return array_sum($array);
103+
}
104+
}

src/Functions/Numbers.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
4+
namespace Functionality\FunctionalityPhp\Functions;
5+
6+
7+
trait Numbers
8+
{
9+
/**float
10+
* Get average between two numbers.
11+
*
12+
* @return float
13+
*/
14+
public static function getAvg(float $a, float $b): float
15+
{
16+
return ($a + $b) / 2;
17+
}
18+
19+
/**
20+
* Check if a number is even.
21+
*
22+
* @param int $number
23+
* @return bool
24+
*/
25+
public static function isEven(float $number): bool
26+
{
27+
return $number % 2 === 0;
28+
}
29+
30+
/**
31+
* Check if a number is odd.
32+
*
33+
* @param int $number
34+
* @return bool
35+
*/
36+
public static function isOdd(float $number): bool
37+
{
38+
return $number % 2 !== 0;
39+
}
40+
41+
/**
42+
* Check if a number is prime.
43+
*
44+
* @param int $number
45+
* @return bool
46+
*/
47+
public static function isPrime(int $number): bool
48+
{
49+
if ($number < 2) {
50+
return false;
51+
}
52+
for ($i = 2; $i < $number; $i++) {
53+
if ($number % $i === 0) {
54+
return false;
55+
}
56+
}
57+
return true;
58+
}
59+
60+
/**
61+
* Reverse a number (e.g. 12345 -> 54321).
62+
*
63+
* @param float $number
64+
* @return float
65+
*/
66+
public static function reverseNumber(float $number): float
67+
{
68+
return $number > 0 ? +strrev((string) $number) : -str_replace('-', '', strrev((string) $number));
69+
}
70+
}

src/Functions/Randoms.php

+90-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,96 @@
66

77
trait Randoms
88
{
9-
public static function randomBool(): bool
9+
/**
10+
* Generates a random boolean.
11+
*
12+
* @return bool
13+
*/
14+
public static function randomBoolean(): bool
1015
{
11-
return (bool) rand(0, 1);
16+
return (bool)rand(0, 1);
1217
}
18+
19+
public static function randomColor(): string
20+
{
21+
$CSS_COLOR_NAMES = [
22+
'AliceBlue', 'AntiqueWhite', 'Aqua', 'Aquamarine', 'Azure',
23+
'Beige', 'Bisque', 'Black', 'BlanchedAlmond', 'Blue',
24+
'BlueViolet', 'Brown', 'BurlyWood', 'CadetBlue', 'Chartreuse',
25+
'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson',
26+
'Cyan', 'DarkBlue', 'DarkCyan', 'DarkGoldenRod', 'DarkGray',
27+
'DarkGrey', 'DarkGreen', 'DarkKhaki', 'DarkMagenta', 'DarkOliveGreen',
28+
'DarkOrange', 'DarkOrchid', 'DarkRed', 'DarkSalmon', 'DarkSeaGreen',
29+
'DarkSlateBlue', 'DarkSlateGray', 'DarkSlateGrey', 'DarkTurquoise',
30+
'DarkViolet', 'DeepPink', 'DeepSkyBlue', 'DimGray', 'DimGrey',
31+
'DodgerBlue', 'FireBrick', 'FloralWhite', 'ForestGreen', 'Fuchsia',
32+
'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod', 'Gray',
33+
'Grey', 'Green', 'GreenYellow', 'HoneyDew', 'HotPink',
34+
'IndianRed', 'Indigo', 'Ivory', 'Khaki', 'Lavender',
35+
'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue',
36+
'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray',
37+
'LightGrey', 'LightGreen', 'LightPink', 'LightSalmon',
38+
'LightSeaGreen', 'LightSkyBlue', 'LightSlateGray', 'LightSlateGrey',
39+
'LightSteelBlue', 'LightYellow', 'Lime', 'LimeGreen', 'Linen',
40+
'Magenta', 'Maroon', 'MediumAquaMarine', 'MediumBlue',
41+
'MediumOrchid', 'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue',
42+
'MediumSpringGreen', 'MediumTurquoise', 'MediumVioletRed',
43+
];
44+
return $CSS_COLOR_NAMES[array_rand($CSS_COLOR_NAMES)];
45+
}
46+
47+
/**
48+
* Generates a random color in HEX format.
49+
*
50+
* @return string
51+
*/
52+
public static function randomHexColor(): string
53+
{
54+
return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
55+
}
56+
57+
/**
58+
* Generates a random HSL color.
59+
*
60+
* @return string
61+
*/
62+
public static function randomHsl(): string
63+
{
64+
$h = rand(0, 360);
65+
$s = rand(0, 100);
66+
$l = rand(0, 100);
67+
return "hsl($h, $s%, $l%)";
68+
}
69+
70+
/**
71+
* Generates a random integer.
72+
*
73+
* @param int $min
74+
* @param int $max
75+
* @return int
76+
*/
77+
public static function randomNumber($min = 0, $max = 100): int
78+
{
79+
return rand($min, $max);
80+
}
81+
82+
/**
83+
* Generates a random string.
84+
*
85+
* @param int $length
86+
* @return string
87+
*/
88+
public static function randomString($length = 8): string
89+
{
90+
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
91+
$count = mb_strlen($chars);
92+
93+
for ($i = 0, $result = ''; $i < $length; $i++) {
94+
$index = rand(0, $count - 1);
95+
$result .= mb_substr($chars, $index, 1);
96+
}
97+
98+
return $result;
99+
}
100+
13101
}

src/Functions/Strings.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
4+
namespace Functionality\FunctionalityPhp\Functions;
5+
6+
7+
trait Strings
8+
{
9+
/**
10+
* Check if a string is a palindrome.
11+
*
12+
* @param string $string
13+
* @return bool
14+
*/
15+
public static function isPalindrome(string $string): bool
16+
{
17+
return $string === strrev($string);
18+
}
19+
20+
/**
21+
* Capitalize first letter of a string.
22+
*
23+
* @param string $string
24+
* @return string
25+
*/
26+
public static function capitalize(string $string): string
27+
{
28+
return ucwords($string);
29+
}
30+
31+
/**
32+
* Reverse a string.
33+
*
34+
* @param string $string
35+
* @return string
36+
*/
37+
public static function reverseString(string $string): string
38+
{
39+
return strrev($string);
40+
}
41+
}

tests/ArraysTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
4+
use Functionality\FunctionalityPhp\Functionality;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class ArraysTest extends TestCase
8+
{
9+
public function testClassConstructor()
10+
{
11+
// filter test
12+
$this->assertEquals([3,4,5,6], Functionality::filter([1,3,4,5,6,0], function ($value) {
13+
return $value > 2;
14+
}));
15+
// max test
16+
$this->assertEquals(10, Functionality::maxArray([1,2,3,4,5,6,7,8,9,10]));
17+
// min test
18+
$this->assertEquals(1, Functionality::minArray([1,2,3,4,5,6,7,8,9,10]));
19+
// random element from array test
20+
$this->assertIsInt(Functionality::randomElementFromArray([1,2,3,4,5,6,7,8,9,10]));
21+
// remove duplicates from array test
22+
$this->assertEquals([1,2,3,4,5,6,7,8,9,10], Functionality::removeDuplicatesFromArray([1,1,2,2,3,3,3,3,4,5,6,7,8,9,10]));
23+
// shuffle test
24+
$this->assertIsArray(Functionality::shuffle([1,2,3,4,5,6,7,8,9,10]));
25+
// sum test
26+
$this->assertEquals(55, Functionality::sumArray([1,2,3,4,5,6,7,8,9,10]));
27+
28+
}
29+
}

tests/NumbersTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
4+
use Functionality\FunctionalityPhp\Functionality;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class NumbersTest extends TestCase
8+
{
9+
public function testClassConstructor()
10+
{
11+
// get avg test
12+
$this->assertIsFloat(Functionality::getAvg(1.5, 2.8));
13+
// is even test
14+
$this->assertTrue(Functionality::isEven(2));
15+
$this->assertFalse(Functionality::isEven(3));
16+
// is odd test
17+
$this->assertTrue(Functionality::isOdd(3));
18+
$this->assertFalse(Functionality::isOdd(2));
19+
// reverse number test
20+
$this->assertEquals(54.321, Functionality::reverseNumber(123.45));
21+
$this->assertEquals(-54.321, Functionality::reverseNumber(-123.45));
22+
}
23+
}

0 commit comments

Comments
 (0)