Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Parser
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
17
100.00% covered (success)
100.00%
1 / 1
 parseClassNameWithParameters
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 parseParameters
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
12
1<?php
2
3namespace Verja;
4
5class Parser
6{
7    /**
8     * Returns a className and an array of parameters from $str
9     *
10     * The parameters start with and are divided by a colon.
11     *
12     * There are several limitations for parameters:
13     *   1. They are all string - no type casting here
14     *   2. They can not have colons - these increases the performance
15     *
16     * To avoid these limitations you can use json array notation. The json decode uses assoc = true - so we get an
17     * array for objects. Please make sure to start and end with brackets. Here is an example how to pass an array:
18     * `'equals:[ { "key": "value" } ]'`
19     *
20     * @param string $str
21     * @return array
22     */
23    public static function parseClassNameWithParameters(string $str)
24    {
25        $colonPos = strpos($str, ':');
26        $className = ucfirst($colonPos === false ? trim($str) : trim(substr($str, 0, $colonPos)));
27
28        if (empty($className)) {
29            throw new \InvalidArgumentException(sprintf(
30                '%s is not a valid string for ' . __METHOD__,
31                empty($str) ? '$str' : $str
32            ));
33        }
34
35        if ($colonPos === false) {
36            return [$className, []];
37        }
38
39        return [$className, self::parseParameters(substr($str, $colonPos+1))];
40    }
41
42    /**
43     * Return an array of parameters from $str
44     *
45     * @see Parser::parseClassNameWithParameters() for description and limitations of parameters
46     * @param string $str
47     * @return array
48     */
49    public static function parseParameters($str)
50    {
51        if (strlen($str) > 1 && $str[0] === '[' && substr($str, -1) === ']') {
52            return (array) json_decode($str, true);
53        }
54
55        $parameters = array_filter(explode(':', $str), function ($value) {
56            return strlen($value) > 0;
57        });
58
59        return array_map(function ($parameter) {
60            if (strlen($parameter) > 1 && (
61                    $parameter[0] === '"' && substr($parameter, -1) === '"' ||
62                    $parameter[0] === "'" && substr($parameter, -1) === "'"
63                )
64            ) {
65                return substr($parameter, 1, -1);
66            }
67
68            if ($parameter === 'false') {
69                return false;
70            }
71
72            if ($parameter === 'true') {
73                return true;
74            }
75
76            if ($parameter === 'null') {
77                return null;
78            }
79
80            return $parameter;
81        }, $parameters);
82    }
83}