Completed
Push — master ( 634fb7...2d8b8d )
by Viacheslav
9s
created

NameMapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
wmc 8
lcom 0
cbo 2
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 26 8
1
<?php
2
3
namespace Swaggest\JsonSchema\PreProcessor;
4
5
6
use Swaggest\JsonSchema\DataPreProcessor;
7
use Swaggest\JsonSchema\Meta\FieldName;
8
use Swaggest\JsonSchema\Schema;
9
10
class NameMapper implements DataPreProcessor
11
{
12
    public function process($data, Schema $schema, $import = true)
13
    {
14
        if ($schema->properties !== null && is_object($data)) {
15
            $result = new \stdClass();
16
            foreach ($schema->properties->toArray() as $propertyName => $property) {
17
                if ($fieldNameMeta = FieldName::get($property)) {
18
                    $fieldName = $fieldNameMeta->name;
19
                } else {
20
                    $fieldName = $propertyName;
21
                }
22
23
                if ($import) {
24
                    if (property_exists($data, $fieldName)) {
25
                        $result->$propertyName = $data->$fieldName;
26
                    }
27
                } else {
28
                    if (property_exists($data, $propertyName)) {
29
                        $result->$fieldName = $data->$propertyName;
30
                    }
31
                }
32
            }
33
            return $result;
34
        }
35
36
        return $data;
37
    }
38
}