Completed
Push — reponame-assert ( e1c5cc )
by Leszek
08:01
created

RepositoryNameAssert::isValidRepositoryName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Assert;
4
5
use Wikimedia\Assert\Assert;
6
use Wikimedia\Assert\ParameterAssertionException;
7
8
/**
9
 * Provides functions to assure values are allowable repository
10
 * names in Wikibase.
11
 *
12
 * @see docs/foreign-entity-ids.wiki
13
 * @see Wikimedia\Assert\Assert
14
 *
15
 * @since 6.3
16
 *
17
 * @license GPL-2.0+
18
 */
19
class RepositoryNameAssert {
20
21
	/**
22
	 * @param string $value The actual value of the parameter
23
	 * @param string $name The name of the parameter being checked
24
	 *
25
	 * @throws ParameterAssertionException If $value is not a valid repository name.
26
	 */
27
	public static function assertParameterIsValidRepositoryName( $value, $name ) {
28
		if ( !self::isValidRepositoryName( $value ) ) {
29
			throw new ParameterAssertionException( $name, 'must be a string not including colons' );
30
		}
31
	}
32
33
	/**
34
	 * @param array $values The actual value of the parameter. If this is not an array,
35
	 *        a ParameterTypeException is thrown.
36
	 * @param string $name The name of the parameter being checked
37
	 *
38
	 * @throws ParameterAssertionException If any element of $values is not
39
	 *         a valid repository name.
40
	 */
41
	public static function assertParameterKeysAreValidRepositoryNames( $values, $name ) {
42
		Assert::parameterType( 'array', $values, $name );
43
		// TODO: change to Assert::parameterKeyType when the new version of the library is released
44
		Assert::parameterElementType( 'string', array_keys( $values ), "array_keys( $name )" );
45
46
		foreach ( array_keys( $values ) as $key ) {
47
			if ( !self::isValidRepositoryName( $key ) ) {
48
				throw new ParameterAssertionException(
49
					"array_keys( $name )",
50
					'must not contain strings including colons'
51
				);
52
			}
53
		}
54
	}
55
56
	/**
57
	 * @param string $value
58
	 * @return bool
59
	 */
60
	private static function isValidRepositoryName($value ) {
61
		return is_string( $value ) && strpos( $value, ':' ) === false;
62
	}
63
64
}
65