ModelAbstract   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 3
dl 0
loc 75
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 17 8
A cleanTelephoneNr() 0 12 2
A convertToId() 0 4 2
A stripProperty() 0 4 1
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
     * @throws \mrcnpdlk\MojePanstwo\Exception
42
     */
43 7
    public function __construct(\stdClass $oData = null, \stdClass $oLayers = null)
44
    {
45 7
        if (null !== $oData) {
46 7
            foreach ($oData as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $oData of type object<stdClass> is not traversable.
Loading history...
47 7
                $key = $this->stripProperty($key);
48 7
                if (!property_exists($this, $key)) {
49
                    Api::getInstance()
50
                       ->getClient()
51
                       ->getLogger()
52
                       ->warning(sprintf('Property [%s] not exists in object [%s]', $key, \get_class($this)))
53
                    ;
54 7
                } elseif (!\is_array($value) && !\is_object($value)) {
55 7
                    $this->{$key} = \is_string($value) && $value === '' ? null : $value;
56
                }
57
            }
58
        }
59 7
    }
60
61
    /**
62
     * Cleaning telephone number
63
     *
64
     * @param $nr
65
     *
66
     * @return string|null
67
     */
68 2
    protected function cleanTelephoneNr($nr)
69
    {
70 2
        if (!empty($nr)) {
71
            // removing non digit chars
72 2
            $nr = preg_replace('/\D/', '', (string)$nr);
73
74
            //removing zeros only for national numbers (only single zero at the beginning)
75 2
            return preg_replace('/^0(?=([1-9]+\d*))/', '', $nr);
76
        }
77
78
        return null;
79
    }
80
81
    /**
82
     * Convert string to integer
83
     *
84
     * @param string $value
85
     *
86
     * @return int|null
87
     */
88 7
    protected function convertToId($value)
89
    {
90 7
        return empty($value) ? null : (int)$value;
91
    }
92
93
    /**
94
     * Remove namespace class from the property name
95
     *
96
     * @param string $property
97
     *
98
     * @return string
99
     */
100 7
    protected function stripProperty(string $property): string
101
    {
102 7
        return str_replace(static::CONTEXT . '.', '', $property);
103
    }
104
105
}
106