From a89bf35e7747bcf56b9303eda2a689a1395288f5 Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 6 Feb 2015 10:19:53 +0530 Subject: [PATCH] Add accessor for protected properties of ValidationRule --- src/Validation/ValidationRule.php | 14 +++++++++++++ .../Validation/ValidationRuleTest.php | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Validation/ValidationRule.php b/src/Validation/ValidationRule.php index 0a8e430ab61..5cd91edc12e 100644 --- a/src/Validation/ValidationRule.php +++ b/src/Validation/ValidationRule.php @@ -193,4 +193,18 @@ protected function _addValidatorProps($validator = []) } } } + + /** + * Returns the value of a property by name + * + * @param string $property The name of the property to retrieve. + * @return mixed + */ + public function get($property) + { + $property = '_' . $property; + if (isset($this->{$property})) { + return $this->{$property}; + } + } } diff --git a/tests/TestCase/Validation/ValidationRuleTest.php b/tests/TestCase/Validation/ValidationRuleTest.php index 87bf9824e18..6532f631d7f 100644 --- a/tests/TestCase/Validation/ValidationRuleTest.php +++ b/tests/TestCase/Validation/ValidationRuleTest.php @@ -171,4 +171,25 @@ public function testCallableOn() ]); $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true])); } + + /** + * testGet + * + * @return void + */ + public function testGet() { + $Rule = new ValidationRule(['rule' => 'myTestRule', 'message' => 'foo']); + + $this->assertEquals('myTestRule', $Rule->get('rule')); + $this->assertEquals('foo', $Rule->get('message')); + $this->assertEquals('default', $Rule->get('provider')); + $this->assertEquals([], $Rule->get('pass')); + $this->assertNull($Rule->get('non-existent')); + + $Rule = new ValidationRule(['rule' => ['myTestRule2', 'param'], 'message' => 'bar']); + + $this->assertEquals('myTestRule2', $Rule->get('rule')); + $this->assertEquals('bar', $Rule->get('message')); + $this->assertEquals(['param'], $Rule->get('pass')); + } }