Completed
Push — pac-335--slash-handling ( 2b9e5a...3b834c )
by Tim
06:14
created

CategoryPathUtil::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Utils\CategoryPathUtil
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2021 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Utils;
22
23
use TechDivision\Import\Serializer\SerializerInterface;
24
25
/**
26
 * Utility class containing the entities member names.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2021 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class CategoryPathUtil implements CategoryPathUtilInterface
35
{
36
37
    const SEPARATOR = '/';
38
39
    /**
40
     * The serializer instance.
41
     *
42
     * @var \TechDivision\Import\Serializer\SerializerInterface
43
     */
44
    private $serializer;
45
46
    /**
47
     *
48
     * @param \TechDivision\Import\Serializer\SerializerInterface $serialzier
0 ignored issues
show
Documentation introduced by
There is no parameter named $serialzier. Did you maybe mean $serializer?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
49
     */
50
    public function __construct(SerializerInterface $serializer)
51
    {
52
        $this->serializer = $serializer;
53
    }
54
55
    public function fromProduct(string $path = null) : array
56
    {
57
58
59
        $categories = array();
60
61
        // load and extract the categories from the CSV file
62
        if ($paths = $this->unserialize($path)) {
63
            // create a tree of categories that has to be created
64
            foreach ($paths as $path) {
65
                // explode the category elements
66
                $categories[$path] = $this->explode($path);
67
            }
68
        }
69
70
        return $categories;
71
    }
72
73
    public function fromCategory(string $path = null) : array
74
    {
75
        return $this->explode($path);
76
    }
77
78
    public function toProduct(array $paths) : string
79
    {
80
81
        $cats = array();
82
83
        foreach ($paths as $elements) {
84
            $cats[] = $this->implode($elements);
85
        }
86
87
        return $this->serialize($cats);
88
    }
89
90
    /**
91
     * Create a CSV compatible string from the passed category path.
92
     *
93
     * @param string The normalized category path (usually from the DB)
94
     *
95
     * @return string The denormalized category path for the import file
96
     * @see \TechDivision\Import\Utils\CategoryPathUtilInterface::denormalize()
97
     */
98
    public function denormalize(string $path) : string
99
    {
100
        return $this->serialize(array($path));
101
    }
102
103
    public function normalize(string $path) : string
104
    {
105
        return $this->implode($this->explode($path));
106
    }
107
108
109
    public function explode(string $path) : array
110
    {
111
        return $this->unserialize($path, CategoryPathUtil::SEPARATOR);
112
    }
113
114
    public function implode(array $elements) : string
115
    {
116
        return $this->serialize($elements, CategoryPathUtil::SEPARATOR);
117
    }
118
119
    /**
120
     * Serializes the elements of the passed array.
121
     *
122
     * @param array|null  $unserialized The serialized data
123
     * @param string|null $delimiter    The delimiter used to serialize the values
124
     *
125
     * @return string The serialized array
126
     */
127
    public function serialize(array $unserialized = null, $delimiter = null)
128
    {
129
        return $this->serializer->serialize($unserialized, $delimiter);
130
    }
131
132
    /**
133
     * Unserializes the elements of the passed string.
134
     *
135
     * @param string|null $serialized The value to unserialize
136
     * @param string|null $delimiter  The delimiter used to unserialize the elements
137
     *
138
     * @return array The unserialized values
139
     */
140
    public function unserialize($serialized = null, $delimiter = null)
141
    {
142
        return $this->serializer->unserialize($serialized, $delimiter);
143
    }
144
}
145