Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
71.43% |
5 / 7 |
CRAP | |
88.57% |
31 / 35 |
EntitySchema\Presentation\InputValidator | |
0.00% |
0 / 1 |
|
71.43% |
5 / 7 |
13.25 | |
88.57% |
31 / 35 |
newFromGlobalState | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
validateIDExists | |
0.00% |
0 / 1 |
3.03 | |
85.71% |
6 / 7 |
|||
validateLangCodeIsSupported | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
validateSchemaTextLength | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
validateAliasesLength | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
validateStringInputLength | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
<?php | |
namespace EntitySchema\Presentation; | |
use Config; | |
use ConfigException; | |
use InvalidArgumentException; | |
use Language; | |
use Message; | |
use MediaWiki\MediaWikiServices; | |
use MessageLocalizer; | |
use RequestContext; | |
use Title; | |
use EntitySchema\Domain\Model\SchemaId; | |
/** | |
* @license GPL-2.0-or-later | |
*/ | |
class InputValidator { | |
/** | |
* @var MessageLocalizer | |
*/ | |
private $msgLocalizer; | |
/** | |
* @var Config | |
*/ | |
private $configService; | |
public static function newFromGlobalState() { | |
return new self( | |
RequestContext::getMain(), | |
MediaWikiServices::getInstance()->getMainConfig() | |
); | |
} | |
public function __construct( MessageLocalizer $msgLocalizer, Config $config ) { | |
$this->msgLocalizer = $msgLocalizer; | |
$this->configService = $config; | |
} | |
/** | |
* @param $id | |
* | |
* @return true|Message returns true on success and Message on failure | |
*/ | |
public function validateIDExists( $id ) { | |
try { | |
$schemaId = new SchemaId( $id ); | |
} catch ( InvalidArgumentException $e ) { | |
return $this->msgLocalizer->msg( 'entityschema-error-invalid-id' ); | |
} | |
$title = Title::makeTitle( NS_ENTITYSCHEMA_JSON, $schemaId->getId() ); | |
if ( !$title->exists() ) { | |
return $this->msgLocalizer->msg( 'entityschema-error-schemadeleted' ); | |
} | |
return true; | |
} | |
/** | |
* @param $langCode | |
* | |
* @return true|Message returns true on success and Message on failure | |
*/ | |
public function validateLangCodeIsSupported( $langCode ) { | |
if ( !Language::isSupportedLanguage( $langCode ) ) { | |
return $this->msgLocalizer->msg( 'entityschema-error-unsupported-langcode' ); | |
} | |
return true; | |
} | |
/** | |
* @param $schemaText | |
* | |
* @return true|Message returns true on success and Message on failure | |
* @throws ConfigException | |
*/ | |
public function validateSchemaTextLength( $schemaText ) { | |
$maxLengthBytes = $this->configService->get( 'WBSchemaSchemaTextMaxSizeBytes' ); | |
$schemaTextLengthBytes = strlen( $schemaText ); | |
if ( $schemaTextLengthBytes > $maxLengthBytes ) { | |
return $this->msgLocalizer->msg( 'entityschema-error-schematext-too-long' ) | |
->numParams( $maxLengthBytes, $schemaTextLengthBytes ); | |
} | |
return true; | |
} | |
public function validateAliasesLength( $aliasesInput ) { | |
$maxLengthChars = $this->configService->get( 'WBSchemaNameBadgeMaxSizeChars' ); | |
$cleanAliasesString = implode( '', array_map( 'trim', explode( '|', $aliasesInput ) ) ); | |
$aliasesLengthChars = mb_strlen( $cleanAliasesString ); | |
if ( $aliasesLengthChars > $maxLengthChars ) { | |
return $this->msgLocalizer->msg( 'entityschema-error-input-too-long' ) | |
->numParams( $maxLengthChars, $aliasesLengthChars ); | |
} | |
return true; | |
} | |
public function validateStringInputLength( $labelOrDescriptionInput ) { | |
$maxLengthChars = $this->configService->get( 'WBSchemaNameBadgeMaxSizeChars' ); | |
$numInputChars = mb_strlen( $labelOrDescriptionInput ); | |
if ( $numInputChars > $maxLengthChars ) { | |
return $this->msgLocalizer->msg( 'entityschema-error-input-too-long' ) | |
->numParams( $maxLengthChars, $numInputChars ); | |
} | |
return true; | |
} | |
} |