chargebee-php
chargebee-php copied to clipboard
Support Enums in ->all() filter values
What problem does your feature request address?
It would be convenient to be able to write code like :
$chargebeeClient->subscription()->all(['status' => ['is' => Status::ACTIVE]])
$chargebeeClient->subscription()->all(['status' => ['is' => $subscription->status])
Currently this produces "Object of class Chargebee\Resources\Subscription\Enums\Status could not be converted to string.", and we have to explicitly convert to a string :
$chargebeeClient->subscription()->all(['status' => ['is' => Status::ACTIVE->value]])
$chargebeeClient->subscription()->all(['status' => ['is' => $subscription->status->value])
Yet it is very easy to convert a string Enum to a string ;-)
Describe the desired solution
In Util::asString(), add something like :
if ($value instanceof \Enum && (new \ReflectionEnum($value))->getBackingType()?->getName() === 'string') {
return $value->value;
}
Hi @ychedemois, this is a fantastic suggestion—thank you! It’s a great improvement for developer ergonomics. We’ll definitely incorporate it. Really appreciate you sharing this!