<?php

namespace PPLCZVendor\Jane\Component\JsonSchema\Guesser;

use PPLCZVendor\Jane\Component\JsonSchema\Guesser\Guess\Type;
use PPLCZVendor\Jane\Component\JsonSchema\Registry\Registry;
class ChainGuesser implements TypeGuesserInterface, PropertiesGuesserInterface, ClassGuesserInterface
{
    /**
     * @var GuesserInterface[]
     */
    private $guessers = [];
    public function addGuesser(GuesserInterface $guesser) : void
    {
        if ($guesser instanceof ChainGuesserAwareInterface) {
            $guesser->setChainGuesser($this);
        }
        $this->guessers[] = $guesser;
    }
    /**
     * {@inheritdoc}
     */
    public function guessClass($object, string $name, string $reference, Registry $registry) : void
    {
        foreach ($this->guessers as $guesser) {
            if (!$guesser instanceof ClassGuesserInterface) {
                continue;
            }
            if ($guesser->supportObject($object)) {
                $guesser->guessClass($object, $name, $reference, $registry);
            }
        }
    }
    /**
     * {@inheritdoc}
     */
    public function guessType($object, string $name, string $reference, Registry $registry) : Type
    {
        $type = null;
        foreach ($this->guessers as $guesser) {
            if (!$guesser instanceof TypeGuesserInterface) {
                continue;
            }
            if ($guesser->supportObject($object)) {
                return $guesser->guessType($object, $name, $reference, $registry);
            }
        }
        if (null === $type) {
            return new Type($object, 'mixed');
        }
        return $type;
    }
    /**
     * {@inheritdoc}
     */
    public function guessProperties($object, string $name, string $reference, Registry $registry) : array
    {
        $properties = [];
        foreach ($this->guessers as $guesser) {
            if (!$guesser instanceof PropertiesGuesserInterface) {
                continue;
            }
            if ($guesser->supportObject($object)) {
                $properties = \array_merge($properties, $guesser->guessProperties($object, $name, $reference, $registry));
            }
        }
        return $properties;
    }
}
