Completed
Push — master ( c6f12e...8beaec )
by Daniel
01:57
created

Basics::getPkgOptAtributeAll()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2017 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\composer_packages_listing;
30
31
trait Basics
32
{
33
34
    protected function getPkgAging($timePkg) {
35
        $dateTimeToday = new \DateTime(date('Y-m-d', strtotime('today')));
36
        $dateTime      = new \DateTime(date('Y-m-d', strtotime($timePkg)));
37
        $interval      = $dateTimeToday->diff($dateTime);
38
        return $interval->format('%a days ago');
39
    }
40
41
    private function getPkgBasicInfo($value, $defaultNA) {
42
        return [
43
            'License'      => (isset($value['license']) ? $this->getPkgLcns($value['license']) : $defaultNA),
44
            'Package Name' => $value['name'],
45
            'PHP required' => (isset($value['require']['php']) ? $value['require']['php'] : $defaultNA),
46
            'Product'      => explode('/', $value['name'])[1],
47
            'Vendor'       => explode('/', $value['name'])[0],
48
        ];
49
    }
50
51
    private function getPkgLcns($license) {
52
        $lcns = $license;
53
        if (is_array($license)) {
54
            $lcns = implode(', ', $license);
55
        }
56
        return $lcns;
57
    }
58
59
    private function getPkgOptAtributeAll($value, $defaultNA) {
60
        $attr    = ['description', 'homepage', 'type', 'url', 'version'];
61
        $aReturn = [];
62
        foreach ($attr as $valueA) {
63
            $aReturn[ucwords($valueA)] = $defaultNA;
64
            if (array_key_exists($valueA, $value)) {
65
                $aReturn[ucwords($valueA)] = $value[$valueA];
66
            }
67
        }
68
        return $aReturn;
69
    }
70
71
    protected function getPkgTiming($value, $defaultNA) {
72
        if (isset($value['time'])) {
73
            return [
74
                'Aging'           => $this->getPkgAging($value['time']),
75
                'Time'            => date('l, d F Y H:i:s', strtotime($value['time'])),
76
                'Time as PHP no.' => strtotime($value['time']),
77
            ];
78
        }
79
        return ['Aging' => $defaultNA, 'Time' => $defaultNA, 'Time as PHP no.' => $defaultNA];
80
    }
81
82
    private function getPkgVerNo($version) {
83
        $vrs = $version;
84
        if (substr($version, 0, 1) == 'v') {
85
            $vrs = substr($version, 1, strlen($version) - 1);
86
        }
87
        if (strpos($vrs, '-') !== false) {
88
            $vrs = substr($vrs, 0, strpos($vrs, '-'));
89
        }
90
        return $vrs;
91
    }
92
93
    protected function getPkgVersion($value, $defaultNA) {
94
        if (isset($value['version'])) {
95
            return [
96
                'Notification URL' => $value['notification-url'],
97
                'Version no.'      => $this->getPkgVerNo($value['version']),
98
            ];
99
        }
100
        return ['Notification URL' => $defaultNA, 'Version no.' => $defaultNA];
101
    }
102
103
}
104