Passed
Push — aliasGroupTest ( 84a0d7...e654f3 )
by no
02:52
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
	 * @since 6.3
23
	 *
24
	 * @param string $value The actual value of the parameter
25
	 * @param string $name The name of the parameter being checked
26
	 *
27
	 * @throws ParameterAssertionException If $value is not a valid repository name.
28
	 */
29
	public static function assertParameterIsValidRepositoryName( $value, $name ) {
30
		if ( !self::isValidRepositoryName( $value ) ) {
31
			throw new ParameterAssertionException( $name, 'must be a string not including colons' );
32
		}
33
	}
34
35
	/**
36
	 * @since 6.3
37
	 *
38
	 * @param array $values The actual value of the parameter. If this is not an array,
39
	 *        a ParameterTypeException is thrown.
40
	 * @param string $name The name of the parameter being checked
41
	 *
42
	 * @throws ParameterAssertionException If any element of $values is not
43
	 *         a valid repository name.
44
	 */
45
	public static function assertParameterKeysAreValidRepositoryNames( $values, $name ) {
46
		Assert::parameterType( 'array', $values, $name );
47
		// TODO: change to Assert::parameterKeyType when the new version of the library is released
48
		Assert::parameterElementType( 'string', array_keys( $values ), "array_keys( $name )" );
49
50
		foreach ( array_keys( $values ) as $key ) {
51
			if ( !self::isValidRepositoryName( $key ) ) {
52
				throw new ParameterAssertionException(
53
					"array_keys( $name )",
54
					'must not contain strings including colons'
55
				);
56
			}
57
		}
58
	}
59
60
	/**
61
	 * @param string $value
62
	 * @return bool
63
	 */
64
	private static function isValidRepositoryName( $value ) {
65
		return is_string( $value ) && strpos( $value, ':' ) === false;
66
	}
67
68
}
69