Skip to content

Commit

Permalink
Use method to instead of get to better consistent with opposite `…
Browse files Browse the repository at this point in the history
…from`
  • Loading branch information
grachevko committed Dec 19, 2019
1 parent c273597 commit 62a599c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Premier\Enum;
* @method bool isFront()
* @method bool isRear()
* @method bool isAllDrive()
* @method string getCode()
* @method string toCode()
*/
final class DriveWheel extends Enum
{
Expand Down Expand Up @@ -56,14 +56,14 @@ DriveWheel::all(['FWD', 'RWD'], $reverse = false, $property = 'code'); // [Drive
DriveWheel::all([1, 2], $reverse = true); // [DriveWheel::allDrive()]

// Methods
$drive->getId(); // 1
$drive->get('id'); // 1
$drive->toId(); // 1
$drive->to('id'); // 1
(string) $drive; // '1'

$drive->getName(); // 'front'
$drive->toName(); // 'front'

$drive->getCode(); // 'FWD'
$drive->get('code'); // 'FWD'
$drive->toCode(); // 'FWD'
$drive->to('code'); // 'FWD'

$drive->isFront(); // true
$drive->isRear(); // false
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)

assert($value instanceof Enum);

return $value->get($this->property);
return $value->to($this->property);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private function __construct(int $id)

final public function __toString(): string
{
return (string) $this->getId();
return (string) $this->toId();
}

/**
Expand All @@ -87,8 +87,8 @@ final public function __call(string $name, array $arguments)
return $this->eq(static::create($reflection->getConstant(self::stringToConstant(substr($name, 2)))));
}

if (0 === strpos($name, 'get') && ctype_upper($name[3])) {
return $this->get(lcfirst(substr($name, 3)));
if (0 === strpos($name, 'to') && ctype_upper($name[2])) {
return $this->to(lcfirst(substr($name, 2)));
}

throw new BadMethodCallException(sprintf('Undefined method "%s" in class "%s"', $name, static::class));
Expand Down Expand Up @@ -143,21 +143,21 @@ final public static function from(string $property, $value): self
return static::create($id);
}

final public function getId(): int
final public function toId(): int
{
return $this->id;
}

/**
* @throws ReflectionException
*/
final public function getName(): string
final public function toName(): string
{
if (self::getReflection()->hasProperty('name')) {
return $this->get('name');
return $this->to('name');
}

return strtolower(array_flip(self::getReflection()->getConstants())[$this->getId()]);
return strtolower(array_flip(self::getReflection()->getConstants())[$this->toId()]);
}

/**
Expand All @@ -166,7 +166,7 @@ final public function getName(): string
*
* @return mixed
*/
final public function get(string $property)
final public function to(string $property)
{
if ('id' === $property) {
return $this->id;
Expand All @@ -176,7 +176,7 @@ final public function get(string $property)
throw new InvalidArgumentException(sprintf('Property "%s" not exist at class "%s"', $property, static::class));
}

return self::getReflectionProperty($property)->getValue()[$this->getId()];
return self::getReflectionProperty($property)->getValue()[$this->toId()];
}

/**
Expand All @@ -189,7 +189,7 @@ final public function get(string $property)
final public static function all(array $values = [], bool $reverse = false, string $property = 'id'): array
{
$values = array_map(static function ($value) {
return $value instanceof self ? $value->getId() : $value;
return $value instanceof self ? $value->toId() : $value;
}, $values);

if ('id' === $property) {
Expand All @@ -211,12 +211,12 @@ final public static function all(array $values = [], bool $reverse = false, stri

final public function eq(self $enum): bool
{
return static::class === get_class($enum) && $enum->getId() === $this->getId();
return static::class === get_class($enum) && $enum->toId() === $this->toId();
}

final public function serialize(): string
{
return (string) $this->getId();
return (string) $this->toId();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Gender.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ class Gender extends Enum

public function isDefined(): bool
{
return in_array($this->getId(), [self::MALE, self::FEMALE], true);
return in_array($this->toId(), [self::MALE, self::FEMALE], true);
}
}
26 changes: 13 additions & 13 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ public function testIntegerValue(): void

public function testNameValue(): void
{
static::assertSame('yo', TestEnum::one()->getName());
static::assertSame('one', AnotherTestEnum::one()->getName());
static::assertSame('yo', TestEnum::one()->toName());
static::assertSame('one', AnotherTestEnum::one()->toName());
}

public function testGet(): void
public function testTo(): void
{
static::assertSame('yo', TestEnum::one()->get('name'));
static::assertSame('This is a description for TestEnum::TWO', TestEnum::two()->get('description'));
static::assertSame('yo', TestEnum::one()->to('name'));
static::assertSame('This is a description for TestEnum::TWO', TestEnum::two()->to('description'));

$this->expectException(InvalidArgumentException::class);
TestEnum::two()->get('undefined_property');
TestEnum::two()->to('undefined_property');

static::assertSame(1, TestEnum::one()->get('id'));
static::assertSame(2, TestEnum::one()->get('id'));
static::assertSame(1, TestEnum::one()->to('id'));
static::assertSame(2, TestEnum::one()->to('id'));
}

public function testCustomPropertyValue(): void
{
static::assertSame('This is a description for TestEnum::TWO', TestEnum::two()->getDescription());
static::assertSame('This is a description for TestEnum::TWO', TestEnum::two()->toDescription());
}

public function testAllMethod(): void
Expand All @@ -72,12 +72,12 @@ public function testReadableName(): void
{
$readableEnum = ReadableEnum::roleAdmin();

static::assertSame('role_admin', $readableEnum->getName());
static::assertSame('role_admin', $readableEnum->toName());
}

public function testCompositeCustomValue(): void
{
static::assertSame('This is two description for one', TestEnum::one()->getDescriptionTwo());
static::assertSame('This is two description for one', TestEnum::one()->toDescriptionTwo());
}

public function testMethodEq(): void
Expand All @@ -101,8 +101,8 @@ public function testSerialization(): void

public function testFrom(): void
{
static::assertSame('yo', TestEnum::fromName('yo')->getName());
static::assertSame('yo', TestEnum::from('name', 'yo')->getName());
static::assertSame('yo', TestEnum::fromName('yo')->toName());
static::assertSame('yo', TestEnum::from('name', 'yo')->toName());

static::assertSame(TestEnum::one(), TestEnum::from('id', 1));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/GenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testEnumIsMale(): void
static::assertTrue($male->isMale());
static::assertFalse($male->isFemale());
static::assertFalse($male->isUnapplicable());
static::assertSame('male', $male->getName());
static::assertSame('male', $male->toName());
}

public function testEnumIsFemale(): void
Expand All @@ -27,7 +27,7 @@ public function testEnumIsFemale(): void
static::assertTrue($female->isFemale());
static::assertFalse($female->isMale());
static::assertFalse($female->isUnapplicable());
static::assertSame('female', $female->getName());
static::assertSame('female', $female->toName());
}

public function testEnumCallStatic(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/TestEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/**
* @method static TestEnum one()
* @method static TestEnum two()
* @method string getDescription()
* @method string getDescriptionTwo()
* @method string toDescription()
* @method string toDescriptionTwo()
* @method static self fromName(string $name)
* @method static self undefinedMethod()
*/
Expand Down

0 comments on commit 62a599c

Please sign in to comment.