Skip to content

Commit

Permalink
Adds Access::CALL
Browse files Browse the repository at this point in the history
  • Loading branch information
antarestupin committed Feb 10, 2016
1 parent 12bb393 commit 81be872
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/accessible.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Let's take again the `Foo` class with its `$bar` property. Here are the values y

- `Access::GET`: This will allow the property `$bar` to be accessed through the `Foo#getBar()` method.
- `Access::IS`: This will allow the property `$bar` to be accessed through the `Foo#isBar()` method.
- `Access::CALL`: This will allow the property `$bar` to be accessed through the `Foo#bar()` method (it can be useful in some specific cases, for example when using Twig).
- `Access::SET`: This will allow the property `$bar` to be modified through the `Foo#setBar($newVal)` method.

### Add constraints on properties
Expand Down
5 changes: 5 additions & 0 deletions lib/Accessible/Annotation/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Access
*/
const IS = 'is';

/**
* The property X can be accessed through an X method.
*/
const CALL = 'call';

/**
* The property can be modified through a setX method.
*
Expand Down
15 changes: 8 additions & 7 deletions lib/Accessible/AutoMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __call($name, array $args)
switch ($method) {
case 'get':
case 'is':
case 'call':
MethodCallManager::assertArgsNumber(0, $args);
return $this->$property;

Expand Down Expand Up @@ -114,15 +115,15 @@ public function __call($name, array $args)
*/
private function getMethodCallInfo($name)
{
// check that the called method is a valid method name
// also get the call type and the property to access
$callIsValid = preg_match("/(set|get|is|add|remove)([A-Z].*)/", $name, $pregMatches);
if (!$callIsValid) {
throw new \BadMethodCallException("Method $name does not exist.");
$extractedMethod = preg_match("/^(set|get|is|add|remove)([A-Z].*)/", $name, $pregMatches);
if ($extractedMethod) {
$method = $pregMatches[1];
$property = lcfirst($pregMatches[2]);
} else {
$method = 'call';
$property = $name;
}

$method = $pregMatches[1];
$property = strtolower(substr($pregMatches[2], 0, 1)) . substr($pregMatches[2], 1);
$collectionProperties = null;
if (in_array($method, array('add', 'remove'))) {
$collectionProperties = $this->_collectionsItemNames['byItemName'][$property];
Expand Down
7 changes: 7 additions & 0 deletions tests/Accessible/Tests/AccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public function testIsMethodCanBeCalled()
$this->assertEquals(true, $testCase->isBooleanProperty());
}

public function testDirectMethodCanBeCalled()
{
$testCase = new TestsCases\BaseTestCase();
$testCase->setCallProperty(42);
$this->assertEquals(42, $testCase->callProperty());
}

public function testTraitsAreNotForgotten()
{
$testCase = new TestsCases\TraitTestCase();
Expand Down
5 changes: 5 additions & 0 deletions tests/Accessible/Tests/TestsCases/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class BaseTestCase
*/
private $booleanProperty;

/**
* @Access({Access::CALL, Access::SET})
*/
private $callProperty;

/**
* @Access({Access::GET, Access::SET})
* @Assert\Type("integer")
Expand Down

0 comments on commit 81be872

Please sign in to comment.