PackageNode::hasAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * Copyright 2012 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Composer\Graph;
20
21
class PackageNode
22
{
23
    /**
24
     * @var string
25
     */
26
    private $repositoryId;
27
    /**
28
     * @var string
29
     */
30
    private $name;
31
    /**
32
     * @var array
33
     */
34
    private $data;
35
    /**
36
     * @var string
37
     */
38
    private $version;
39
    /**
40
     * @var string
41
     */
42
    private $sourceReference;
43
    /**
44
     * @var DependencyEdge[]
45
     */
46
    private $inEdges = array();
47
    /**
48
     * @var DependencyEdge[]
49
     */
50
    private $outEdges = array();
51
    /**
52
     * @var array
53
     */
54
    private $attributes;
55
56
    /**
57
     * @param string $name
58
     * @param array  $data
59
     * @param array  $attributes
60
     */
61
    public function __construct($name, array $data = array(), array $attributes = array())
62
    {
63
        $this->name = $name;
64
        $this->data = $data;
65
        $this->attributes = $attributes;
66
    }
67
68
    /**
69
     * @param string $id
70
     */
71
    public function setRepositoryId($id)
72
    {
73
        $this->repositoryId = $id;
74
    }
75
76
    public function isPhpExtension()
77
    {
78
        return 0 === stripos($this->getQualifiedName(), 'ext-');
79
    }
80
81
    public function isPhpRuntime()
82
    {
83
        return strtolower($this->getQualifiedName()) === 'php';
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getQualifiedName()
90
    {
91
        if ( ! $this->hasAttribute('dir')) {
92
            return $this->name;
93
        }
94
95
        $repositoryId = $this->repositoryId ?: 'packagist';
96
97
        return $repositoryId.'__'.$this->name;
98
    }
99
100
    /**
101
     * @param string $key
102
     * @param string $value
103
     */
104
    public function setAttribute($key, $value)
105
    {
106
        $this->attributes[$key] = $value;
107
    }
108
109
    /**
110
     * @param $key
111
     *
112
     * @return bool
113
     */
114
    public function hasAttribute($key)
115
    {
116
        return isset($this->attributes[$key]);
117
    }
118
119
    /**
120
     * @param $key
121
     *
122
     * @return mixed
123
     * @throws \InvalidArgumentException
124
     */
125
    public function getAttribute($key)
126
    {
127
        if ( ! isset($this->attributes[$key])) {
128
            throw new \InvalidArgumentException(sprintf('The attribute "%s" does not exist.', $key));
129
        }
130
131
        return $this->attributes[$key];
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    public function getData()
138
    {
139
        return $this->data;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getName()
146
    {
147
        return $this->name;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getVersion()
154
    {
155
        return $this->version;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getSourceReference()
162
    {
163
        return $this->sourceReference;
164
    }
165
166
    /**
167
     * @param $version
168
     */
169
    public function setVersion($version)
170
    {
171
        $this->version = $version;
172
    }
173
174
    /**
175
     * @param $ref
176
     */
177
    public function setSourceReference($ref)
178
    {
179
        $this->sourceReference = $ref;
180
    }
181
182
    /**
183
     * @return DependencyEdge[]
184
     */
185
    public function getInEdges()
186
    {
187
        return $this->inEdges;
188
    }
189
190
    /**
191
     * @return DependencyEdge[]
192
     */
193
    public function getOutEdges()
194
    {
195
        return $this->outEdges;
196
    }
197
198
    /**
199
     * @param DependencyEdge $edge
200
     */
201
    public function addInEdge(DependencyEdge $edge)
202
    {
203
        $this->inEdges[] = $edge;
204
    }
205
206
    /**
207
     * @param DependencyEdge $edge
208
     */
209
    public function addOutEdge(DependencyEdge $edge)
210
    {
211
        $this->outEdges[] = $edge;
212
    }
213
214
    /**
215
     * @param $package
216
     *
217
     * @return bool
218
     */
219
    public function replaces($package)
220
    {
221
        return $this->hasDataPackageKey('replace', $package);
222
    }
223
224
    /**
225
     * Checks if this package has the given $packageName in its $data[$dataKey] hash.
226
     *
227
     * @param string $dataKey
228
     * @param string $packageName
229
     *
230
     * @return bool
231
     */
232
    public function hasDataPackageKey($dataKey, $packageName)
233
    {
234
        if (isset($this->data[$dataKey]) && is_array($this->data[$dataKey])) {
235
            $packageName = strtolower($packageName);
236
            foreach (array_keys($this->data[$dataKey]) as $k) {
237
                if (strtolower($k) === $packageName) {
238
                    return true;
239
                }
240
            }
241
        }
242
        return false;
243
    }
244
245
    /**
246
     * @return string
247
     */
248
    public function __toString()
249
    {
250
        return sprintf('PackageNode(qualifiedName = %s, version = %s, ref = %s, hasDir = %s)',
251
                $this->getQualifiedName(),
252
                $this->version,
253
                $this->sourceReference ?: 'null',
254
                $this->hasAttribute('dir') ? 'true' : 'false');
255
    }
256
}