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 0000000..262b549 Binary files /dev/null and b/scientist.png differ 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)); + } +}