Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Permutation
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 permut
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 isPermutation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isPalindrome
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 permutate
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Utils
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Utils;
16
17/**
18 * String utils.
19 *
20 * @package phpOMS\Utils
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25final class Permutation
26{
27    /**
28     * Constructor.
29     *
30     * @since 1.0.0
31     * @codeCoverageIgnore
32     */
33    private function __construct()
34    {
35    }
36
37    /**
38     * Create all permutations.
39     *
40     * @param array $toPermute data to permutate
41     * @param array $result    existing permutations
42     *
43     * @return array<array|string>
44     *
45     * @since 1.0.0
46     */
47    public static function permut(array $toPermute, array $result = [], bool $concat = true) : array
48    {
49        $permutations = [];
50
51        if (empty($toPermute)) {
52            $permutations[] = $concat ? \implode('', $result) : $result;
53        } else {
54            foreach ($toPermute as $key => $val) {
55                $newArr   = $toPermute;
56                $newres   = $result;
57                $newres[] = $val;
58
59                unset($newArr[$key]);
60
61                $permutations = \array_merge($permutations, self::permut($newArr, $newres, $concat));
62            }
63        }
64
65        return $permutations;
66    }
67
68    /**
69     * Check if two strings are permutations of each other.
70     *
71     * @param string $a String a
72     * @param string $b String b
73     *
74     * @return bool
75     *
76     * @since 1.0.0
77     */
78    public static function isPermutation(string $a, string $b) : bool
79    {
80        return \count_chars($a, 1) === \count_chars($b, 1);
81    }
82
83    /**
84     * Check if a string is a palindrome.
85     *
86     * @param string $a      String a
87     * @param string $filter Characters to filter
88     *
89     * @return bool
90     *
91     * @since 1.0.0
92     */
93    public static function isPalindrome(string $a, string $filter = 'a-zA-Z0-9') : bool
94    {
95        $a = \strtolower(\preg_replace('/[^' . $filter . ']/', '', $a) ?? '');
96
97        return $a === \strrev($a);
98    }
99
100    /**
101     * Permutate based on transposition key.
102     *
103     * @param array|string $toPermute To permutate
104     * @param int[]        $key       Permutation keys
105     *
106     * @return string|array
107     *
108     * @throws \OutOfBoundsException This exception is thrown if the permutation key is larger than the data to permute
109     *
110     * @since 1.0.0
111     */
112    public static function permutate(string | array $toPermute, array $key) : string | array
113    {
114        $length = \is_array($toPermute) ? \count($toPermute) : \strlen($toPermute);
115
116        if (\count($key) > $length) {
117            throw new \OutOfBoundsException('There must not be more keys than permutation elements.');
118        }
119
120        $i = 0;
121        foreach ($key as $pos) {
122            $temp                = $toPermute[$i];
123            $toPermute[$i]       = $toPermute[$pos - 1];
124            $toPermute[$pos - 1] = $temp;
125            ++$i;
126        }
127
128        return $toPermute;
129    }
130}