1: <?php
2:
3: namespace Chromabits\Nucleus\Data\Traits;
4:
5: use Chromabits\Nucleus\Data\Exceptions\MismatchedDataTypesException;
6:
7: /**
8: * Class SameTypeTrait
9: *
10: * @author Eduardo Trujillo <ed@chromabits.com>
11: * @package Chromabits\Nucleus\Data\Traits
12: */
13: trait SameTypeTrait
14: {
15: /**
16: * Ensure that the received value is the same type as this class.
17: *
18: * @param string|object $received
19: *
20: * @throws MismatchedDataTypesException
21: */
22: protected function assertSameType($received)
23: {
24: if (!$received instanceof static) {
25: $this->throwMismatchedDataTypeException($received);
26: }
27: }
28:
29: /**
30: * @param string|object $received
31: *
32: * @throws MismatchedDataTypesException
33: */
34: protected function throwMismatchedDataTypeException($received)
35: {
36: throw MismatchedDataTypesException::create(
37: static::class,
38: $received
39: );
40: }
41: }
42: