Completed
Push — master ( 5c5133...4bc4b0 )
by Marcin
02:40
created

ModelAbstract::__construct()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 10.3696

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 12
cp 0.6667
rs 7.7777
c 0
b 0
f 0
cc 8
eloc 11
nc 8
nop 2
crap 10.3696
1
<?php
2
/**
3
 * MOJEPANSTWO-API
4
 *
5
 * Copyright © 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 */
14
15
/**
16
 * Created by Marcin.
17
 * Date: 28.10.2017
18
 * Time: 20:58
19
 */
20
declare (strict_types=1);
21
22
namespace mrcnpdlk\MojePanstwo\Model;
23
24
use mrcnpdlk\MojePanstwo\Api;
25
26
/**
27
 * Class ModelAbstract
28
 *
29
 * @package mrcnpdlk\MojePanstwo\Model
30
 */
31
class ModelAbstract extends \stdClass
32
{
33
    const CONTEXT = '';
34
35
    /**
36
     * ModelAbstract constructor.
37
     *
38
     * @param \stdClass|null $oData
39
     * @param \stdClass|null $oLayers
40
     */
41 6
    public function __construct(\stdClass $oData = null, \stdClass $oLayers = null)
42
    {
43 6
        if (!is_null($oData)) {
44 6
            foreach ($oData as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $oData of type object<stdClass> is not traversable.
Loading history...
45 6
                $key = $this->stripProperty($key);
46 6
                if (!property_exists($this, $key)) {
47
                    Api::getInstance()
48
                       ->getClient()
49
                       ->getLogger()
50
                       ->warning(sprintf('Property [%s] not exists in object [%s]', $key, get_class($this)))
51
                    ;
52 6
                } elseif (!is_array($value) && !is_object($value)) {
53 6
                    $this->{$key} = is_string($value) && $value === '' ? null : $value;
54
                }
55
            }
56
        }
57 6
    }
58
59
    /**
60
     * Cleaning telephone number
61
     *
62
     * @param $nr
63
     *
64
     * @return string|null
65
     */
66 2
    protected function cleanTelephoneNr($nr)
67
    {
68 2
        if (empty($nr)) {
69
            return null;
70
        } else {
71 2
            $nr = preg_replace('/[^0-9]/', '', strval($nr));
72
73
            //removing zeros only for national numbers (only single zero at the beginning)
74 2
            return preg_replace('/^0(?=([1-9]+[0-9]*))/', '', $nr);
75
        }
76
    }
77
78
    /**
79
     * Convert string to integer
80
     *
81
     * @param string $value
82
     *
83
     * @return int|null
84
     */
85 6
    protected function convertToId($value)
86
    {
87 6
        return empty($value) ? null : intval($value);
88
    }
89
90
    /**
91
     * Remove namesoace class from the property name
92
     *
93
     * @param string $property
94
     *
95
     * @return string
96
     */
97 6
    protected function stripProperty(string $property)
98
    {
99 6
        return str_replace(static::CONTEXT . '.', '', $property);
100
    }
101
102
}
103