5pm-HDH / churchtools-api

The Churchtools-API Client is a wrapper for the churchtools-api written in PHP.
MIT License
18 stars 8 forks source link

Bug: Retrieve Group - Weekday #94

Closed DumbergerL closed 2 years ago

DumbergerL commented 2 years ago

Property Weekday come sometimes in as array, and sometimes as non-array-type.

Stacktrace:

PHP Fatal error:  Uncaught TypeError: Cannot assign int to property CTApi\Models\GroupInformation::$weekday of type ?array in C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Models\Traits\FillWithData.php:22
Stack trace:
#0 C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Models\Traits\FillWithData.php(15): CTApi\Models\GroupInformation->fillNonArrayType('weekday', 3)
#1 C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Models\Traits\FillWithData.php(63): CTApi\Models\GroupInformation->fillWithData(Array)
#2 C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Models\Group.php(48): CTApi\Models\GroupInformation::createModelFromData(Array)
#3 C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Models\Traits\FillWithData.php(13): CTApi\Models\Group->fillArrayType('information', Array)
#4 C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Models\Traits\FillWithData.php(63): CTApi\Models\Group->fillWithData(Array)
#5 C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Models\Traits\FillWithData.php(44): CTApi\Models\Group::createModelFromData(Array)
#6 [internal function]: CTApi\Models\Group::CTApi\Models\Traits\{closure}(Array)
#7 C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Models\Traits\FillWithData.php(43): array_map(Object(Closure), Array)
#8 C:\Users\ldumberger\projects\5pm-ct-export\vendor\5pm-hdh\churchtools-api\src\Requests\AbstractRequestBuilder.php(24): CTApi\Models\Group::createModelsFromArray(Array)
DumbergerL commented 2 years ago

Code to fix it for GroupInformation-Model:

protected function fillNonArrayType(string $key, $value): void
{
    switch ($key){
        case "weekday":
            $this->weekday = [$value];
            break;
        default:
            $this->{$key} = $value;
    }
}

Maybe one could use some more general approach and replace this whole $this->{$key} = $value-approach with a call a trait method called fillModelDefault where a check could be done it this is assignable. This could also handle #86