laravel-nami-api/src/Member.php

106 lines
3.1 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;
2020-06-29 00:30:57 +02:00
class Member extends Model {
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-07-04 13:07:58 +02:00
'gruppierungId' => 'group_id'
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));
}
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) {
yield $this->membership($membership['id']);
}
});
}
public function membership($id): Membership {
return Membership::fromNami(Nami::membership($this->id, $id));
}
2020-06-29 00:30:57 +02:00
}