-
Notifications
You must be signed in to change notification settings - Fork 611
/
StripTags.php
65 lines (59 loc) · 1.64 KB
/
StripTags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\gql\directives;
use craft\gql\base\Directive;
use craft\gql\GqlEntityRegistry;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Type\Definition\Directive as GqlDirective;
use GraphQL\Type\Definition\FieldArgument;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
/**
* StripTags GraphQL Directive
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.5.4
*/
class StripTags extends Directive
{
/**
* @inheritdoc
*/
public static function create(): GqlDirective
{
$typeName = static::name();
return GqlEntityRegistry::getOrCreate($typeName, fn() => new self([
'name' => $typeName,
'locations' => [
DirectiveLocation::FIELD,
],
'description' => 'Strips HTML tags from the field value.',
'args' => [
new FieldArgument([
'name' => 'allowed',
'type' => Type::listOf(Type::string()),
'defaultValue' => [],
'description' => 'List of allowed tag names.',
]),
],
]));
}
/**
* @inheritdoc
*/
public static function name(): string
{
return 'stripTags';
}
/**
* @inheritdoc
*/
public static function apply(mixed $source, mixed $value, array $arguments, ResolveInfo $resolveInfo): mixed
{
return strip_tags((string)$value, $arguments['allowed'] ?? null);
}
}