*/ 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 $instrument * @return array */ 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|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]; } }