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\Context\RequestContext; |
| 12 | use MediaWiki\Languages\LanguageNameUtils; |
| 13 | use MediaWiki\MediaWikiServices; |
| 14 | use MediaWiki\Message\Message; |
| 15 | use MediaWiki\Title\Title; |
| 16 | use MessageLocalizer; |
| 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(): self { |
| 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 ) { |
| 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 | /** |
| 94 | * @return true|Message returns true on success and Message on failure |
| 95 | */ |
| 96 | public function validateAliasesLength( string $aliasesInput ) { |
| 97 | $maxLengthChars = $this->configService->get( 'EntitySchemaNameBadgeMaxSizeChars' ); |
| 98 | $cleanAliasesString = implode( '', array_map( 'trim', explode( '|', $aliasesInput ) ) ); |
| 99 | $aliasesLengthChars = mb_strlen( $cleanAliasesString ); |
| 100 | if ( $aliasesLengthChars > $maxLengthChars ) { |
| 101 | return $this->msgLocalizer->msg( 'entityschema-error-input-too-long' ) |
| 102 | ->numParams( $maxLengthChars, $aliasesLengthChars ); |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return true|Message returns true on success and Message on failure |
| 110 | */ |
| 111 | public function validateStringInputLength( string $labelOrDescriptionInput ) { |
| 112 | $maxLengthChars = $this->configService->get( 'EntitySchemaNameBadgeMaxSizeChars' ); |
| 113 | $numInputChars = mb_strlen( $labelOrDescriptionInput ); |
| 114 | if ( $numInputChars > $maxLengthChars ) { |
| 115 | return $this->msgLocalizer->msg( 'entityschema-error-input-too-long' ) |
| 116 | ->numParams( $maxLengthChars, $numInputChars ); |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | } |