Completed
Push — master ( ec2f81...11966d )
by adam
03:28
created

FluentGenerator::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Mediawiki\Api\Generator;
4
5
/**
6
 * @access public
7
 *
8
 * @author Addshore
9
 *
10
 * @since 0.5.1
11
 */
12
class FluentGenerator implements ApiGenerator {
13
14
	private $name;
15
	private $params;
16
17
	/**
18
	 * @param string $name
19
	 */
20 3
	public function __construct( $name ) {
21 3
		$this->name = $name;
22 3
	}
23
24
	/**
25
	 * Convenience method for using this fluidly
26
	 *
27
	 * @param string $name
28
	 *
29
	 * @return FluentGenerator
30
	 */
31 1
	public static function factory( $name ) {
32 1
		return new self( $name );
33
	}
34
35
	/**
36
	 * @return string[]
37
	 */
38 3
	public function getParams() {
39 3
		$params = $this->params;
40 3
		$params['generator'] = $this->name;
41 3
		return $params;
42
	}
43
44
	/**
45
	 * @param string $key optionally with the 'g' prefix
46
	 * @param string $value
47
	 *
48
	 * @return $this
49
	 */
50 3
	public function set( $key, $value ) {
51 3
		$key = $this->addKeyprefixIfNeeded( $key );
52 3
		$this->params[$key] = $value;
53 3
		return $this;
54
	}
55
56
	/**
57
	 * @param string $key
58
	 *
59
	 * @return string
60
	 */
61 3
	private function addKeyPrefixIfNeeded( $key ) {
62 3
		if ( strtolower( substr( $key, 0, 1 ) ) === 'g' ) {
63 2
			return $key;
64
		}
65 2
		return 'g' . $key;
66
	}
67
68
}
69