Completed
Pull Request — master (#200)
by Carlos
04:52 queued 02:20
created

TopFee::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * TopFee.php
4
 *
5
 * Part of Overtrue\Wechat.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    a939638621 <[email protected]>
11
 * @copyright 2015 a939638621 <[email protected]>
12
 *
13
 * @link      https://github.com/a939638621
14
 */
15
namespace Overtrue\Wechat\Shop\Data;
16
17
use Overtrue\Wechat\Utils\MagicAttributes;
18
use Overtrue\Wechat\Shop\Foundation\ShopsException;
19
use Overtrue\Wechat\Shop\Postage;
20
21
/**
22
 * Class TopFee
23
 *
24
 * @property $custom
25
 * @property $normal
26
 * @property $topFee
27
 */
28
class TopFee extends MagicAttributes
29
{
30
    /**
31
     * 设置TopFee
32
     *
33
     * @param string $type
34
     *
35
     * @return $this
36
     *
37
     * @throws ShopsException
38
     */
39
    public function setTopFee($type = Postage::KUAI_DI)
40
    {
41
        if (!isset($this->normal) && !isset($this->custom)) {
42
            throw new ShopsException('normal 和 custom 必须设置');
43
        }
44
        if (!isset($this->normal) || !isset($this->custom)) {
45
            throw new ShopsException('normal 和 custom 必须全部设置');
46
        }
47
        $keys = array_keys($this->custom);
0 ignored issues
show
Documentation introduced by
The property custom does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48
49
        foreach ($keys as $v) {
50
            if (!is_numeric($v)) {
51
                throw new ShopsException('数据不合法');
52
            }
53
        }
54
55
        $this->attributes['topFee'][] = array(
56
            'Type' => $type,
57
            'Normal' => $this->normal,
0 ignored issues
show
Documentation introduced by
The property normal does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
            'Custom' => $this->custom,
0 ignored issues
show
Documentation introduced by
The property custom does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
59
        );
60
61
        return $this;
62
    }
63
64
    /**
65
     * 设置 Normal
66
     *
67
     * @param $startStandards
68
     * @param $startFees
69
     * @param $addStandards
70
     * @param $addFees
71
     *
72
     * @return $this
73
     */
74
    public function setNormal($startStandards, $startFees, $addStandards, $addFees)
75
    {
76
        $this->normal = array(
0 ignored issues
show
Documentation introduced by
The property normal does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
77
            'StartStandards' => $startStandards,
78
            'StartFees' => $startFees,
79
            'AddStandards' => $addStandards,
80
            'AddFees' => $addFees,
81
        );
82
83
        return $this;
84
    }
85
86
    /**
87
     * 设置Custom
88
     *
89
     * @param $startStandards
90
     * @param $startFees
91
     * @param $addStandards
92
     * @param $addFees
93
     * @param $destProvince
94
     * @param null   $destCity
95
     * @param string $destCountry
96
     *
97
     * @return $this
98
     *
99
     * @throws ShopsException
100
     */
101
    public function setCustom(
102
        $startStandards, $startFees, $addStandards, $addFees,
103
        $destProvince, $destCity = null, $destCountry = '中国'
104
    ) {
105
        if (empty($destProvince)) {
106
            throw new ShopsException('$destProvince不允许为空');
107
        }
108
109
        $this->custom = array();
0 ignored issues
show
Documentation introduced by
The property custom does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
111
        //todo 未做反选,排除一个城市,选择其他
112
113
        if (empty($destCity)) {
114
115
            //todo  $destProvince的判断
116
            //todo  加入 全国省直辖市的 简称等
117
            //todo 加入 某些不平等条约的存在 例如 江浙沪 ,你们懂得!!
118
119
            $destProvince = is_string($destProvince) ? array($destProvince) : $destProvince;
120
121
            $regional = new Regional();
122
123
            foreach ($destProvince as $province) {
124
                $citys = $regional->getCity($province);
125
126
                if (empty($citys)) {
127
                    throw new ShopsException('请传入合法的省份名!!!');
128
                }
129
130 View Code Duplication
                foreach ($citys[0] as $city) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
                    $this->attributes['custom'][] = array(
132
                        'StartStandards' => $startStandards,
133
                        'StartFees' => $startFees,
134
                        'AddStandards' => $addStandards,
135
                        'AddFees' => $addFees,
136
                        'DestCountry' => $destCountry,
137
                        'DestProvince' => $province,
138
                        'DestCity' => $city,
139
                    );
140
                }
141
            }
142
143
            return $this;
144
        } else {
145
146
            //todo 未做省份的检测
147
            //todo 未做市检测
148
149
            $destCity = is_string($destCity) ? array($destCity) : $destCity;
150
151
            if (is_array($destCity)) {
152 View Code Duplication
                foreach ($destCity as $city) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
                    $this->attributes['custom'][] = array(
154
                        'StartStandards' => $startStandards,
155
                        'StartFees' => $startFees,
156
                        'AddStandards' => $addStandards,
157
                        'AddFees' => $addFees,
158
                        'DestCountry' => $destCountry,
159
                        'DestProvince' => $destProvince,
160
                        'DestCity' => $city,
161
                    );
162
                }
163
            }
164
165
            return $this;
166
        }
167
    }
168
169
    public function __get($property)
170
    {
171
        return parent::__get($property);
172
    }
173
174
    public function __isset($property)
175
    {
176
        return isset($this->attributes[$this->snake($property)]);
177
    }
178
}
179