<?php
/**
 * BookiFlex Payment Extension
 *
 * @package    BookiFlex
 * @subpackage Payment
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Payment\ORM\Typecast;

use Cycle\ORM\Parser\CastableInterface;
use Cycle\ORM\Parser\UncastableInterface;
use Payum\Core\Storage\IdentityInterface;

defined( 'ABSPATH' ) || exit;

/**
 * Identity typecast for Payum PaymentToken.
 */
class PaymentTokenIdentityTypecast implements CastableInterface, UncastableInterface
{
    public function setRules(array $rules): array
    {
        // Rules can be set here if necessary
        return $rules;
    }

    public function cast(array $data): array
    {
        if (isset($data['details']) && is_string($data['details'])) {
            $data['details'] = unserialize($data['details']);
        }

        return $data;
    }

    public function uncast(array $data): array
    {
        if (isset($data['details']) && $data['details'] instanceof IdentityInterface) {
            $data['details'] = serialize($data['details']);
        }

        return $data;
    }
}
