GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TShippingAddress::cleanString()
last analyzed

Size

Total Lines 1

Duplication

Lines 1
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 1
loc 1
nc 1
1
<?php
2
/**
3
 * Copyright (c) 2013-2014 eBay Enterprise, Inc.
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the Open Software License (OSL 3.0)
8
 * that is bundled with this package in the file LICENSE.md.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * @copyright   Copyright (c) 2013-2014 eBay Enterprise, Inc. (http://www.ebayenterprise.com/)
13
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
14
 */
15
16
namespace eBayEnterprise\RetailOrderManagement\Payload\Payment;
17
18 View Code Duplication
trait TShippingAddress
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
19
{
20
    /** @var array */
21
    protected $shipToLines;
22
    /** @var string */
23
    protected $shipToCity;
24
    /** @var string */
25
    protected $shipToMainDivision;
26
    /** @var string */
27
    protected $shipToCountryCode;
28
    /** @var string */
29
    protected $shipToPostalCode;
30
31
    public function getShipToLines()
32
    {
33
        return is_array($this->shipToLines) ? implode("\n", $this->shipToLines) : null;
34
    }
35
36
    public function setShipToLines($lines)
37
    {
38
        $this->shipToLines = $this->cleanAddressLines($lines);
39
        return $this;
40
    }
41
42
    /**
43
     * Make sure we have max 4 address lines of 70 chars max
44
     *
45
     * If there are more than 4 lines concatenate all extra lines with the 4th line.
46
     *
47
     * Truncate any lines to 70 chars max.
48
     *
49
     * @param string $lines
50
     * @return array or null
51
     */
52
    protected function cleanAddressLines($lines)
53
    {
54
        $finalLines = null;
55
56
        if (is_string($lines)) {
57
            $trimmed = trim($lines);
58
            $addressLines = preg_split("/\n/", $trimmed, null, PREG_SPLIT_NO_EMPTY);
59
60
            $newLines = [];
61
            foreach ($addressLines as $line) {
62
                $newLines[] = $this->cleanString($line, 70);
63
            }
64
65
            if (count($newLines) > 4) {
66
                // concat lines beyond the four allowed down into the last line
67
                $newLines[3] = $this->cleanString(implode(' ', array_slice($newLines, 3)), 70);
68
            }
69
70
            $finalLines = array_slice($newLines, 0, 4);
71
        }
72
73
        return empty($finalLines) ? null : $finalLines;
74
    }
75
76
    /**
77
     * Aggregate the shipTo address lines into the ShippingAddress node. This is an optional node.
78
     *
79
     * @return string
80
     */
81
    protected function serializeShippingAddress()
82
    {
83
        $lines = [];
84
        $idx = 0;
85
        $shipToLines = is_array($this->shipToLines) ? $this->shipToLines : [];
86
        foreach ($shipToLines as $line) {
87
            $idx++;
88
            $lines[] = sprintf(
89
                '<Line%d>%s</Line%1$d>',
90
                $idx,
91
                $this->xmlEncode($line)
92
            );
93
        }
94
        // If we don't have any address lines, we treat as having no address at all.
95
        return ($idx) ? $this->buildShippingAddressNode($lines) : '';
96
    }
97
98
    /**
99
     * Build the Shipping Address Node
100
     * @param array lines Street Address
101
     * @return type
102
     */
103
    protected function buildShippingAddressNode(array $lines)
104
    {
105
        return sprintf(
106
            '<ShippingAddress>%s<City>%s</City>%s<CountryCode>%s</CountryCode>%s</ShippingAddress>',
107
            implode('', $lines),
108
            $this->xmlEncode($this->getShipToCity()),
109
            $this->serializeOptionalXmlEncodedValue('MainDivision', $this->getShipToMainDivision()),
110
            $this->xmlEncode($this->getShipToCountryCode()),
111
            $this->serializeOptionalXmlEncodedValue('PostalCode', $this->getShipToPostalCode())
112
        );
113
    }
114
115
    public function getShipToCity()
116
    {
117
        return $this->shipToCity;
118
    }
119
120
    public function setShipToCity($city)
121
    {
122
        $this->shipToCity = $this->cleanString($city, 35);
123
        return $this;
124
    }
125
126
    public function getShipToMainDivision()
127
    {
128
        return $this->shipToMainDivision;
129
    }
130
131
    public function setShipToMainDivision($div)
132
    {
133
        $this->shipToMainDivision = $this->cleanString($div, 35);
134
        return $this;
135
    }
136
137
    public function getShipToCountryCode()
138
    {
139
        return $this->shipToCountryCode;
140
    }
141
142
    public function setShipToCountryCode($code)
143
    {
144
        $cleaned = $this->cleanString($code, 40);
145
        $this->shipToCountryCode = strlen($cleaned) >= 2 ? $cleaned : null;
146
        return $this;
147
    }
148
149
    public function getShipToPostalCode()
150
    {
151
        return $this->shipToPostalCode;
152
    }
153
154
    public function setShipToPostalCode($code)
155
    {
156
        $this->shipToPostalCode = $this->cleanString($code, 15);
157
        return $this;
158
    }
159
160
    /**
161
     * Trim any white space and return the resulting string truncating to $maxLength.
162
     *
163
     * Return null if the result is an empty string or not a string
164
     *
165
     * @param string $string
166
     * @param int $maxLength
167
     * @return string or null
168
     */
169
    abstract protected function cleanString($string, $maxLength);
170
171
    /**
172
     * Serialize an optional element containing a string. The value will be
173
     * xml-encoded if is not null.
174
     *
175
     * @param string
176
     * @param string
177
     * @return string
178
     */
179
    abstract protected function serializeOptionalXmlEncodedValue($name, $value);
180
181
    /**
182
     * encode the passed in string to be safe for xml if it is not null,
183
     * otherwise simply return the null parameter.
184
     *
185
     * @param string|null
186
     * @return string|null
187
     */
188
    abstract protected function xmlEncode($value = null);
189
}
190