Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EntityNamespaces
91.67% covered (success)
91.67%
11 / 12
66.67% covered (warning)
66.67%
2 / 3
10.06
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 validateMapping
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
8.09
 toArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\WikibaseManifest;
4
5use InvalidArgumentException;
6
7class EntityNamespaces {
8
9    public const NAMESPACE_ID = 'namespace_id';
10    public const NAMESPACE_NAME = 'namespace_name';
11
12    /**
13     * @var array
14     */
15    private $mapping;
16
17    /**
18     * @param array[] $mapping Key entity type, Value array keyed with namespaceId and
19     * namespaceString
20     */
21    public function __construct( array $mapping ) {
22        $this->validateMapping( $mapping );
23        $this->mapping = $mapping;
24    }
25
26    private function validateMapping( array $mapping ): void {
27        foreach ( $mapping as $k => $v ) {
28            if ( !is_string( $k ) ) {
29                throw new InvalidArgumentException( 'Keys of mapping should be strings' );
30            }
31            if (
32                !is_array( $v )
33                || !array_key_exists( self::NAMESPACE_ID, $v )
34                || !array_key_exists( self::NAMESPACE_NAME, $v )
35                || !is_int( $v[self::NAMESPACE_ID] )
36                || !is_string( $v[self::NAMESPACE_NAME] )
37            ) {
38                throw new InvalidArgumentException( 'Values of mapping should be arrays with needed keys and correct types' );
39            }
40        }
41    }
42
43    public function toArray(): array {
44        return $this->mapping;
45    }
46
47}