| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Services\Didit;
- class DiditDecisionExtractor
- {
- public function extract(array $decision): array
- {
- $id = $this->first($decision, 'id_verifications');
- $liveness = $this->first($decision, 'liveness_checks');
- $face = $this->first($decision, 'face_matches');
- return [
- 'id_verification_status' => data_get($id, 'status'),
- 'liveness_status' => data_get($liveness, 'status'),
- 'face_match_status' => data_get($face, 'status'),
- 'liveness_score' => $this->score($liveness, 'liveness_score'),
- 'face_match_score' => $this->score($face, 'face_match_score'),
- 'warnings' => $this->warnings($decision),
- ];
- }
- public function extractDocument(array $decision): ?string
- {
- $id = $this->first($decision, 'id_verifications');
- $taxNumber = data_get($id, 'extra_fields.tax_number');
- return is_string($taxNumber) && $taxNumber !== ''
- ? preg_replace('/\D+/', '', $taxNumber)
- : null;
- }
- /** @return list<array<string, mixed>> */
- public function warnings(array $decision): array
- {
- $warnings = [];
- foreach ($this->featureKeys() as $key) {
- foreach ($this->items($decision, $key) as $item) {
- foreach ((array) data_get($item, 'warnings', []) as $warning) {
- $warnings[] = $warning;
- }
- }
- }
- return $warnings;
- }
- public function actionableWarnings(array $decision): array
- {
- return array_values(array_filter(
- $this->warnings($decision),
- static fn ($warning) => data_get($warning, 'log_type') === 'warning',
- ));
- }
- /** @return list<string> */
- public function featureKeys(): array
- {
- return [
- 'id_verifications',
- 'liveness_checks',
- 'face_matches',
- 'ip_analyses',
- 'nfc_verifications',
- 'aml_screenings',
- 'poa_verifications',
- ];
- }
- public function items(array $decision, string $key): array
- {
- $value = data_get($decision, $key);
- return is_array($value) ? $value : [];
- }
- public function first(array $decision, string $key): ?array
- {
- $items = $this->items($decision, $key);
- return $items === [] ? null : (array) reset($items);
- }
- private function score(?array $item, string $fallbackKey): ?float
- {
- if ($item === null) {
- return null;
- }
- $value = data_get($item, 'score')
- ?? data_get($item, $fallbackKey)
- ?? data_get($item, 'similarity_percentage');
- if (! is_numeric($value)) {
- return null;
- }
- $value = (float) $value;
- return $value <= 1 ? round($value * 100, 2) : round($value, 2);
- }
- }
|