Completed
Push — master ( 5b8d73...30d679 )
by Adam
21:09
created

ExtendedArrayTrait::arrayToCommaString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\ExtendedArray;
4
5
use BestServedCold\PhalueObjects\Mathematical\Integer;
6
use BestServedCold\PhalueObjects\String\StringTrait;
7
8
/**
9
 * Class ExtendedArrayTrait
10
 *
11
 * @package   BestServedCold\PhalueObjects\ExtendedArray
12
 * @author    Adam Lewis <[email protected]>
13
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
14
 * @license	  http://http://opensource.org/licenses/GPL-3.0 GPL License
15
 * @link	  http://bestservedcold.com
16
 * @since	  0.0.1-alpha
17
 * @version   0.0.8-alpha
18
 */
19
trait ExtendedArrayTrait
20
{
21
    use StringTrait;
22
23
    /**
24
     * @param  array $array
25
     * @param  Integer $spaces
26
     * @return string
27
     */
28 11
    public function arrayToCommaString(array $array, Integer $spaces)
29
    {
30 11
        return implode(",{$this->integerToSpace($spaces)}", $array);
31
    }
32
33
    /**
34
     * @param  $json
35
     * @return array
36
     */
37 3
    public static function getArrayUsingJsonNotation($json)
38
    {
39 3
        return explode('.', $json);
40
    }
41
42
    /**
43
     * Get an item from an array using "dot" notation.
44
     *
45
     * @param array  $array
46
     * @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...
47
     * @param mixed  $default
48
     *
49
     * @return mixed
50
     */
51 2
    public static function getFromArrayUsingJsonNotation(
52
        $array,
53
        $key = null,
54
        $default = null
55
    ) {
56 2
        if (is_null($key)) {
57 1
            return $array;
58
        }
59
60 2
        if (isset($array[ $key ])) {
61 2
            return $array[ $key ];
62
        }
63
64 2
        foreach (self::getArrayUsingJsonNotation($key) as $segment) {
65 2
            if (!is_array($array) || !array_key_exists($segment, $array)) {
66 1
                return $default;
67
            }
68 2
            $array = $array[ $segment ];
69 2
        }
70
71 2
        return $array;
72
    }
73
74
    /**
75
     * @param array $array
76
     * @param bool|false $key
77
     * @return null
78
     */
79
    public static function nullIfNotSet(array $array, $key = false)
80
    {
81
        return isset($array[$key]) ? $array[$key] : null;
82
    }
83
84
    /**
85
     * @param array $array
86
     * @param bool|false $key
87
     * @return bool
88
     */
89
    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...
90
    {
91
        return isset($array[$key]) ? $array[$key] : false;
92
    }
93
}
94