<?php

namespace CouponsPlus\App\Coupon\Tree\Data\Transformers;

use CouponsPlus\App\Coupon\Tree\Data\Transformers\Abilities\TransformationStrategy;
use CouponsPlus\App\Coupon\Tree\Data\Transformers\Abilities\DataTransformationMatcher;

class AllTransformersStrategy implements TransformationStrategy
{
    /**
     * @param \CouponsPlus\Original\Collections\Collection $transformers
     * @param object $data
     * @return object
     */
    public function execute($transformers, $data)
    {
        $result = $data;

        foreach ($transformers->asArray() as $transformer) {
            if (is_a($transformer, DataTransformationMatcher::class, true)) {
                if ($transformer->canTransform($result)) {
                    $result = $transformer->transform($result);
                }
            } else {
                $result = $transformer->transform($result);
            }
        }

        return $result;
    }
}
