Skip to content

Commit

Permalink
Rename annotation attributes to fields
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Aug 27, 2022
1 parent 561dac8 commit aeb66bb
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 53 deletions.
16 changes: 8 additions & 8 deletions src/Extracting/Strategies/Responses/UseApiResourceTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ private function getStatusCodeAndApiResourceClass(Tag $tag): array
$status = $result[1] ?: 0;
$content = $result[2];

['attributes' => $attributes, 'content' => $content] = a::parseIntoContentAndAttributes($content, ['status', 'scenario']);
['fields' => $fields, 'content' => $content] = a::parseIntoContentAndFields($content, ['status', 'scenario']);

$status = $attributes['status'] ?: $status;
$status = $fields['status'] ?: $status;
$apiResourceClass = $content;
$description = $attributes['scenario'] ? "$status, {$attributes['scenario']}" : "$status";
$description = $fields['scenario'] ? "$status, {$fields['scenario']}" : "$status";

$isCollection = strtolower($tag->getName()) == 'apiresourcecollection';
return [(int)$status, $description, $apiResourceClass, $isCollection];
Expand All @@ -94,10 +94,10 @@ private function getClassToBeTransformedAndAttributes(array $tags): array
$pagination = [];

if ($modelTag) {
['content' => $modelClass, 'attributes' => $attributes] = a::parseIntoContentAndAttributes($modelTag->getContent(), ['states', 'with', 'paginate']);
$states = $attributes['states'] ? explode(',', $attributes['states']) : [];
$relations = $attributes['with'] ? explode(',', $attributes['with']) : [];
$pagination = $attributes['paginate'] ? explode(',', $attributes['paginate']) : [];
['content' => $modelClass, 'fields' => $fields] = a::parseIntoContentAndFields($modelTag->getContent(), ['states', 'with', 'paginate']);
$states = $fields['states'] ? explode(',', $fields['states']) : [];
$relations = $fields['with'] ? explode(',', $fields['with']) : [];
$pagination = $fields['paginate'] ? explode(',', $fields['paginate']) : [];
}

