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

NameMapper::process()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
rs 5.3846
cc 8
eloc 16
nc 10
nop 3
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
}