Completed
Pull Request — master (#2)
by Tomáš
09:55
created

UserMapping::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Common;
6
7
use SixtyEightPublishers;
8
9
final class UserMapping implements \ArrayAccess
10
{
11
	const 	FIELD_ID = 'id',
12
			FILED_EMAIL = 'email',
13
			FIELD_PASSWORD = 'password',
14
			FIELD_LOGIN = 'login';
15
16
	/** @var string  */
17
	private $className;
18
19
	/** @var array  */
20
	private $fields = [
21
		self::FIELD_ID => 'id',
22
		self::FILED_EMAIL => 'email',
23
		self::FIELD_PASSWORD => 'password',
24
		self::FIELD_LOGIN => 'login',
25
	];
26
27
	/**
28
	 * @param string $className
29
	 * @param array  $fields
30
	 */
31
	public function __construct(string $className, array $fields)
32
	{
33
		$this->className = $className;
34
		$this->fields = array_merge($this->fields, $fields);
35
	}
36
37
	/**
38
	 * @return string
39
	 */
40
	public function getClassName(): string
41
	{
42
		return $this->className;
43
	}
44
45
	/**
46
	 * @param mixed $name
47
	 *
48
	 * @return bool
49
	 */
50
	public function __isset($name): bool
51
	{
52
		return isset($this->fields[$name]);
53
	}
54
55
	/**
56
	 * @param mixed $name
57
	 *
58
	 * @return string
59
	 */
60
	public function __get($name): string
61
	{
62
		if (!$this->__isset($name)) {
63
			throw new SixtyEightPublishers\User\Common\Exception\InvalidArgumentException(sprintf(
64
				'Missing field with name "%s"',
65
				$name
66
			));
67
		}
68
69
		return $this->fields[$name];
70
	}
71
72
	/**************** interface \ArrayAccess ****************/
73
74
	/**
75
	 * {@inheritdoc}
76
	 */
77
	public function offsetExists($offset): bool
78
	{
79
		return $this->__isset($offset);
80
	}
81
82
	/**
83
	 * {@inheritdoc}
84
	 */
85
	public function offsetGet($offset): string
86
	{
87
		return $this->__get($offset);
88
	}
89
90
	/**
91
	 * {@inheritdoc}
92
	 */
93
	public function offsetSet($offset, $value)
94
	{
95
		throw new SixtyEightPublishers\User\Common\Exception\RuntimeException(sprintf(
96
			'Calling of method %s is not supported.',
97
			__METHOD__
98
		));
99
	}
100
101
	/**
102
	 * {@inheritdoc}
103
	 */
104
	public function offsetUnset($offset)
105
	{
106
		throw new SixtyEightPublishers\User\Common\Exception\RuntimeException(sprintf(
107
			'Calling of method %s is not supported.',
108
			__METHOD__
109
		));
110
	}
111
}
112