laravel-connect/src/Traits/HasConnectData.php

48 lines
1.3 KiB
PHP

<?php
namespace Bluesquare\Connect\Traits;
use Illuminate\Support\Facades\Log;
trait HasConnectData
{
public function getConnectIdentifier()
{
return $this->connectIdentifier ?? 'connect_id';
}
public function fillConnectData(array $data)
{
$touched = [];
$fillable = $this->connectFillable ?? [];
foreach ($fillable as $origin => $targets) {
$value = is_string($origin) ? $data[$origin] : $data[$targets];
$targets = is_string($origin) && is_array($targets) ? $targets : [$targets];
foreach ($targets as $target) {
$parts = explode('|', $target);
$target = $parts[0];
$currentValue = $value ?? ($parts[1] ?? null);
$target_model = $this;
$parts = explode('.', $target);
foreach ($parts as $i => $property) {
if ($i < count($parts) - 1) {
$target_model = $target_model->$property;
continue;
}
if ($target_model !== $this)
$touched[] = $target_model;
$target_model->$property = $currentValue;
}
}
}
foreach ($touched as $model)
$model->save();
}
}