Просмотр исходного кода

Rename annotation attributes to fields

shalvah 2 лет назад
Родитель
Сommit
aeb66bb0b8

+ 8 - 8
src/Extracting/Strategies/Responses/UseApiResourceTags.php

@@ -74,11 +74,11 @@ class UseApiResourceTags extends Strategy
         $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];
@@ -94,10 +94,10 @@ class UseApiResourceTags extends Strategy
         $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)) {
@@ -117,7 +117,7 @@ class UseApiResourceTags extends Strategy
     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

+ 3 - 3
src/Extracting/Strategies/Responses/UseResponseFileTag.php

@@ -35,10 +35,10 @@ class UseResponseFileTag extends Strategy
             [$_, $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 [

+ 3 - 3
src/Extracting/Strategies/Responses/UseResponseTag.php

@@ -36,10 +36,10 @@ class UseResponseTag extends Strategy
             $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);

+ 4 - 4
src/Extracting/Strategies/Responses/UseTransformerTags.php

@@ -87,10 +87,10 @@ class UseTransformerTags extends Strategy
         $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())) {

+ 15 - 15
src/Tools/AnnotationParser.php

@@ -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\'"]+|".+?"|\'.+?\')/',
@@ -54,9 +54,9 @@ class AnnotationParser
         );
 
         foreach ($matches as $match) {
-            $attributes[trim($match[1], '"\' ')] = trim($match[2], '"\' ');
+            $fields[trim($match[1], '"\' ')] = trim($match[2], '"\' ');
         }
 
-        return $attributes;
+        return $fields;
     }
 }

+ 1 - 1
tests/Strategies/Responses/UseResponseFileTagTest.php

@@ -60,7 +60,7 @@ class UseResponseFileTagTest extends TestCase
                 ],
             ],
 
-            "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'),

+ 1 - 1
tests/Strategies/Responses/UseResponseTagTest.php

@@ -63,7 +63,7 @@ class UseResponseTagTest extends TestCase
                 ],
             ],
 
-            "with attributes" => [
+            "with fields" => [
                 [
                     new Tag('response', "scenario=\"success\" $response1"),
                     new Tag('response', "status=401 scenario='auth problem' $response2"),

+ 18 - 18
tests/Unit/AnnotationParserTest.php

@@ -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"}',
                 ],
             ],
@@ -54,16 +54,16 @@ class AnnotationParserTest extends TestCase
 
     /**
      * @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" => [
@@ -75,11 +75,11 @@ class AnnotationParserTest extends TestCase
                     'snaked_data' => 'value'
                 ]
             ],
-            "no attributes" => [
+            "no fields" => [
                 '{"message": "failed"}',
                 []
             ],
-            "attributes with empty values" => [
+            "fields with empty values" => [
                 'title= message="everything good"',
                 [
                     'message' => 'everything good'