Passed
Push — master ( 2dad30...d8106d )
by Marcin
02:08
created

ModelAbstract::cleanTelephoneNr()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2.2559
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($value)) {
0 ignored issues
show
Bug introduced by
The variable $value seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
69
            $nr = preg_replace('/[^0-9]/', '', strval($nr));
70
71
            //removing zeros only for national numbers (only single zero at the beginning)
72
            return preg_replace('/^0(?=([1-9]+[0-9]*))/', '', $nr);
73
        }
74
75 2
        return null;
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