if (empty($modelClass)) {
Expand All @@ -117,7 +117,7 @@ private function getClassToBeTransformedAndAttributes(array $tags): array
private function getAdditionalData(array $tags): array
{
$tag = Arr::first(Utils::filterDocBlockTags($tags, 'apiresourceadditional'));
return $tag ? a::parseIntoAttributes($tag->getContent()) : [];
return $tag ? a::parseIntoFields($tag->getContent()) : [];
}

public function getApiResourceTag(array $tags): ?Tag
Expand Down
6 changes: 3 additions & 3 deletions src/Extracting/Strategies/Responses/UseResponseFileTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function getFileResponses(array $tags): ?array
[$_, $status, $mainContent] = $result;
$json = $result[3] ?? null;

['attributes' => $attributes, 'content' => $filePath] = a::parseIntoContentAndAttributes($mainContent, ['status', 'scenario']);
['fields' => $fields, 'content' => $filePath] = a::parseIntoContentAndFields($mainContent, ['status', 'scenario']);

$status = $attributes['status'] ?: ($status ?: 200);
$description = $attributes['scenario'] ? "$status, {$attributes['scenario']}" : "$status";
$status = $fields['status'] ?: ($status ?: 200);
$description = $fields['scenario'] ? "$status, {$fields['scenario']}" : "$status";
$content = ResponseFileTools::getResponseContents($filePath, $json);

return [
Expand Down
6 changes: 3 additions & 3 deletions src/Extracting/Strategies/Responses/UseResponseTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function getDocBlockResponses(array $tags): ?array
$status = $result[1] ?: 200;
$content = $result[2] ?: '{}';

['attributes' => $attributes, 'content' => $content] = a::parseIntoContentAndAttributes($content, ['status', 'scenario']);
['fields' => $fields, 'content' => $content] = a::parseIntoContentAndFields($content, ['status', 'scenario']);

$status = $attributes['status'] ?: $status;
$description = $attributes['scenario'] ? "$status, {$attributes['scenario']}" : "$status";
$status = $fields['status'] ?: $status;
$description = $fields['scenario'] ? "$status, {$fields['scenario']}" : "$status";

return ['content' => $content, 'status' => (int) $status, 'description' => $description];
}, $responseTags);
Expand Down
8 changes: 4 additions & 4 deletions src/Extracting/Strategies/Responses/UseTransformerTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ private function getClassToBeTransformed(array $tags, ReflectionFunctionAbstract
$relations = [];
$resourceKey = null;
if ($modelTag) {
['content' => $type, 'attributes' => $attributes] = a::parseIntoContentAndAttributes($modelTag->getContent(), ['states', 'with', 'resourceKey']);
$states = $attributes['states'] ? explode(',', $attributes['states']) : [];
$relations = $attributes['with'] ? explode(',', $attributes['with']) : [];
$resourceKey = $attributes['resourceKey'] ?? null;
['content' => $type, 'fields' => $fields] = a::parseIntoContentAndFields($modelTag->getContent(), ['states', 'with', 'resourceKey']);
$states = $fields['states'] ? explode(',', $fields['states']) : [];
$relations = $fields['with'] ? explode(',', $fields['with']) : [];
$resourceKey = $fields['resourceKey'] ?? null;
} else {
$parameter = Arr::first($transformerMethod->getParameters());
if ($parameter->hasType() && !$parameter->getType()->isBuiltin() && class_exists($parameter->getType()->getName())) {
Expand Down
30 changes: 15 additions & 15 deletions src/Tools/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@ class AnnotationParser
* Fields are always optional and may appear at the start or the end of the string.
*
* @param string $annotationContent
* @param array $allowedAttributes List of attributes to look for.
* @param array $allowedFields List of fields to look for.
*
* @return array{content: string, attributes: string[]}
* @return array{content: string, fields: string[]}
*/
public static function parseIntoContentAndAttributes(string $annotationContent, array $allowedAttributes): array
public static function parseIntoContentAndFields(string $annotationContent, array $allowedFields): array
{
$parsedAttributes = array_fill_keys($allowedAttributes, null);
$parsedFields = array_fill_keys($allowedFields, null);

foreach ($allowedAttributes as $attribute) {
preg_match("/$attribute=([^\\s'\"]+|\".+?\"|'.+?')\\s*/", $annotationContent, $attributeAndValue);
foreach ($allowedFields as $field) {
preg_match("/$field=([^\\s'\"]+|\".+?\"|'.+?')\\s*/", $annotationContent, $fieldAndValue);

if (count($attributeAndValue)) {
[$matchingText, $attributeValue] = $attributeAndValue;
if (count($fieldAndValue)) {
[$matchingText, $attributeValue] = $fieldAndValue;
$annotationContent = str_replace($matchingText, '', $annotationContent);

$parsedAttributes[$attribute] = trim($attributeValue, '"\' ');
$parsedFields[$field] = trim($attributeValue, '"\' ');
}
}

return [
'content' => trim($annotationContent),
'attributes' => $parsedAttributes
'fields' => $parsedFields
];
}

/**
* Parse an annotation like 'title=This message="everything good"' into a key-value array.
* All non key-value fields will be ignored. Useful for `@apiResourceAdditional`,
* where users may specify arbitrary attributes.
* where users may specify arbitrary fields.
*
* @param string $annotationContent
* @return array
*/
public static function parseIntoAttributes(string $annotationContent): array
public static function parseIntoFields(string $annotationContent): array
{
$attributes = $matches = [];
$fields = $matches = [];

preg_match_all(
'/([^\s\'"]+|".+?"|\'.+?\')=([^\s\'"]+|".+?"|\'.+?\')/',
Expand All @@ -54,9 +54,9 @@ public static function parseIntoAttributes(string $annotationContent): array
);

foreach ($matches as $match) {
$attributes[trim($match[1], '"\' ')] = trim($match[2], '"\' ');
$fields[trim($match[1], '"\' ')] = trim($match[2], '"\' ');
}

return $attributes;
return $fields;
}
}
2 changes: 1 addition & 1 deletion tests/Strategies/Responses/UseResponseFileTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function responseFileTags()
],
],

"with attributes" => [
"with fields" => [
[
new Tag('responseFile', 'scenario="success" tests/Fixtures/response_test.json'),
new Tag('responseFile', 'status=401 scenario=\'auth problem\' tests/Fixtures/response_error_test.json'),
Expand Down
2 changes: 1 addition & 1 deletion tests/Strategies/Responses/UseResponseTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function responseTags()
],
],

"with attributes" => [
"with fields" => [
[
new Tag('response', "scenario=\"success\" $response1"),
new Tag('response', "status=401 scenario='auth problem' $response2"),
Expand Down
36 changes: 18 additions & 18 deletions tests/Unit/AnnotationParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@ class AnnotationParserTest extends TestCase
{
/**
* @test
* @dataProvider annotationsWithContentAndAttributes
* @dataProvider annotationsWithContentAndFields
*/
public function can_parse_annotation_into_content_and_attributes(string $annotation, array $expected)
public function can_parse_annotation_into_content_and_fields(string $annotation, array $expected)
{
$result = AnnotationParser::parseIntoContentAndAttributes($annotation, ['status', 'scenario']);
$result = AnnotationParser::parseIntoContentAndFields($annotation, ['status', 'scenario']);

$this->assertEquals($expected, $result);
}

public function annotationsWithContentAndAttributes()
public function annotationsWithContentAndFields()
{
return [
"when attributes come first" => [
"when fields come first" => [
'status=400 scenario="things go wrong" {"message": "failed"}',
[
'attributes' => ['status' => '400', 'scenario' => 'things go wrong'],
'fields' => ['status' => '400', 'scenario' => 'things go wrong'],
'content' => '{"message": "failed"}',
],
],
"when attributes come last" => [
"when fields come last" => [
'{"message": "failed"} status=400 scenario="things go wrong"',
[
'attributes' => ['status' => '400', 'scenario' => 'things go wrong'],
'fields' => ['status' => '400', 'scenario' => 'things go wrong'],
'content' => '{"message": "failed"}',
],
],
"when there are no attributes" => [
"when there are no fields" => [
'{"message": "failed"} ',
[
'attributes' => ['status' => null, 'scenario' => null],
'fields' => ['status' => null, 'scenario' => null],
'content' => '{"message": "failed"}',
],
],
"when there are some attributes" => [
"when there are some fields" => [
' status=hey {"message": "failed"} ',
[
'attributes' => ['status' => 'hey', 'scenario' => null],
'fields' => ['status' => 'hey', 'scenario' => null],
'content' => '{"message": "failed"}',
],
],
Expand All @@ -54,16 +54,16 @@ public function annotationsWithContentAndAttributes()

/**
* @test
* @dataProvider annotationsWithAttributes
* @dataProvider annotationsWithFields
*/
public function can_parse_annotation_into_attributes(string $annotation, array $expected)
public function can_parse_annotation_into_fields(string $annotation, array $expected)
{
$result = AnnotationParser::parseIntoAttributes($annotation);
$result = AnnotationParser::parseIntoFields($annotation);

$this->assertEquals($expected, $result);
}

public function annotationsWithAttributes()
public function annotationsWithFields()
{
return [
"with or without quotes" => [
Expand All @@ -75,11 +75,11 @@ public function annotationsWithAttributes()
'snaked_data' => 'value'
]
],
"no attributes" => [
"no fields" => [
'{"message": "failed"}',
[]
],
"attributes with empty values" => [
"fields with empty values" => [
'title= message="everything good"',
[
'message' => 'everything good'
Expand Down

0 comments on commit aeb66bb

Please sign in to comment.