Passed
Push — master ( aae7cc...acea2e )
by Anton
29s
created

Inflector::member2var()   A

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
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi;
7
8
use yii\helpers\BaseInflector;
9
10
class Inflector extends BaseInflector
11
{
12
    /**
13
     * Format member names according to recommendations for JSON API implementations.
14
     * For example, both 'firstName' and 'first_name' will be converted to 'first-name'.
15
     * @link http://jsonapi.org/format/#document-member-names
16
     * @param $var string
17
     * @return string
18
     */
19
    public static function var2member($var)
20
    {
21
        return self::camel2id(self::variablize($var));
22
    }
23
24
    /**
25
     * Converts member names to variable names
26
     * All special characters will be replaced by underscore
27
     * For example, 'first-name' will be converted to 'first_name'
28
     * @param $member string
29
     * @return mixed
30
     */
31
    public static function member2var($member)
32
    {
33
        return str_replace(' ', '_', preg_replace('/[^A-Za-z0-9]+/', ' ', $member));
34
    }
35
36
    /**
37
     * Converts 'type' member to form name
38
     * Will be converted to singular form.
39
     * For example, 'articles' will be converted to 'Article'
40
     * @param $type string 'type' member of the document
41
     * @return string
42
     */
43
    public static function type2form($type)
44
    {
45
        return self::id2camel(self::singularize($type));
46
    }
47
}