Skip to content

Commit 98ea5c6

Browse files
committed
allow object as key
1 parent 0f43ea7 commit 98ea5c6

File tree

4 files changed

+127
-52
lines changed

4 files changed

+127
-52
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
.idea
3-
nbproject
3+
nbproject
4+
composer.lock

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ You can install library through Composer:
2121
}
2222
```
2323

24-
## Priority List
24+
## Priority Map
2525

26-
Priority list allows you to specify priority of items and
26+
Priority map allows you to specify priority of items and
2727
iterate through this list in order to priority.
2828

2929
Add elements to list with priority:
3030

3131
```php
3232
<?php
3333

34-
$list = new \Sokil\DataType\PriorityList();
34+
$list = new \Sokil\DataType\PriorityMap();
3535
$list->set('key1', 'value1', 10);
3636
$list->set('key2', 'value2', 100);
3737
```
@@ -54,7 +54,7 @@ Get element by key:
5454
```php
5555
<?php
5656

57-
$list = new \Sokil\DataType\PriorityList();
57+
$list = new \Sokil\DataType\PriorityMap();
5858
$list->set('key1', 'value1', 10);
5959
$list->get('key1');
6060
```

src/PriorityList.php src/PriorityMap.php

+95-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Sokil\DataType;
44

5-
class PriorityList implements \Iterator, \Countable
5+
class PriorityMap implements \Iterator, \Countable
66
{
77
private $lastSequence = 0;
88

@@ -14,14 +14,29 @@ class PriorityList implements \Iterator, \Countable
1414
private $order = self::ORDER_DESC;
1515

1616
/**
17+
* Get scalar key from mixed
18+
*/
19+
private function getScalarKey($key)
20+
{
21+
if (is_object($key)) {
22+
return spl_object_hash($key);
23+
} else {
24+
return $key;
25+
}
26+
}
27+
28+
/**
29+
* Add new item to map
1730
*
1831
* @param string $key name
1932
* @param string $value value
2033
* @param string $priority priority
21-
* @return \Sokil\PriorityList
34+
* @return PriorityMap
2235
*/
2336
public function set($key, $value, $priority = 0)
2437
{
38+
$key = $this->getScalarKey($key);
39+
2540
$this->list[$key] = new \stdclass();
2641
$this->list[$key]->value = $value;
2742
$this->list[$key]->priority = (int) $priority;
@@ -30,38 +45,79 @@ public function set($key, $value, $priority = 0)
3045
return $this->list[$key];
3146
}
3247

33-
public function get($name)
48+
/**
49+
* Get item from map
50+
*
51+
* @param $key
52+
* @return null
53+
*/
54+
public function get($key)
3455
{
35-
return isset($this->list[$name]) ? $this->list[$name]->value : null;
56+
$key = $this->getScalarKey($key);
57+
return isset($this->list[$key]) ? $this->list[$key]->value : null;
3658
}
3759

38-
public function has($name)
60+
/**
61+
* Check if item in map
62+
*
63+
* @param $key
64+
* @return bool
65+
*/
66+
public function has($key)
3967
{
40-
return isset($this->list[$name]);
68+
$key = $this->getScalarKey($key);
69+
return isset($this->list[$key]);
4170
}
4271

72+
/**
73+
* Get list of keys
74+
*
75+
* @return array
76+
*/
4377
public function getKeys()
4478
{
4579
return array_keys($this->list);
4680
}
4781

82+
/**
83+
* Get count or map
84+
*
85+
* @return int
86+
*/
4887
public function count()
4988
{
5089
return count($this->list);
5190
}
5291

92+
/**
93+
* Set ASC direction of sorting
94+
*
95+
* @return $this
96+
*/
5397
public function setAscOrder()
5498
{
5599
$this->order = self::ORDER_ASC;
56100
return $this;
57101
}
58102

103+
/**
104+
* Set DESC direction of sorting
105+
*
106+
* @return $this
107+
*/
59108
public function setDescOrder()
60109
{
61110
$this->order = self::ORDER_DESC;
62111
return $this;
63112
}
64113

114+
/**
115+
* ASC sort strategy
116+
*
117+
* @param $declaration1
118+
* @param $declaration2
119+
* @return int
120+
*/
65121
private function ascSortStrategy($declaration1, $declaration2)
66122
{
67123
if($declaration1->priority === $declaration2->priority) {
@@ -71,6 +127,13 @@ private function ascSortStrategy($declaration1, $declaration2)
71127
return $declaration1->priority > $declaration2->priority ? 1 : -1;
72128
}
73129

130+
/**
131+
* DESC sort strategy
132+
*
133+
* @param $declaration1
134+
* @param $declaration2
135+
* @return int
136+
*/
74137
private function descSortStrategy($declaration1, $declaration2)
75138
{
76139
if($declaration1->priority === $declaration2->priority) {
@@ -80,33 +143,59 @@ private function descSortStrategy($declaration1, $declaration2)
80143
return $declaration1->priority < $declaration2->priority ? 1 : -1;
81144
}
82145

146+
/**
147+
* Reset iterator
148+
*/
83149
public function rewind()
84150
{
85151
uasort($this->list, array($this, $this->order . 'SortStrategy'));
86152
reset($this->list);
87153
}
88154

155+
/**
156+
* Get current item
157+
*
158+
* @return mixed
159+
*/
89160
public function current()
90161
{
91162
$item = current($this->list);
92163
return $item->value;
93164
}
94165

166+
/**
167+
* Get current key
168+
*
169+
* @return mixed
170+
*/
95171
public function key()
96172
{
97173
return key($this->list);
98174
}
99175

176+
/**
177+
* Mve iterator next
178+
*/
100179
public function next()
101180
{
102181
next($this->list);
103182
}
104183

184+
/**
185+
* Check if current key is valid
186+
*
187+
* @return bool
188+
*/
105189
public function valid()
106190
{
107191
return null !== $this->key();
108192
}
109193

194+
/**
195+
* Convert map to array
196+
*
197+
* @return array
198+
*/
110199
public function toArray()
111200
{
112201
return iterator_to_array($this);

tests/PriorityListTest.php tests/PriorityMapTest.php

+26-41
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace Sokil\DataType;
44

5-
class PriorityListTest extends \PHPUnit_Framework_TestCase
5+
class PriorityMapTest extends \PHPUnit_Framework_TestCase
66
{
77
public function testToArrayDesc()
88
{
9-
$list = new PriorityList();
10-
11-
$this->assertEquals(array('a' => 'a', 'b' => 'b'), array('b' => 'b', 'a' => 'a'));
9+
$list = new PriorityMap();
1210

1311
$list->set('k1', 'v1', 2);
1412
$list->set('k2', 'v2', 8);
@@ -38,7 +36,7 @@ public function testToArrayDesc()
3836

3937
public function testToArrayAsc()
4038
{
41-
$list = new PriorityList();
39+
$list = new PriorityMap();
4240

4341
$list->setAscOrder();
4442

@@ -70,7 +68,7 @@ public function testToArrayAsc()
7068

7169
public function testGet()
7270
{
73-
$list = new PriorityList();
71+
$list = new PriorityMap();
7472

7573
$list->set('k1', 'v1', 2);
7674
$list->set('k2', 'v2', 8);
@@ -83,7 +81,7 @@ public function testGet()
8381

8482
public function testGetKeys()
8583
{
86-
$list = new PriorityList();
84+
$list = new PriorityMap();
8785

8886
$list->set('k1', 'v1', 2);
8987
$list->set('k2', 'v2', 8);
@@ -96,30 +94,20 @@ public function testGetKeys()
9694
$list->getKeys()
9795
);
9896
}
99-
97+
10098
public function testGetKeys_EmptyList()
10199
{
102-
$list = new PriorityList();
100+
$list = new PriorityMap();
103101

104102
$this->assertEquals(
105103
array(),
106104
$list->getKeys()
107105
);
108106
}
109107

110-
public function testHas()
111-
{
112-
$list = new PriorityList();
113-
114-
$list->set('k1', 'v1', 2);
115-
116-
$this->assertTrue($list->has('k1'));
117-
$this->assertFalse($list->has('UNKNOWN_KEY'));
118-
}
119-
120108
public function testGet_KeyNotExists()
121109
{
122-
$list = new PriorityList();
110+
$list = new PriorityMap();
123111

124112
$list->set('k1', 'v1', 2);
125113
$list->set('k2', 'v2', 8);
@@ -130,33 +118,30 @@ public function testGet_KeyNotExists()
130118
$this->assertEquals(null, $list->get('KEY_NOT_EXISTS'));
131119
}
132120

133-
public function testToArray()
121+
public function testHas()
134122
{
135-
$list = new PriorityList();
123+
$list = new PriorityMap();
136124

137125
$list->set('k1', 'v1', 2);
138-
$list->set('k2', 'v2', 8);
139-
$list->set('k3', 'v3', 16);
140-
$list->set('k4', 'v4', 1);
141-
$list->set('k5', 'v5', 4);
142126

143-
$expectedArray = array(
144-
'k3' => 'v3',
145-
'k2' => 'v2',
146-
'k5' => 'v5',
147-
'k1' => 'v1',
148-
'k4' => 'v4',
149-
);
127+
$this->assertTrue($list->has('k1'));
128+
$this->assertFalse($list->has('UNKNOWN_KEY'));
129+
}
150130

151-
foreach($list->toArray() as $key => $value) {
152-
$this->assertEquals(key($expectedArray), $key);
153-
$this->assertEquals(current($expectedArray), $value);
131+
public function testSet_ObjectKey()
132+
{
133+
$key1 = new \stdClass();
134+
$key2 = new \stdClass();
135+
$key3 = new \stdClass();
154136

155-
next($expectedArray);
156-
}
137+
$list = new PriorityMap();
138+
$list->setAscOrder();
157139

158-
if(key($expectedArray)) {
159-
$this->fail('Actual list less than expected');
160-
}
140+
$list->set($key1, 42, 1);
141+
$list->set($key2, 41, 0);
142+
$list->set($key3, 43, 2);
143+
144+
$list->rewind();
145+
$this->assertEquals(41, $list->current());
161146
}
162147
}

0 commit comments

Comments
 (0)