| 1 | <?php |
||
| 12 | class FluentGenerator implements ApiGenerator { |
||
| 13 | |||
| 14 | private string $name; |
||
|
|
|||
| 15 | private array $params = []; |
||
| 16 | |||
| 17 | public function __construct( string $name ) { |
||
| 18 | $this->name = $name; |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Convenience method for using this fluidly |
||
| 23 | * |
||
| 24 | * |
||
| 25 | */ |
||
| 26 | public static function factory( string $name ): FluentGenerator { |
||
| 27 | return new self( $name ); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return string[] |
||
| 32 | */ |
||
| 33 | public function getParams(): array { |
||
| 34 | $params = $this->params; |
||
| 35 | $params['generator'] = $this->name; |
||
| 36 | return $params; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $key optionally with the 'g' prefix |
||
| 41 | * |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | public function set( string $key, string $value ): self { |
||
| 45 | $key = $this->addKeyPrefixIfNeeded( $key ); |
||
| 46 | $this->params[$key] = $value; |
||
| 47 | return $this; |
||
| 48 | } |
||
| 49 | |||
| 50 | private function addKeyPrefixIfNeeded( string $key ): string { |
||
| 51 | if ( strtolower( substr( $key, 0, 1 ) ) === 'g' ) { |
||
| 52 | return $key; |
||
| 53 | } |
||
| 54 | return 'g' . $key; |
||
| 55 | } |
||
| 56 | |||
| 57 | } |
||
| 58 |