Completed
Push — master ( 975c17...8f2777 )
by Adam
04:20 queued 01:49
created

ExtendedArrayTrait::arrayToPairString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\ExtendedArray;
4
5
use BestServedCold\PhalueObjects\String\StringTrait;
6
7
/**
8
 * Class ExtendedArrayTrait
9
 *
10
 * @package   BestServedCold\PhalueObjects\ExtendedArray
11
 * @author    Adam Lewis <[email protected]>
12
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
13
 * @license      http://http://opensource.org/licenses/GPL-3.0 GPL License
14
 * @link      http://bestservedcold.com
15
 * @since      0.0.1-alpha
16
 * @version   0.0.8-alpha
17
 */
18
trait ExtendedArrayTrait
19
{
20
    use StringTrait;
21
22
    /**
23
     * @param  array $array
24
     * @param  Integer $spaces
25
     * @return string
26
     */
27 12
    public function arrayToCommaString(array $array, $spaces)
28
    {
29 12
        return implode(",{$this->integerToSpace($spaces)}", $array);
0 ignored issues
show
Documentation introduced by
$spaces is of type integer, but the function expects a object<BestServedCold\Ph...s\Mathematical\Integer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
    }
31
32
    /**
33
     * @param  array  $array
34
     * @return string
35
     */
36 1
    public function arrayToPairString(array $array)
37
    {
38
        return implode(',', array_map(function($key, $value) {
39 1
            return $key.'='.$value;
40 1
        }, array_keys($array), $array));
41
    }
42
43
    /**
44
     * @param  array $array
45
     * @return string
46
     */
47
    public function arrayToAttributeArray(array $array)
48
    {
49
        return implode(' ', array_map(function($key, $value) {
50
            return is_null($value) ? $key : $key.'="'.$value.'"';
51
        }, array_keys($array), $array));
52
    }
53
54
    /**
55
     * @param  $json
56
     * @return array
57
     */
58 3
    public static function getArrayUsingJsonNotation($json)
59
    {
60 3
        return explode('.', $json);
61
    }
62
63
    /**
64
     * Get an item from an array using "dot" notation.
65
     *
66
     * @param array  $array
67
     * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
68
     * @param mixed  $default
69
     *
70
     * @return mixed
71
     */
72 2
    public static function getFromArrayUsingJsonNotation(
73
        $array,
74
        $key = null,
75
        $default = null
76
    ) {
77 2
        if (is_null($key)) {
78 1
            return $array;
79
        }
80
81 2
        if (isset($array[ $key ])) {
82 2
            return $array[ $key ];
83
        }
84
85 2
        foreach (self::getArrayUsingJsonNotation($key) as $segment) {
86 2
            if (!is_array($array) || !array_key_exists($segment, $array)) {
87 1
                return $default;
88
            }
89 2
            $array = $array[ $segment ];
90 2
        }
91
92 2
        return $array;
93
    }
94
95
    /**
96
     * @param array $array
97
     * @param bool|false $key
98
     * @return null
99
     */
100
    public static function nullIfNotSet(array $array, $key = false)
101
    {
102
        return isset($array[ $key ]) ? $array[ $key ] : null;
103
    }
104
105
    /**
106
     * @param array $array
107
     * @param bool|false $key
108
     * @return bool
109
     */
110
    public static function falseIfNotSet(array $array, $key = false)
0 ignored issues
show
Coding Style introduced by
function falseIfNotSet() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
111
    {
112
        return isset($array[ $key ]) ? $array[ $key ] : false;
113
    }
114
}
115