private function castToObject($value, $class)
    {
        if (!class_exists($class) || $value instanceof $class) {
            return $value;
        }

        $parentClasses = class_parents($class);

        if (in_array(Enum::class, $parentClasses) || in_array(AbstractObject::class, $parentClasses)) {
            return new $class($value);
        }

        if (in_array(Model::class, $parentClasses)) {
            return new $class($this->getClient(), $value);
        }

        if (in_array(ListObject::class, $parentClasses)) {
            return new $class($value);
        }

        return $value;
    }