]> git.andy128k.dev Git - missing-tools.git/commitdiff
add map methods 0.3
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 19 Mar 2019 23:45:20 +0000 (00:45 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 19 Mar 2019 23:50:24 +0000 (00:50 +0100)
src/map.php
t/MapTest.php

index 6af1f3756aee25811df8177ade5909485648e3a2..9e4dbcf5db2d502d7681d58a3414c58d9d5e0b0f 100644 (file)
@@ -49,6 +49,27 @@ class Map
         });
     }
 
+    function update($key, $updater)
+    {
+        foreach ($this->data as &$entry) {
+            if ($this->keysAreEqual($key, $entry[0])) {
+                $entry[1] = call_user_func($updater, $entry[1]);
+                return;
+            }
+        }
+    }
+
+    function updateOrInsert($key, $updater, $valueToInsert)
+    {
+        foreach ($this->data as &$entry) {
+            if ($this->keysAreEqual($key, $entry[0])) {
+                $entry[1] = call_user_func($updater, $entry[1]);
+                return;
+            }
+        }
+        $this->data[] = [$key, $valueToInsert];
+    }
+
     function __debugInfo()
     {
         $i = [];
index 6b11b272acca44d2971003a0954f9588e9fa2ce2..d7db5312af5d3bea97b6b156ba31434f1efeaba7 100644 (file)
@@ -4,30 +4,30 @@ class MapTest extends PHPUnit_Framework_TestCase
 {
     public function testGet()
     {
-        $arr = new \PFF\Map;
-        $arr->set(['fruit', 'apple'], 100);
-        $arr->set(['fruit', 'grapefruit'], 400);
-        $arr->set(['vegetable', 'carrot'], 50);
-
-        $this->assertEquals(3, $arr->count());
-        $this->assertEquals(true, $arr->contains(['fruit', 'grapefruit']));
-        $this->assertEquals(400, $arr->get(['fruit', 'grapefruit']));
-        $this->assertEquals(null, $arr->get(['fruit', 'orange']));
-        $this->assertEquals(false, $arr->contains(['apple', 'fruit']));
-        $this->assertEquals(null, $arr->get(['apple', 'fruit']));
-
-        $this->assertEquals(50, $arr->get(['vegetable', 'carrot']));
-        $arr->set(['vegetable', 'carrot'], 150);
-        $this->assertEquals(150, $arr->get(['vegetable', 'carrot']));
-        $arr->remove(['vegetable', 'carrot']);
-        $this->assertEquals(false, $arr->contains(['vegetable', 'carrot']));
+        $map = new \PFF\Map;
+        $map->set(['fruit', 'apple'], 100);
+        $map->set(['fruit', 'grapefruit'], 400);
+        $map->set(['vegetable', 'carrot'], 50);
+
+        $this->assertEquals(3, $map->count());
+        $this->assertEquals(true, $map->contains(['fruit', 'grapefruit']));
+        $this->assertEquals(400, $map->get(['fruit', 'grapefruit']));
+        $this->assertEquals(null, $map->get(['fruit', 'orange']));
+        $this->assertEquals(false, $map->contains(['apple', 'fruit']));
+        $this->assertEquals(null, $map->get(['apple', 'fruit']));
+
+        $this->assertEquals(50, $map->get(['vegetable', 'carrot']));
+        $map->set(['vegetable', 'carrot'], 150);
+        $this->assertEquals(150, $map->get(['vegetable', 'carrot']));
+        $map->remove(['vegetable', 'carrot']);
+        $this->assertEquals(false, $map->contains(['vegetable', 'carrot']));
     }
 
     public function testDebugInfo()
     {
-        $arr = new \PFF\Map;
-        $arr->set(['fruit', 'apple'], 100);
-        $output = $this->varDumpToString($arr);
+        $map = new \PFF\Map;
+        $map->set(['fruit', 'apple'], 100);
+        $output = $this->varDumpToString($map);
         $this->assertContains('fruit', $output);
         $this->assertContains('apple', $output);
         $this->assertContains('100', $output);
@@ -41,4 +41,38 @@ class MapTest extends PHPUnit_Framework_TestCase
         ob_end_clean();
         return $output;
     }
+
+    public function testUpdate()
+    {
+        $map = new \PFF\Map;
+        $map->set('carrot', 1);
+        $map->update('carrot', function ($c) {
+            return $c + 1;
+        });
+        $map->update('tomato', function ($c) {
+            return $c + 1;
+        });
+        $this->assertEquals(2, $map->get('carrot'));
+        $this->assertEquals(null, $map->get('tomato'));
+    }
+
+    public function testUpdateOrInsert()
+    {
+        $map = new \PFF\Map;
+        $map->set('carrot', 1);
+        $map->updateOrInsert('carrot', function ($c) {return $c + 1;}, 1);
+        $map->updateOrInsert('tomato', function ($c) {return $c + 1;}, 1);
+        $this->assertEquals(2, $map->get('carrot'));
+        $this->assertEquals(1, $map->get('tomato'));
+    }
+
+    public function testUpdateOrInsertArray()
+    {
+        $map = new \PFF\Map;
+        $map->set('carrot', [1]);
+        $map->updateOrInsert('carrot', function ($c) {return array_merge($c, [1]);}, [1]);
+        $map->updateOrInsert('tomato', function ($c) {return array_merge($c, [1]);}, [1]);
+        $this->assertEquals([1, 1], $map->get('carrot'));
+        $this->assertEquals([1], $map->get('tomato'));
+    }
 }