From 7fbba2fb37266ea2004ce8ce8abfbabff5d0fe61 Mon Sep 17 00:00:00 2001 From: Dan Hanly Date: Sun, 7 Feb 2016 00:34:20 +0000 Subject: [PATCH] Added ArrayKeyMatcher --- README.md | 24 +++++++ scientist.png | Bin 0 -> 6829 bytes src/ArrayKeyMatcher.php | 80 ++++++++++++++++++++++ tests/ArrayKeyMatcherTest.php | 125 ++++++++++++++++++++++++++++++++++ 4 files changed, 229 insertions(+) create mode 100644 scientist.png create mode 100644 src/ArrayKeyMatcher.php create mode 100644 tests/ArrayKeyMatcherTest.php diff --git a/README.md b/README.md index 645982f..82a9b5e 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,30 @@ $matcher = new \DanHanly\Scientist\UtilityMatchers\ObjectPropertyMatcher('proper $matcher = new \DanHanly\Scientist\UtilityMatchers\ObjectPropertyMatcher(['propertyName', 'anotherPropertyName']); ``` +### ArrayKeyMatcher + +This allows you to match specific keys on arrays returned with the control and trial processes. + +When initialising the matcher, you can configure it to either match against a single key (via string), or multiple keys (via array) + +```php +// initialise matcher +$matcher = new \DanHanly\Scientist\UtilityMatchers\ArrayKeyMatcher; +// define keys via string +$matcher->setKeys('keyName'); +// or via array +$matcher->setKeys(['keyName', 'anotherKeyName']); +``` + +You can also set keys directly via the matcher constructor. + +```php +// define key via string +$matcher = new \DanHanly\Scientist\UtilityMatchers\ArrayKeyMatcher('keyName'); +// or via array +$matcher = new \DanHanly\Scientist\UtilityMatchers\ArrayKeyMatcher(['keyName', 'anotherKeyName']); +``` + ## Usage Once you've initialised and configured your matcher, you can use it within your experiments. diff --git a/scientist.png b/scientist.png new file mode 100644 index 0000000000000000000000000000000000000000..262b5496366e417d48ae471748db05b9ae31b28a GIT binary patch literal 6829 zcmV;e8dBwnP) zd6*n^na4lBs_ve9CX-__lY4S;Mj!~n0)l{uB6uLOx~RCQu&c7GtjD^FuDYwc3JTtM zt*EQ3tOAO1h={WAa0a;qLJ-1$B+QYy`>OiwA8$`G(^J)5)m7Ei-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c3iDchj%F1-1kE8U?k zMiQLEViz)M{SLU9n7Sr9r6`?+B-Iu-%WV{%L&CBhlVWs}g)yAK+}?q1n^&%OHeN@r zN=G~4#uTYE8| z?!@jX8Q|+>gct|@=+{tS39>u%f<~e;9z)U(w;)lr4--wpxImODx%$h(ufve~vil=n zgr@dN3OHdR>yhoq|Mcr9*IwMPF>+vTSt`5J(hYrgAR*)HO5E{&{I7|Of~~W$kc#cw zk&$aFvIcv+@aF-a&xj2#HGa2H+r{rf!|u$Jcw_;IM;Bsur@j2Qx}DO3tK>kv8Mq!9 zjR%m;%>dF}yW|{S4^-M1q%mpIm;)WC$t1cx=S6ToK!lKScr687U4a#OWOrKk;@ZbP bV(0$>5&mJ9E{PEA00000NkvXXu0mjfsxBuM literal 0 HcmV?d00001 diff --git a/src/ArrayKeyMatcher.php b/src/ArrayKeyMatcher.php new file mode 100644 index 0000000..eab3275 --- /dev/null +++ b/src/ArrayKeyMatcher.php @@ -0,0 +1,80 @@ +setKeys($keys); + } + } + + /** + * Determine whether two values match. + * + * @param mixed $control + * @param mixed $trial + * + * @return boolean + */ + public function match($control, $trial) + { + $keys = $this->getKeys(); + if (null === $keys) { + return false; + } + + foreach ($keys as $key) { + if ($control[$key] !== $trial[$key]) { + return false; + } + } + + return true; + } + + /** + * @param array|string $keys + * + * @return void + */ + public function setKeys($keys) + { + if (true === is_string($keys)) { + $this->keys[] = $keys; + return; + } + + if (true === is_array($keys)) { + foreach ($keys as $key) { + // Only add if the property name is a string + if (is_string($key)) { + $this->keys[] = $key; + } + } + return; + } + } + + /** + * @return array|null + */ + public function getKeys() + { + if (true === empty($this->keys)) { + return null; + } + + return $this->keys; + } +} diff --git a/tests/ArrayKeyMatcherTest.php b/tests/ArrayKeyMatcherTest.php new file mode 100644 index 0000000..015a5e2 --- /dev/null +++ b/tests/ArrayKeyMatcherTest.php @@ -0,0 +1,125 @@ +assertFalse($matcher->match([], [])); + } + + public function testInvalidKeys() + { + $matcher = new ArrayKeyMatcher(); + $matcher->setKeys(new \stdClass()); + $this->assertFalse($matcher->match([], [])); + + $matcher->setKeys([new \stdClass()]); + $this->assertFalse($matcher->match([], [])); + } + + public function testInvalidKeysViaConstructor() + { + $matcher = new ArrayKeyMatcher(new \stdClass()); + $this->assertFalse($matcher->match([], [])); + + $matcher = new ArrayKeyMatcher([new \stdClass()]); + $this->assertFalse($matcher->match([], [])); + } + + public function testStringProperty() + { + $control = []; + $control['key'] = 'test'; + + $trial = []; + $trial['key'] = 'test'; + + $matcher = new ArrayKeyMatcher(); + $matcher->setKeys('key'); + + $this->assertTrue($matcher->match($control, $trial)); + + $trial['key'] = 'different'; + + $this->assertFalse($matcher->match($control, $trial)); + } + + public function testStringPropertyViaConstructor() + { + $control = []; + $control['key'] = 'test'; + + $trial = []; + $trial['key'] = 'test'; + + $matcher = new ArrayKeyMatcher('key'); + + $this->assertTrue($matcher->match($control, $trial)); + + $trial['key'] = 'different'; + + $this->assertFalse($matcher->match($control, $trial)); + } + + public function testArrayProperties() + { + $control = []; + $control['property'] = 'test'; + $control['property2'] = 'test'; + + $trial = []; + $trial['property'] = 'test'; + $trial['property2'] = 'test'; + + $matcher = new ArrayKeyMatcher(); + $matcher->setKeys( + [ + 'property', + 'property2' + ] + ); + + $this->assertTrue($matcher->match($control, $trial)); + + $trial['property'] = 'different'; + + $this->assertFalse($matcher->match($control, $trial)); + + $trial['property2'] = 'different'; + + $this->assertFalse($matcher->match($control, $trial)); + } + + public function testArrayPropertiesViaConstructor() + { + $control = []; + $control['property'] = 'test'; + $control['property2'] = 'test'; + + $trial = []; + $trial['property'] = 'test'; + $trial['property2'] = 'test'; + + $matcher = new ArrayKeyMatcher( + [ + 'property', + 'property2' + ] + ); + + $this->assertTrue($matcher->match($control, $trial)); + + $trial['property'] = 'different'; + + $this->assertFalse($matcher->match($control, $trial)); + + $trial['property2'] = 'different'; + + $this->assertFalse($matcher->match($control, $trial)); + } +}