85 lines
2.8 KiB
PHP
85 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use stdClass;
|
|
|
|
/** Formats an already-authorized instrument; callers retain their own access checks. */
|
|
class PartnershipAgreementProjection
|
|
{
|
|
/** @var array<int,string|null> */
|
|
private array $people = [];
|
|
|
|
public function current(?string $json): stdClass|string|null
|
|
{
|
|
$value = $json === null ? null : json_decode($json, false, 512, JSON_THROW_ON_ERROR);
|
|
if ($value === null) {
|
|
return null;
|
|
}
|
|
if (! $value instanceof stdClass) {
|
|
return 'unrecognized';
|
|
}
|
|
|
|
return (object) $this->fields((array) $value);
|
|
}
|
|
|
|
/** @param array<string,mixed> $instrument
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function fields(array $instrument, bool $names = false): array
|
|
{
|
|
$fields = [];
|
|
foreach (['title', 'terms', 'legal_signature_status'] as $key) {
|
|
if (is_string($instrument[$key] ?? null)) {
|
|
$fields[$key] = $instrument[$key];
|
|
}
|
|
}
|
|
if (array_key_exists('confirmations', $instrument)) {
|
|
$fields['confirmations'] = $this->confirmations($instrument['confirmations'], $names);
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
|
|
/** @return array<string,mixed>|stdClass|string|null */
|
|
private function confirmations(mixed $value, bool $names): array|stdClass|string|null
|
|
{
|
|
if ($value === null) {
|
|
return null;
|
|
}
|
|
if ($value instanceof stdClass) {
|
|
$value = (array) $value;
|
|
}
|
|
if (! is_array($value) || $value !== [] && array_is_list($value)) {
|
|
return 'unrecognized';
|
|
}
|
|
$result = [];
|
|
foreach (['proponent', 'candidate'] as $party) {
|
|
if (! array_key_exists($party, $value)) {
|
|
continue;
|
|
}
|
|
$stamp = $value[$party] instanceof stdClass ? (array) $value[$party] : (is_array($value[$party]) ? $value[$party] : []);
|
|
$id = is_int($stamp['actor_id'] ?? null) && $stamp['actor_id'] > 0 ? $stamp['actor_id'] : null;
|
|
$result[$party] = ['actor_id' => $id, 'date' => is_string($stamp['date'] ?? null) ? $stamp['date'] : null, 'reason' => is_string($stamp['reason'] ?? null) ? $stamp['reason'] : null];
|
|
if ($names) {
|
|
$result[$party]['actor_name'] = $id ? $this->person($id) : null;
|
|
}
|
|
}
|
|
if (array_diff(array_keys($value), ['proponent', 'candidate']) !== []) {
|
|
$result['_unrecognized_references'] = true;
|
|
}
|
|
|
|
return $result === [] ? (object) [] : $result;
|
|
}
|
|
|
|
private function person(int $id): ?string
|
|
{
|
|
if (! array_key_exists($id, $this->people)) {
|
|
$this->people[$id] = DB::table('users')->where('id', $id)->value('name');
|
|
}
|
|
|
|
return $this->people[$id];
|
|
}
|
|
}
|