Completed
Push — master ( 713d54...d55531 )
by Amine
02:08
created

object.php ➔ keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Tarsana\Functional;
2
/**
3
 * This file contains some useful functions to handle objects (associative arrays are considered objects).
4
 */
5
6
/**
7
 * Converts an object to an associative array containing accessible 
8
 * non-static attributes. A curried version of `get_object_vars`.
9
 * If `$object` is not an object, it returns it.
10
 * ```php
11
 * class foo {
12
 *     private $a;
13
 *     public $b = 1;
14
 *     public $c;
15
 *     private $d;
16
 *     static $e;
17
 *
18
 *     public function getAttrs() {
19
 *         return attributes($this));
20
 *     }
21
 * }
22
 *
23
 * $test = new foo;
24
 * attributes($test); // ['b' => 1, 'c' => null]
25
 *
26
 * $test->getAttrs(); // ['a' => null, 'b' => 1, 'c' => null, 'd' => null]
27
 * ```
28
 *
29
 * @signature {k: v} -> {k: v}
30
 * @param  object $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
31
 * @return array
32
 */
33
function attributes() {
34
    $attrs = function($object) {
35
        if (is_object($object))
36
            return get_object_vars($object);
37
        return $object;
38
    };
39
    return apply(curry($attrs), func_get_args());
40
}
41
42
/**
43
 * Returns a list of array's keys or object's accessible attributes names.
44
 * ```php
45
 * keys([1, 2, 3]); // [0, 1, 2]
46
 * keys(['name' => 'foo', 'age' => 11]); // ['name', 'age']
47
 * keys((object)['name' => 'foo', 'age' => 11]); // ['name', 'age']
48
 * ```
49
 *
50
 * @signature [*] -> [Number]
51
 * @signature {k: v} -> [k]
52
 */
53
function keys() {
54
    $keys = function($object) {
55
        return array_keys(attributes($object));
56
    };
57
    return apply(curry($keys), func_get_args());
58
}
59
60
/**
61
 * Returns a list of array's values or object's accessible attributes values.
62
 * ```php
63
 * keys([1, 2, 3]); // [1, 2, 3]
64
 * keys(['name' => 'foo', 'age' => 11]); // ['foo', 11]
65
 * keys((object)['name' => 'foo', 'age' => 11]); // ['foo', 11]
66
 * ```
67
 *
68
 * @signature [a] -> [a]
69
 * @signature {k: v} -> [v]
70
 */
71
function values() {
72
    $values = function($object) {
73
        return array_values(attributes($object));
74
    };
75
    return apply(curry($values), func_get_args());
76
}
77
78
/**
79
 * Checks if the given array or object has a specific key or accessible attribute.
80
 * ```php
81
 * $array = [
82
 *     'type' => 'Array',
83
 *     'length' => 78
84
 * ];
85
 * $array[3] = 'three';
86
 * $object = (object) ['name' => 'ok'];
87
 *
88
 * $hasName = F\has('name');
89
 *
90
 * F\has('type', $array); // true
91
 * F\has(3, $array); // true
92
 * $hasName($array); // false
93
 * $hasName($object); // true
94
 * F\has('length', $object); // false
95
 * ```
96
 *
97
 * @signature String|Number -> [key => *] -> Boolean
98
 * @param  string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
99
 * @param  array $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
100
 * @return mixed
101
 */
102
function has() {
103
    $has = function($name, $object){
104
        return contains($name, keys($object));
105
    };
106
    return apply(curry($has), func_get_args());
107
}
108
109
/**
110
 * Gets the value of a key from an array or the
111
 * value of an accessible attribute from an object.
112
 * If the key/attribute is missing, `null` is returned.
113
 * ```php
114
 * $data = [
115
 *     ['name' => 'foo', 'type' => 'test'],
116
 *     ['name' => 'bar', 'type' => 'test'],
117
 *     (object) ['name' => 'baz']
118
 *     [1, 2, 3]
119
 * ];
120
 * $nameOf = get('name');
121
 * get(0, $data) // ['name' => 'foo', 'type' => 'test']
122
 * $nameOf($data[1]) // 'bar'
123
 * $nameOf($data[2]) // 'baz'
124
 * $nameOf($data[4]) // null
125
 * ```
126
 *
127
 * @signature k -> {k: v} -> Maybe(v)
128
 * @param  string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
129
 * @param  array $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
130
 * @return mixed
131
 */
132
function get() {
133
    $get = function($name, $object){
134
        $object = attributes($object);
135
        return has($name, $object)
136
            ? $object[$name]
137
            : null;
138
    };
139
    return apply(curry($get), func_get_args());
140
}
141
142
/**
143
 * Converts an object or associative array to an array of [key, value] pairs.
144
 * ```php
145
 * $list = ['key' => 'value', 'number' => 53, 'foo', 'bar'];
146
 * toPairs($list); // [['key', 'value'], ['number', 53], [0, 'foo'], [1, 'bar']]
147
 * ```
148
 *
149
 * @signature {k: v} -> [(k,v)]
150
 * @param  array $object
151
 * @return array
152
 */
153
function toPairs($object) {
154
    $object = attributes($object);
155
    return map(function($key) use($object) {
156
        return [$key, $object[$key]];
157
    }, keys($object));
158
}
159