Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.78% |
31 / 37 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
InputValidator | |
83.78% |
31 / 37 |
|
71.43% |
5 / 7 |
13.72 | |
0.00% |
0 / 1 |
newFromGlobalState | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
validateIDExists | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
validateLangCodeIsSupported | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
validateSchemaTextLength | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
validateAliasesLength | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
validateStringInputLength | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace EntitySchema\Presentation; |
6 | |
7 | use EntitySchema\Domain\Model\EntitySchemaId; |
8 | use InvalidArgumentException; |
9 | use MediaWiki\Config\Config; |
10 | use MediaWiki\Config\ConfigException; |
11 | use MediaWiki\Languages\LanguageNameUtils; |
12 | use MediaWiki\MediaWikiServices; |
13 | use MediaWiki\Title\Title; |
14 | use Message; |
15 | use MessageLocalizer; |
16 | use RequestContext; |
17 | |
18 | /** |
19 | * @license GPL-2.0-or-later |
20 | */ |
21 | class InputValidator { |
22 | |
23 | private MessageLocalizer $msgLocalizer; |
24 | private Config $configService; |
25 | private LanguageNameUtils $languageNameUtils; |
26 | |
27 | public static function newFromGlobalState() { |
28 | return new self( |
29 | RequestContext::getMain(), |
30 | MediaWikiServices::getInstance()->getMainConfig(), |
31 | MediaWikiServices::getInstance()->getLanguageNameUtils() |
32 | ); |
33 | } |
34 | |
35 | public function __construct( |
36 | MessageLocalizer $msgLocalizer, |
37 | Config $config, |
38 | LanguageNameUtils $languageNameUtils |
39 | ) { |
40 | $this->msgLocalizer = $msgLocalizer; |
41 | $this->configService = $config; |
42 | $this->languageNameUtils = $languageNameUtils; |
43 | } |
44 | |
45 | /** |
46 | * @param string $id |
47 | * |
48 | * @return true|Message returns true on success and Message on failure |
49 | */ |
50 | public function validateIDExists( string $id ) { |
51 | try { |
52 | $entitySchemaId = new EntitySchemaId( $id ); |
53 | } catch ( InvalidArgumentException $e ) { |
54 | return $this->msgLocalizer->msg( 'entityschema-error-invalid-id' ); |
55 | } |
56 | $title = Title::makeTitle( NS_ENTITYSCHEMA_JSON, $entitySchemaId->getId() ); |
57 | if ( !$title->exists() ) { |
58 | return $this->msgLocalizer->msg( 'entityschema-error-schemadeleted' ); |
59 | } |
60 | |
61 | return true; |
62 | } |
63 | |
64 | /** |
65 | * @param string $langCode |
66 | * |
67 | * @return true|Message returns true on success and Message on failure |
68 | */ |
69 | public function validateLangCodeIsSupported( string $langCode ) { |
70 | if ( !$this->languageNameUtils->isSupportedLanguage( $langCode ) ) { |
71 | return $this->msgLocalizer->msg( 'entityschema-error-unsupported-langcode' ); |
72 | } |
73 | return true; |
74 | } |
75 | |
76 | /** |
77 | * @param string $schemaText |
78 | * |
79 | * @return true|Message returns true on success and Message on failure |
80 | * @throws ConfigException |
81 | */ |
82 | public function validateSchemaTextLength( string $schemaText ) { |
83 | $maxLengthBytes = $this->configService->get( 'EntitySchemaSchemaTextMaxSizeBytes' ); |
84 | $schemaTextLengthBytes = strlen( $schemaText ); |
85 | if ( $schemaTextLengthBytes > $maxLengthBytes ) { |
86 | return $this->msgLocalizer->msg( 'entityschema-error-schematext-too-long' ) |
87 | ->numParams( $maxLengthBytes, $schemaTextLengthBytes ); |
88 | } |
89 | |
90 | return true; |
91 | } |
92 | |
93 | public function validateAliasesLength( string $aliasesInput ) { |
94 | $maxLengthChars = $this->configService->get( 'EntitySchemaNameBadgeMaxSizeChars' ); |
95 | $cleanAliasesString = implode( '', array_map( 'trim', explode( '|', $aliasesInput ) ) ); |
96 | $aliasesLengthChars = mb_strlen( $cleanAliasesString ); |
97 | if ( $aliasesLengthChars > $maxLengthChars ) { |
98 | return $this->msgLocalizer->msg( 'entityschema-error-input-too-long' ) |
99 | ->numParams( $maxLengthChars, $aliasesLengthChars ); |
100 | } |
101 | |
102 | return true; |
103 | } |
104 | |
105 | public function validateStringInputLength( string $labelOrDescriptionInput ) { |
106 | $maxLengthChars = $this->configService->get( 'EntitySchemaNameBadgeMaxSizeChars' ); |
107 | $numInputChars = mb_strlen( $labelOrDescriptionInput ); |
108 | if ( $numInputChars > $maxLengthChars ) { |
109 | return $this->msgLocalizer->msg( 'entityschema-error-input-too-long' ) |
110 | ->numParams( $maxLengthChars, $numInputChars ); |
111 | } |
112 | |
113 | return true; |
114 | } |
115 | |
116 | } |