Skip to content

Commit a453a75

Browse files
authored
Merge pull request #146 from fre5h/feature-random-values
Add new method `getRandomValue` for AbstractEnumType
2 parents 8418099 + 9bcc541 commit a453a75

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

DBAL/Types/AbstractEnumType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ public static function getValues(): array
143143
return \array_keys(static::$choices);
144144
}
145145

146+
/**
147+
* Get random value for the ENUM field.
148+
*
149+
* @static
150+
*
151+
* @return int|string
152+
*/
153+
public static function getRandomValue()
154+
{
155+
$values = self::getValues();
156+
$randomKey = \random_int(0, \count($values) - 1);
157+
158+
return $values[$randomKey];
159+
}
160+
146161
/**
147162
* Get array of ENUM Values, where ENUM values are keys and their readable versions are values.
148163
*

Resources/docs/additional_methods.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BasketballPositionType::getReadableValue(BasketballPositionType::SHOOTING_GUARD)
1616
// Will return: Shooting Guard
1717
```
1818

19-
##### If you need to get values in the readable format:
19+
##### If you need to get all values:
2020

2121
```php
2222
BasketballPositionType::getValues();
@@ -25,6 +25,15 @@ BasketballPositionType::getValues();
2525

2626
---
2727

28+
##### If you need to return a random value:
29+
30+
```php
31+
BasketballPositionType::getRandomValue();
32+
// Will randomly return one of the available values: 'PG', 'SG', 'SF', 'PF', 'C'
33+
```
34+
35+
---
36+
2837
### More features
2938

3039
* [NULL values](./null_values.md "NULL values")

Tests/DBAL/Types/AbstractEnumTypeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ public function testInvalidArgumentExceptionInConvertToDatabaseValue(): void
109109
$this->type->convertToDatabaseValue('YO', new MySqlPlatform());
110110
}
111111

112+
public function testGetRandomValue(): void
113+
{
114+
$values = $this->type::getValues();
115+
116+
self::assertContains($this->type::getRandomValue(), $values);
117+
self::assertContains($this->type::getRandomValue(), $values);
118+
self::assertContains($this->type::getRandomValue(), $values);
119+
}
120+
112121
public function testGetReadableValues(): void
113122
{
114123
$choices = [

0 commit comments

Comments
 (0)