laravel-nami-api/src/Member.php

164 lines
5.3 KiB
PHP
Raw Normal View History

2020-06-29 00:30:57 +02:00
<?php
namespace Zoomyboy\LaravelNami;
2020-06-30 00:05:36 +02:00
use Carbon\Carbon;
2020-06-29 00:30:57 +02:00
use Illuminate\Database\Eloquent\Model;
2020-07-04 20:47:30 +02:00
use Illuminate\Support\LazyCollection;
2021-08-22 19:32:45 +02:00
use Zoomyboy\LaravelNami\Exceptions\RightException;
2020-06-29 00:30:57 +02:00
class Member extends Model {
2020-08-15 02:18:39 +02:00
public $timestamps = false;
public $geschlechtMaps = [
'männlich' => 19,
'weiblich' => 20,
'keine Angabe' => 23
];
2020-06-29 00:30:57 +02:00
protected static $overviewAttributes = [
'vorname' => 'firstname',
'nachname' => 'lastname',
'spitzname' => 'nickname',
'staatsangehoerigkeitText' => 'other_country',
'staatangehoerigkeitText' => 'other_country',
2020-06-29 00:30:57 +02:00
'strasse' => 'address',
'nameZusatz' => 'further_address',
'plz' => 'zip',
'ort' => 'location',
'id' => 'id',
'telefon1' => 'main_phone',
'telefon2' => 'mobile_phone',
'telefon3' => 'work_phone',
'telefax' => 'fax',
'email' => 'email',
'geschlecht' => 'geschlecht_text',
2020-06-29 23:29:54 +02:00
'geschlechtId' => 'gender_id',
2020-06-29 23:42:48 +02:00
'emailVertretungsberechtigter' => 'email_parents',
2020-06-30 00:01:17 +02:00
'staatsangehoerigkeitId' => 'nationality_id',
2020-06-30 00:05:36 +02:00
'konfessionId' => 'confession_id',
2020-06-30 00:07:53 +02:00
'geburtsDatum' => 'birthday',
'eintrittsdatum' => 'joined_at',
2020-08-15 02:18:39 +02:00
'gruppierungId' => 'group_id',
2020-09-15 22:02:28 +02:00
'lastUpdated' => 'updated_at',
'mitgliedsNummer' => 'mitgliedsnr',
2021-04-10 01:46:35 +02:00
'zeitschriftenversand' => 'send_newspaper',
2021-04-10 02:03:53 +02:00
'regionId' => 'region_id',
2021-04-10 02:07:07 +02:00
'landId' => 'country_id',
2021-04-10 02:55:37 +02:00
'beitragsartId' => 'fee_id',
2021-06-21 22:37:23 +02:00
'version' => 'version',
2020-06-29 00:30:57 +02:00
];
2020-07-04 01:08:26 +02:00
protected $casts = [];
2020-07-04 01:49:59 +02:00
protected $nullable = ['further_address', 'other_country', 'nickname', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents'];
2020-07-04 01:08:26 +02:00
2020-06-29 00:30:57 +02:00
protected $guarded = [];
public static function fromNami($item) {
$item = collect($item)
->only(array_keys(static::$overviewAttributes))
->mapWithKeys(function($item, $key) {
return [ data_get(static::$overviewAttributes, $key, $key) => $item ];
})
->toArray();
return (new self($item));
}
2021-05-14 20:10:22 +02:00
public static function fromAttributes($attributes) {
return new self($attributes);
}
public function toNami() {
return [
'vorname' => $this->firstname,
'nachname' => $this->lastname,
'spitzname' => $this->nickname ?: '',
2021-06-21 22:37:23 +02:00
'strasse' => $this->address,
'plz' => $this->zip,
'ort' => $this->location,
'eintrittsdatum' => $this->joined_at.'T00:00:00',
'version' => $this->version,
'beitragsartId' => $this->fee_id,
2021-06-21 23:50:28 +02:00
'regionId' => $this->region_id ?: Region::getNullValue(),
2021-06-21 22:37:23 +02:00
'landId' => $this->country_id,
'staatsangehoerigkeitId' => $this->nationality_id,
'geburtsDatum' => $this->birthday,
2021-05-14 20:10:22 +02:00
'geschlechtId' => $this->gender_id ?: Gender::getNullValue(),
2021-06-13 11:24:52 +02:00
'gruppierungId' => $this->group_id,
'id' => $this->id,
2021-06-21 23:50:28 +02:00
'zeitschriftenversand' => $this->send_newspaper,
'telefon1' => $this->main_phone ?: '',
'telefon2' => $this->mobile_phone ?: '',
'telefon3' => $this->work_phone ?: '',
'telefax' => $this->fax ?: '',
'email' => $this->email ?: '',
'emailVertretungsberechtigter' => $this->email_parents ?: '',
'geburtsDatum' => $this->birthday.' 00:00:00',
'nameZusatz' => $this->further_address ?: '',
'konfessionId' => $this->confession_id,
'staatsangehoerigkeitText' => $this->other_country ?: '',
2021-06-23 01:57:19 +02:00
'ersteTaetigkeitId' => $this->first_activity_id ?: null,
'ersteUntergliederungId' => $this->first_subactivity_id ?: null,
2021-05-14 20:10:22 +02:00
];
}
2020-06-29 00:30:57 +02:00
public function __construct($member) {
parent::__construct($member);
}
2020-06-30 00:05:36 +02:00
public function getBirthdayAttribute() {
return Carbon::parse($this->attributes['birthday'])->format('Y-m-d');
}
2020-06-30 00:07:53 +02:00
public function getJoinedAtAttribute() {
2020-07-04 01:08:26 +02:00
$date = $this->attributes['joined_at'];
return empty($date)
? null
: Carbon::parse($date)->format('Y-m-d');
2020-06-30 00:07:53 +02:00
}
2020-06-29 23:29:54 +02:00
public function getGenderIdAttribute() {
return $this->attributes['gender_id'] == Gender::getNullValue() ? null : $this->attributes['gender_id'];
}
public function setGeschlechtTextAttribute($v) {
$this->attributes['gender_id'] = data_get($this->geschlechtMaps, $v, null);
}
2020-07-04 01:46:03 +02:00
public function setAttribute($key, $value) {
if (in_array($key, $this->nullable) && $value === '') {
return parent::setAttribute($key, null);
2020-07-04 01:08:26 +02:00
}
2020-07-04 01:46:03 +02:00
return parent::setAttribute($key, $value);
2020-07-04 01:08:26 +02:00
}
2020-07-04 20:47:30 +02:00
public function memberships() {
$memberships = Nami::membershipsOf($this->id);
return LazyCollection::make(function() use ($memberships) {
foreach ($memberships as $membership) {
2021-08-22 19:32:45 +02:00
$m = $this->membership($membership['id']);
if ($m === null) {
continue;
}
yield $m;
2020-07-04 20:47:30 +02:00
}
});
}
2021-08-22 19:32:45 +02:00
public function membership($id): ?Membership {
try {
return Membership::fromNami(Nami::membership($this->id, $id));
} catch (RightException $e) {
return null;
}
2020-07-04 20:47:30 +02:00
}
2020-06-29 00:30:57 +02:00
}