-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypedMap.php
More file actions
77 lines (71 loc) · 1.61 KB
/
TypedMap.php
File metadata and controls
77 lines (71 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* This file is part of the Zimbra API in PHP library.
*
* © Nguyen Van Nguyen <nguyennv1981@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zimbra\Common;
use PhpCollection\Map;
/**
* TypedMap class
*
* @package Zimbra
* @category Common
* @author Nguyen Van Nguyen - nguyennv1981@gmail.com
* @copyright Copyright © 2013 by Nguyen Van Nguyen.
*/
class TypedMap extends Map
{
/**
* Class type
* @var string
*/
private $_type;
/**
* Constructor method for TypedMap
* @param string $type
* @param array $elements
* @return self
*/
public function __construct($type, array $elements = [])
{
$this->_type = $type;
$this->setAll($elements);
}
/**
* Appends an element at the end of the sequence.
* @param string $key
* @param T $value
* @return self
*/
public function set($key, $value)
{
if($value instanceof $this->_type)
{
parent::set($key, $value);
}
else
{
throw new \UnexpectedValueException(
"TypedMap<$this->_type> can only hold objects of $this->_type class."
);
}
return $this;
}
/**
* Sets all key/value pairs in the map.
* @param array<string,T> $kvMap
* @return self
*/
public function setAll(array $kvMap)
{
foreach ($kvMap as $key => $value)
{
$this->set($key, $value);
}
return $this;
}
}