Skip to content

Commit bad2403

Browse files
committed
Add enum argument.
1 parent ee91c6a commit bad2403

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,16 @@ $person1 = new ArrayArgument('', [new Argument('name', 'Hans'), new Argument('ag
171171
$person2 = new ArrayArgument('', [new Argument('name', 'Max'), new Argument('age', 20)]);
172172

173173
$persons = new ArrayArgument('persons', [$person1, $person2]);
174-
```
174+
```
175+
176+
### Enum arguments
177+
To create enum arguments it should look like this:
178+
179+
```php
180+
<?php
181+
declare(strict_types=1);
182+
183+
use GraphQL\RequestBuilder\EnumArgument;
184+
185+
$person1 = new EnumArgument('EnumAttribute', 'EnumValue');
186+
```

src/Argument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Argument implements ArgumentInterface
1919
protected $name;
2020

2121
/** @var int|float|string|bool|Argument[]|Argument The value of the argument. */
22-
private $value;
22+
protected $value;
2323

2424
/**
2525
* @inheritdoc

src/ArrayArgument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* @author david.pauli
8-
* @package GraphQLTest\RequestBuilder
8+
* @package GraphQL\RequestBuilder
99
* @since 15.08.2018
1010
*/
1111
class ArrayArgument extends Argument

src/EnumArgument.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace GraphQL\RequestBuilder;
5+
6+
/**
7+
* @author david.pauli
8+
* @package GraphQL\RequestBuilder
9+
* @since 29.08.2018
10+
*/
11+
class EnumArgument extends Argument
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function __toString(): string
17+
{
18+
switch (gettype($this->value)) {
19+
case 'string':
20+
$value = '' . $this->value . '';
21+
break;
22+
default:
23+
$value = null;
24+
}
25+
26+
return $value === null ? '': $this->name . ':' . $value;
27+
}
28+
}

tests/EnumArgumentTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace GraphQLTest\RequestBuilder;
5+
6+
use GraphQL\RequestBuilder\EnumArgument;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @author david.pauli
11+
* @package GraphQLTest\RequestBuilder
12+
* @since 29.08.2018
13+
*/
14+
class EnumArgumentTest extends TestCase
15+
{
16+
private const ARGUMENT_NAME = 'argumentName';
17+
private const ENUM_VALUE = 'enumValue';
18+
19+
public function testToStringEnum(): void
20+
{
21+
$argument = new EnumArgument(self::ARGUMENT_NAME, self::ENUM_VALUE);
22+
23+
static::assertEquals(
24+
self::ARGUMENT_NAME . ':' . self::ENUM_VALUE,
25+
(string) $argument,
26+
'Enum should be correct created.'
27+
);
28+
}
29+
}

0 commit comments

Comments
 (0)