chargebee-php icon indicating copy to clipboard operation
chargebee-php copied to clipboard

Support Enums in ->all() filter values

Open ychedemois opened this issue 5 months ago • 1 comments

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;
}

ychedemois avatar Jul 18 '25 13:07 ychedemois

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!

cb-alish avatar Jul 18 '25 16:07 cb-alish