• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

selamiphp / stdlib / 71

pending completion
71

push

travis-ci-com

mkorkmaz
fix: composer allowed plugins

229 of 281 relevant lines covered (81.49%)

7.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Arrays/PayloadSanitizer.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Selami\Stdlib\Arrays;
6

7
use function array_key_exists;
8
use function filter_var;
9
use function is_array;
10
use function is_int;
11
use function is_string;
12
use function password_hash;
13

14
use const FILTER_FLAG_NO_ENCODE_QUOTES;
15
use const FILTER_SANITIZE_EMAIL;
16
use const FILTER_SANITIZE_STRING;
17
use const PASSWORD_ARGON2ID;
18

19
class PayloadSanitizer
20
{
21
    public static function sanitize(?array $payload, ?array $allowHtml = [], ?string $keyValue = '$'): array
22
    {
23
        if ($payload === null) {
×
24
            return [];
×
25
        }
26

27
        $sanitizedPayload = [];
×
28
        foreach ($payload as $key => $value) {
×
29
            if (is_string($key)) {
×
30
                $key = filter_var($key, FILTER_SANITIZE_STRING);
×
31
            }
32

33
            $currentKeyValue = $keyValue . '.' . $key;
×
34
            if (array_key_exists($currentKeyValue, $allowHtml)) {
×
35
                $sanitizedPayload[$key] = TagAndAttributeRemover::cleanHtml(
×
36
                    $value,
37
                    $allowHtml[$currentKeyValue]['allowedTags'],
×
38
                    $allowHtml[$currentKeyValue]['urlPrefixes'] ?? 'https'
×
39
                );
40
                continue;
×
41
            }
42

43
            if (is_int($value)) {
×
44
                $sanitizedPayload[$key] =  $value;
×
45
                continue;
×
46
            }
47

48
            if ($key === 'email') {
×
49
                $sanitizedPayload[$key] =  filter_var($value, FILTER_SANITIZE_EMAIL);
×
50
                continue;
×
51
            }
52

53
            if ($key === 'password') {
×
54
                $sanitizedPayload['passwordHash']     =  password_hash($payload['password'], PASSWORD_ARGON2ID);
×
55
                $sanitizedPayload['passwordHashAlgo'] = PASSWORD_ARGON2ID;
×
56
                continue;
×
57
            }
58

59
            if (is_string($value)) {
×
60
                $sanitizedPayload[$key] =  filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
×
61
                continue;
×
62
            }
63

64
            if (is_array($value)) {
×
65
                $sanitizedPayload[$key] =  self::sanitize($value, $allowHtml, $keyValue . '.' . $key);
×
66
                continue;
×
67
            }
68

69
            $sanitizedPayload[$key] = $value;
×
70
        }
71

72
        return $sanitizedPayload;
×
73
    }
74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc