Skip to content

Guess non writable attributes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 43 additions & 25 deletions Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,27 @@ public function getAttributes(
$reflClass = $this->getReflectionClass();
// methods
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
!$reflMethod->isConstructor() &&
!$reflMethod->isDestructor() &&
0 === $reflMethod->getNumberOfRequiredParameters()
) {
$methodName = $reflMethod->getName();

if (strpos($methodName, 'get') === 0 || strpos($methodName, 'has') === 0) {
// getters and hassers
$attributeName = lcfirst(substr($methodName, 3));
} elseif (strpos($methodName, 'is') === 0) {
// issers
$attributeName = lcfirst(substr($methodName, 2));
}
if ($reflMethod->isConstructor() || $reflMethod->isDestructor()) {
continue;
}

if (isset($attributeName)) {
$attributes[$attributeName] = [
'readable' => true,
'writable' => true,
'description' => (new DocBlock($reflMethod))->getShortDescription(),
];
$methodName = $reflMethod->getName();
if (null === ($attributeName = $this->guessAttributeName($methodName))) {
continue;
}

$this->populateAttribute($attributeName, $attributes[$attributeName], $validationGroups);
unset($attributeName);
}
if (!isset($attributes[$attributeName])) {
$attributes[$attributeName] = [
'readable' => false,
'writable' => false,
'description' => (new DocBlock($reflMethod))->getShortDescription(),
];
}

if (0 === $reflMethod->getNumberOfRequiredParameters()) {
$attributes[$attributeName]['readable'] = true;
} else {
$attributes[$attributeName]['writable'] = true;
}
}

Expand All @@ -195,10 +191,13 @@ public function getAttributes(
'writable' => true,
'description' => (new DocBlock($reflProperty))->getShortDescription(),
];

$this->populateAttribute($attributeName, $attributes[$attributeName], $validationGroups);
}
}

// populate attributes
foreach ($attributes as $attributeName => $attribute) {
$this->populateAttribute($attributeName, $attributes[$attributeName], $validationGroups);
}
}

return $this->attributes[$key] = $attributes;
Expand Down Expand Up @@ -226,6 +225,25 @@ public function getReflectionClass()
return $this->reflClass;
}

/**
* Guess an attribute name by its method name.
*
* @param $methodName
* @return null|string
*/
private function guessAttributeName($methodName)
{
if (strpos($methodName, 'get') === 0 || strpos($methodName, 'has') === 0 || strpos($methodName, 'set') === 0) {
// getters, setters and hassers
return lcfirst(substr($methodName, 3));
} elseif (strpos($methodName, 'is') === 0) {
// issers
return lcfirst(substr($methodName, 2));
}

return null;
}

/**
* Returns a DocBlock instance for this class.
*
Expand Down
12 changes: 11 additions & 1 deletion Tests/Behat/TestBundle/Entity/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ class Dummy
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
private $id;

/**
* @ORM\Column
* @Assert\NotBlank
*/
private $name;

/**
* @ORM\Column(nullable=true)
*/
public $dummy;

public function getId()
{
return $this->id;
}

public function setName($name)
{
$this->name = $name;
Expand Down
27 changes: 18 additions & 9 deletions features/collection.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ Feature: Collections support
{
"@id": "/dummies/1",
"@type": "Dummy",
"name": "Dummy #1"
"name": "Dummy #1",
"dummy": null
},
{
"@id": "/dummies/2",
"@type": "Dummy",
"name": "Dummy #2"
"name": "Dummy #2",
"dummy": null
},
{
"@id": "/dummies/3",
"@type": "Dummy",
"name": "Dummy #3"
"name": "Dummy #3",
"dummy": null
}
]
}
Expand Down Expand Up @@ -66,17 +69,20 @@ Feature: Collections support
{
"@id": "/dummies/19",
"@type": "Dummy",
"name": "Dummy #19"
"name": "Dummy #19",
"dummy": null
},
{
"@id": "/dummies/20",
"@type": "Dummy",
"name": "Dummy #20"
"name": "Dummy #20",
"dummy": null
},
{
"@id": "/dummies/21",
"@type": "Dummy",
"name": "Dummy #21"
"name": "Dummy #21",
"dummy": null
}
]
}
Expand Down Expand Up @@ -105,17 +111,20 @@ Feature: Collections support
{
"@id": "/dummies/28",
"@type": "Dummy",
"name": "Dummy #28"
"name": "Dummy #28",
"dummy": null
},
{
"@id": "/dummies/29",
"@type": "Dummy",
"name": "Dummy #29"
"name": "Dummy #29",
"dummy": null
},
{
"@id": "/dummies/30",
"@type": "Dummy",
"name": "Dummy #30"
"name": "Dummy #30",
"dummy": null
}
]
}
Expand Down
12 changes: 8 additions & 4 deletions features/crud.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Feature: Create-Retrieve-Update-Delete
"@context": "/contexts/Dummy",
"@id": "/dummies/1",
"@type": "Dummy",
"name": "My Dummy"
"name": "My Dummy",
"dummy": null
}
"""

Expand All @@ -35,7 +36,8 @@ Feature: Create-Retrieve-Update-Delete
"@context": "/contexts/Dummy",
"@id": "/dummies/1",
"@type": "Dummy",
"name": "My Dummy"
"name": "My Dummy",
"dummy": null
}
"""

Expand All @@ -58,7 +60,8 @@ Feature: Create-Retrieve-Update-Delete
{
"@id":"/dummies/1",
"@type":"Dummy",
"name":"My Dummy"
"name":"My Dummy",
"dummy": null
}
]
}
Expand All @@ -81,7 +84,8 @@ Feature: Create-Retrieve-Update-Delete
"@context": "/contexts/Dummy",
"@id": "/dummies/1",
"@type": "Dummy",
"name": "A nice dummy"
"name": "A nice dummy",
"dummy": null
}
"""

Expand Down
106 changes: 106 additions & 0 deletions features/vocab.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
Feature: Documentation support
In order to build an auto-discoverable API
As a client software developer
I need to know the specifications of objects I send and receive

@createSchema
@dropSchema
Scenario: Retrieve the first page of a collection
And I send a "GET" request to "/vocab"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json"
And the JSON should be equal to:
"""
{
"@context": "\/contexts\/ApiDocumentation",
"@id": "\/vocab",
"hydra:title": "My Dummy API",
"hydra:description": "This is a test API.",
"hydra:entrypoint": "\/",
"hydra:supportedClass": [
{
"@id": "Entrypoint",
"@type": "hydra:class",
"hydra:title": "The API entrypoint",
"hydra:supportedProperty": [
{
"@type": "hydra:SupportedProperty",
"hydra:property": "dummies",
"hydra:title": "The collection of Dummy resources",
"hydra:readable": true,
"hydra:writable": false,
"hydra:supportedOperation": [
{
"@type": "hydra:Operation",
"hydra:title": "Retrieves the collection of Dummy resources.",
"hydra:returns": "hydra:PagedCollection",
"hydra:method": "GET"
},
{
"@type": "hydra:CreateResourceOperation",
"hydra:title": "Creates a Dummy resource.",
"hydra:expects": "Dummy",
"hydra:returns": "Dummy",
"hydra:method": "POST"
}
]
}
]
},
{
"@id": "Dummy",
"@type": "hydra:Class",
"hydra:title": "Dummy",
"hydra:description": "Dummy.",
"hydra:supportedProperty": [
{
"@type": "hydra:SupportedProperty",
"hydra:property": "Dummy\/id",
"hydra:title": "id",
"hydra:required": false,
"hydra:readable": true,
"hydra:writable": false
},
{
"@type": "hydra:SupportedProperty",
"hydra:property": "Dummy\/name",
"hydra:title": "name",
"hydra:required": true,
"hydra:readable": true,
"hydra:writable": true
},
{
"@type": "hydra:SupportedProperty",
"hydra:property": "Dummy\/dummy",
"hydra:title": "dummy",
"hydra:required": false,
"hydra:readable": true,
"hydra:writable": true
}
],
"hydra:supportedOperation": [
{
"@type": "hydra:Operation",
"hydra:title": "Retrieves Dummy resource.",
"hydra:returns": "Dummy",
"hydra:method": "GET"
},
{
"@type": "hydra:ReplaceResourceOperation",
"hydra:title": "Replaces the Dummy resource.",
"hydra:expects": "Dummy",
"hydra:returns": "Dummy",
"hydra:method": "PUT"
},
{
"@type": "hydra:Operation",
"hydra:title": "Deletes the Dummy resource.",
"hydra:expects": "Dummy",
"hydra:method": "DELETE"
}
]
}
]
}
"""