<?php
namespace Fifa\ConnectId\Sdk\HashedPersonProcessor;

use Fifa\ConnectId\Sdk\Model\HashedPersonType;

/**
 * Class HashedPersonTypeHashingProcessor
 * @package Fifa\ConnectId\Sdk\HashedPersonProcessor
 */
class HashedPersonTypeHashingProcessor implements HashedPersonProcessorInterface
{
    /**
     * Process provided hashed person type to ensure that person local id is hashed before send
     *
     * @param HashedPersonType|null $hashedPersonType
     * @return HashedPersonType|null
     */
    public function process(?HashedPersonType $hashedPersonType): ?HashedPersonType
    {
        if (is_null($hashedPersonType) || empty($hashedPersonType->getLocalId())) {
            return $hashedPersonType;
        }

        $sha512 = hash('sha512', base64_encode($hashedPersonType->getLocalId()));
        
        $processedHashedType = new HashedPersonType();
        $processedHashedType->setNameHashes($hashedPersonType->getNameHashes())
            ->setPerson($hashedPersonType->getPerson())
            ->setLocalId($sha512);

        return $processedHashedType;
    }
}