1<?php
2
3namespace Doctrine\Instantiator\Exception;
4
5use Exception;
6use ReflectionClass;
7use UnexpectedValueException as BaseUnexpectedValueException;
8use function sprintf;
9
10/**
11 * Exception for given parameters causing invalid/unexpected state on instantiation
12 */
13class UnexpectedValueException extends BaseUnexpectedValueException implements ExceptionInterface
14{
15    public static function fromSerializationTriggeredException(
16        ReflectionClass $reflectionClass,
17        Exception $exception
18    ) : self {
19        return new self(
20            sprintf(
21                'An exception was raised while trying to instantiate an instance of "%s" via un-serialization',
22                $reflectionClass->getName()
23            ),
24            0,
25            $exception
26        );
27    }
28
29    public static function fromUncleanUnSerialization(
30        ReflectionClass $reflectionClass,
31        string $errorString,
32        int $errorCode,
33        string $errorFile,
34        int $errorLine
35    ) : self {
36        return new self(
37            sprintf(
38                'Could not produce an instance of "%s" via un-serialization, since an error was triggered '
39                . 'in file "%s" at line "%d"',
40                $reflectionClass->getName(),
41                $errorFile,
42                $errorLine
43            ),
44            0,
45            new Exception($errorString, $errorCode)
46        );
47    }
48}
49