Completed
Push — master ( 9ba5b5...de2684 )
by Amine
02:17
created
src/Argument/ArgumentFormat.php 1 patch
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -3,41 +3,41 @@
 block discarded – undo
3 3
 
4 4
 class ArgumentFormat {
5 5
 
6
-	/**
7
-	 * Argument name
8
-	 * 
9
-	 * @var string
10
-	 */
11
-	public $name;
6
+    /**
7
+     * Argument name
8
+     * 
9
+     * @var string
10
+     */
11
+    public $name;
12 12
 
13
-	/**
14
-	 * One of the types: array, object, string, boolean, number
15
-	 * 
16
-	 * @var string
17
-	 */
18
-	public $type;
13
+    /**
14
+     * One of the types: array, object, string, boolean, number
15
+     * 
16
+     * @var string
17
+     */
18
+    public $type;
19 19
 	
20
-	/**
21
-	 * The default value
22
-	 * 
23
-	 * @var string
24
-	 */
25
-	public $default;
20
+    /**
21
+     * The default value
22
+     * 
23
+     * @var string
24
+     */
25
+    public $default;
26 26
 
27
-	/**
28
-	 * The separator, by default ":" for object and "," for array.
29
-	 * 
30
-	 * @var string
31
-	 */
32
-	public $separator;
27
+    /**
28
+     * The separator, by default ":" for object and "," for array.
29
+     * 
30
+     * @var string
31
+     */
32
+    public $separator;
33 33
 
34
-	/**
35
-	 * Specify the format of fields for object
36
-	 *     [ field_name => Format, field_name => Format, ... ] 
37
-	 * Specify the format of an element of array
38
-	 * 
39
-	 * @var mixed
40
-	 */
41
-	public $format;
34
+    /**
35
+     * Specify the format of fields for object
36
+     *     [ field_name => Format, field_name => Format, ... ] 
37
+     * Specify the format of an element of array
38
+     * 
39
+     * @var mixed
40
+     */
41
+    public $format;
42 42
 	
43 43
 }
Please login to merge, or discard this patch.
src/Argument/ArgumentFormatLoader.php 1 patch
Indentation   +131 added lines, -131 removed lines patch added patch discarded remove patch
@@ -7,140 +7,140 @@
 block discarded – undo
7 7
 
8 8
 class ArgumentFormatLoader {
9 9
 
10
-	protected $fs;
11
-
12
-	protected $loaded;
13
-
14
-	public function __construct(Filesystem $fs)
15
-	{
16
-		$this->fs = $fs;
17
-		$this->loaded = [];
18
-	}
19
-
20
-	public function load($name)
21
-	{
22
-    	if(! isset($this->loaded[$name])){
23
-    		$path = __DIR__ . "/../../formats/{$name}.json";
24
-    		$json = "";
25
-			try {
26
-				$json = json_decode($this->fs->get($path));
27
-			} catch(\Exception $e) {
28
-				throw new ArgumentFormatException("Unable to read the file '{$path}'");
29
-			}
30
-			if (json_last_error() !== JSON_ERROR_NONE){
31
-				throw new ArgumentFormatException("Error while parsing the JSON file '{$path}'");
32
-			}
33
-			$this->loaded[$name] = $this->buildFormat($json);
34
-		}
35
-		return $this->loaded[$name];
36
-	}
37
-
38
-	protected function buildFormat($obj)
39
-	{
40
-		return $this->fillFirstLevel($obj);
41
-	}
42
-
43
-	protected function fillFirstLevel($obj)
44
-	{
45
-	    return $this->fill($obj, true);
46
-	}
47
-
48
-	protected function fill($obj, $firstLevel = false)
49
-	{
50
-	    if (is_string($obj)) {
51
-	        return $this->fillString($obj, $firstLevel);
52
-	    } else {
53
-	        return $this->fillObject($obj, $firstLevel);
54
-	    }
55
-	}
56
-
57
-	protected function fillString($string, $firstLevel = false)
58
-	{
59
-	    list($name, $type, $isArray) = $this->parseName($string);
10
+    protected $fs;
11
+
12
+    protected $loaded;
13
+
14
+    public function __construct(Filesystem $fs)
15
+    {
16
+        $this->fs = $fs;
17
+        $this->loaded = [];
18
+    }
19
+
20
+    public function load($name)
21
+    {
22
+        if(! isset($this->loaded[$name])){
23
+            $path = __DIR__ . "/../../formats/{$name}.json";
24
+            $json = "";
25
+            try {
26
+                $json = json_decode($this->fs->get($path));
27
+            } catch(\Exception $e) {
28
+                throw new ArgumentFormatException("Unable to read the file '{$path}'");
29
+            }
30
+            if (json_last_error() !== JSON_ERROR_NONE){
31
+                throw new ArgumentFormatException("Error while parsing the JSON file '{$path}'");
32
+            }
33
+            $this->loaded[$name] = $this->buildFormat($json);
34
+        }
35
+        return $this->loaded[$name];
36
+    }
37
+
38
+    protected function buildFormat($obj)
39
+    {
40
+        return $this->fillFirstLevel($obj);
41
+    }
42
+
43
+    protected function fillFirstLevel($obj)
44
+    {
45
+        return $this->fill($obj, true);
46
+    }
47
+
48
+    protected function fill($obj, $firstLevel = false)
49
+    {
50
+        if (is_string($obj)) {
51
+            return $this->fillString($obj, $firstLevel);
52
+        } else {
53
+            return $this->fillObject($obj, $firstLevel);
54
+        }
55
+    }
56
+
57
+    protected function fillString($string, $firstLevel = false)
58
+    {
59
+        list($name, $type, $isArray) = $this->parseName($string);
60 60
 	    
61
-	    $format = new ArgumentFormat;
62
-	    $format->name = $name;
61
+        $format = new ArgumentFormat;
62
+        $format->name = $name;
63 63
 	    
64
-	    if ($isArray) {
65
-	        $format->type    = 'array';
66
-	        $subFormat       = new ArgumentFormat;
67
-	        $subFormat->type = $type ?: 'string';            
68
-	        $format->format  = $subFormat;
69
-	    } else {
70
-	        $format->type = 'string';
71
-	    }
72
-
73
-	    if ($firstLevel) {
74
-	        if ($format->type === 'object') {
75
-	            $format->separator = ':';
76
-	        } elseif ($format->type === 'array') {
77
-	            $format->separator = ',';
78
-	        }
79
-	    }
80
-
81
-	    return $format;
82
-	}
83
-
84
-	protected function fillObject($obj, $firstLevel=false)
85
-	{
86
-	    $format = new ArgumentFormat;
87
-
88
-	    // Resolve type from name
89
-	    if (isset($obj->name)) {
90
-	        list($name, $type) = $this->parseName($obj->name);
91
-	        $format->name = $name;
92
-	        $format->type = $type;
93
-	    }
94
-
95
-	    // Fill type if set
96
-	    if (isset($obj->type)) {
97
-	        $format->type = $obj->type;
98
-	    }
99
-
100
-	    // Fill default if set
101
-	    if (isset($obj->default)) {
102
-	        $format->default = $obj->default;
103
-	    }
64
+        if ($isArray) {
65
+            $format->type    = 'array';
66
+            $subFormat       = new ArgumentFormat;
67
+            $subFormat->type = $type ?: 'string';            
68
+            $format->format  = $subFormat;
69
+        } else {
70
+            $format->type = 'string';
71
+        }
72
+
73
+        if ($firstLevel) {
74
+            if ($format->type === 'object') {
75
+                $format->separator = ':';
76
+            } elseif ($format->type === 'array') {
77
+                $format->separator = ',';
78
+            }
79
+        }
80
+
81
+        return $format;
82
+    }
83
+
84
+    protected function fillObject($obj, $firstLevel=false)
85
+    {
86
+        $format = new ArgumentFormat;
87
+
88
+        // Resolve type from name
89
+        if (isset($obj->name)) {
90
+            list($name, $type) = $this->parseName($obj->name);
91
+            $format->name = $name;
92
+            $format->type = $type;
93
+        }
94
+
95
+        // Fill type if set
96
+        if (isset($obj->type)) {
97
+            $format->type = $obj->type;
98
+        }
99
+
100
+        // Fill default if set
101
+        if (isset($obj->default)) {
102
+            $format->default = $obj->default;
103
+        }
104 104
 	    
105
-	    // Set separator, default to ':' for objects and ',' for arrays in first level
106
-	    if (in_array($format->type, ['object', 'array'])) {
107
-	        if (isset($obj->separator)) {
108
-	            $format->separator = $obj->separator;
109
-	        } elseif ($firstLevel) {
110
-	           $format->separator = ($format->type === 'object') ? ':':',';
111
-	        }
112
-	    }
113
-
114
-	    // Build format recursively
115
-	    if (isset($obj->fields)) {
116
-	        if ($firstLevel) {
117
-	            if (is_array($obj->fields)) {
118
-	                $format->format = array_map([$this, 'fillFirstLevel'], $obj->fields);
119
-	            } else {
120
-	                $format->format = $this->fill($obj->fields, true);
121
-	            }
122
-	        } else {
123
-	            if (is_array($obj->fields)) {
124
-	                $format->format = array_map([$this, 'fill'], $obj->fields);
125
-	            } else {
126
-	                $format->format = $this->fill($obj->fields);
127
-	            }
128
-	        }
129
-	    }
130
-
131
-	    return $format;
132
-	}
133
-
134
-	protected function parseName($name)
135
-	{
136
-	    $pattern = '/^(?P<attr>\w+)(\[(?P<type>\w+)?\])?/';
137
-	    preg_match($pattern, $name, $matches);
138
-
139
-	    $attr = $matches['attr'];
140
-	    $type = empty($matches['type']) ? null : $matches['type'];
141
-	    $isArray = (isset($matches[2][0]) && $matches[2][0] === '[');
105
+        // Set separator, default to ':' for objects and ',' for arrays in first level
106
+        if (in_array($format->type, ['object', 'array'])) {
107
+            if (isset($obj->separator)) {
108
+                $format->separator = $obj->separator;
109
+            } elseif ($firstLevel) {
110
+                $format->separator = ($format->type === 'object') ? ':':',';
111
+            }
112
+        }
113
+
114
+        // Build format recursively
115
+        if (isset($obj->fields)) {
116
+            if ($firstLevel) {
117
+                if (is_array($obj->fields)) {
118
+                    $format->format = array_map([$this, 'fillFirstLevel'], $obj->fields);
119
+                } else {
120
+                    $format->format = $this->fill($obj->fields, true);
121
+                }
122
+            } else {
123
+                if (is_array($obj->fields)) {
124
+                    $format->format = array_map([$this, 'fill'], $obj->fields);
125
+                } else {
126
+                    $format->format = $this->fill($obj->fields);
127
+                }
128
+            }
129
+        }
130
+
131
+        return $format;
132
+    }
133
+
134
+    protected function parseName($name)
135
+    {
136
+        $pattern = '/^(?P<attr>\w+)(\[(?P<type>\w+)?\])?/';
137
+        preg_match($pattern, $name, $matches);
138
+
139
+        $attr = $matches['attr'];
140
+        $type = empty($matches['type']) ? null : $matches['type'];
141
+        $isArray = (isset($matches[2][0]) && $matches[2][0] === '[');
142 142
 	    
143
-	    return [$attr, $type, $isArray];
144
-	}
143
+        return [$attr, $type, $isArray];
144
+    }
145 145
 
146 146
 }
Please login to merge, or discard this patch.
src/Argument/ArgumentParser.php 1 patch
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -6,110 +6,110 @@
 block discarded – undo
6 6
 
7 7
 class ArgumentParser {
8 8
 
9
-	protected $format;
10
-
11
-	public function __construct(ArgumentFormat $format)
12
-	{
13
-	    $this->format = $format;
14
-	}
15
-
16
-	public function parse($args)
17
-	{
18
-		return $this->parseToken($args, $this->format);
19
-	}
20
-
21
-	protected function parseToken($token, ArgumentFormat $format){
22
-	    switch($format->type) {
23
-	        case 'string':
24
-	            return $token;
25
-	        case 'number':
26
-	            return $this->parseNumber($token);
27
-	        case 'boolean':
28
-	            return $this->parseBoolean($token, $format->name);
29
-	        case 'array':
30
-	            return $this->parseArray($token, $format->separator, $format->format);
31
-	        case 'object':
32
-	            return $this->parseObject($token, $format->separator, $format->format);
33
-	        default:
34
-	            throw new ArgumentParserException("Unknown format type: '{$format->type}'");
35
-	    }
36
-	}
37
-
38
-	protected function parseNumber($token)
39
-	{
40
-	    if(! is_numeric($token)) {
41
-	        throw new ArgumentParserException("Unable to parse '{$token}' as number");
42
-	    }
43
-	    return $token + 0;
44
-	}
45
-
46
-	protected function parseBoolean($token, $name)
47
-	{
48
-	    if(in_array($token, ['yes', 'true', '1', $name])) {
49
-	        return true;
50
-	    } else if(in_array($token, ['no', 'false', '0', "!{$name}"])){
51
-	        return false;
52
-	    } else {
53
-	        return null;
54
-	    }
55
-	}
56
-
57
-	protected function parseArray($token, $separator, ArgumentFormat $format)
58
-	{
59
-	    $result = [];
60
-	    $tokens = explode($separator, $token);
61
-	    foreach($tokens as $value) {
62
-	        array_push($result, $this->parseToken($value, $format));
63
-	    }
64
-	    return $result;
65
-	}
66
-
67
-	protected function parseObject($token, $separator, $fields)
68
-	{
69
-	    $result = [];
70
-	    $tokens = explode($separator, $token);
71
-	    $tokensNumber = count($tokens);
72
-
73
-	    $requiredFieldsIndexes = [];
74
-	    $optionalFieldsIndexes = [];
75
-	    foreach($fields as $index => $format) {
76
-	        if($format->default === null) {
77
-	            array_push($requiredFieldsIndexes, $index);
78
-	        } else {
79
-	            array_push($optionalFieldsIndexes, $index);
80
-	        }
81
-	    }
82
-	    $requiredFieldsIndexesNumber = count($requiredFieldsIndexes);
83
-
84
-	    if($tokensNumber < $requiredFieldsIndexesNumber) {
85
-	        $requiredFields = array_map(function($index) use ($fields) {
86
-	                return $fields[$index]->name;
87
-	            }, $requiredFieldsIndexes);
88
-	        $requiredFields = implode($separator, $requiredFields);
89
-	        throw new ArgumentParserException("Required field missing: {$tokensNumber} given "
90
-	            . "({$token}) but {$requiredFieldsIndexesNumber} required ({$requiredFields})");
91
-	    }
92
-
93
-	    $givenOptionalFieldsIndexes = array_slice(
94
-	        $optionalFieldsIndexes, 0, $tokensNumber - $requiredFieldsIndexesNumber);
95
-	    $notPresentFieldsIndexes = array_slice(
96
-	        $optionalFieldsIndexes, $tokensNumber - $requiredFieldsIndexesNumber);
97
-	    $givenFieldsIndexes = array_merge($requiredFieldsIndexes, $givenOptionalFieldsIndexes);
98
-	    sort($givenFieldsIndexes);
99
-
100
-	    // Fill the given fields
101
-	    for($i = 0; $i < $tokensNumber; $i ++) {
102
-	        $fieldFormat = $fields[$givenFieldsIndexes[$i]];
103
-	        $result[$fieldFormat->name] = $this->parseToken($tokens[$i], $fieldFormat);
104
-	    }
105
-
106
-	    // Fill other fields with default values
107
-	    foreach($notPresentFieldsIndexes as $index) {
108
-	        $fieldFormat = $fields[$index];
109
-	        $result[$fieldFormat->name] = $fieldFormat->default;
110
-	    }
111
-
112
-	    return $result;
113
-	}
9
+    protected $format;
10
+
11
+    public function __construct(ArgumentFormat $format)
12
+    {
13
+        $this->format = $format;
14
+    }
15
+
16
+    public function parse($args)
17
+    {
18
+        return $this->parseToken($args, $this->format);
19
+    }
20
+
21
+    protected function parseToken($token, ArgumentFormat $format){
22
+        switch($format->type) {
23
+            case 'string':
24
+                return $token;
25
+            case 'number':
26
+                return $this->parseNumber($token);
27
+            case 'boolean':
28
+                return $this->parseBoolean($token, $format->name);
29
+            case 'array':
30
+                return $this->parseArray($token, $format->separator, $format->format);
31
+            case 'object':
32
+                return $this->parseObject($token, $format->separator, $format->format);
33
+            default:
34
+                throw new ArgumentParserException("Unknown format type: '{$format->type}'");
35
+        }
36
+    }
37
+
38
+    protected function parseNumber($token)
39
+    {
40
+        if(! is_numeric($token)) {
41
+            throw new ArgumentParserException("Unable to parse '{$token}' as number");
42
+        }
43
+        return $token + 0;
44
+    }
45
+
46
+    protected function parseBoolean($token, $name)
47
+    {
48
+        if(in_array($token, ['yes', 'true', '1', $name])) {
49
+            return true;
50
+        } else if(in_array($token, ['no', 'false', '0', "!{$name}"])){
51
+            return false;
52
+        } else {
53
+            return null;
54
+        }
55
+    }
56
+
57
+    protected function parseArray($token, $separator, ArgumentFormat $format)
58
+    {
59
+        $result = [];
60
+        $tokens = explode($separator, $token);
61
+        foreach($tokens as $value) {
62
+            array_push($result, $this->parseToken($value, $format));
63
+        }
64
+        return $result;
65
+    }
66
+
67
+    protected function parseObject($token, $separator, $fields)
68
+    {
69
+        $result = [];
70
+        $tokens = explode($separator, $token);
71
+        $tokensNumber = count($tokens);
72
+
73
+        $requiredFieldsIndexes = [];
74
+        $optionalFieldsIndexes = [];
75
+        foreach($fields as $index => $format) {
76
+            if($format->default === null) {
77
+                array_push($requiredFieldsIndexes, $index);
78
+            } else {
79
+                array_push($optionalFieldsIndexes, $index);
80
+            }
81
+        }
82
+        $requiredFieldsIndexesNumber = count($requiredFieldsIndexes);
83
+
84
+        if($tokensNumber < $requiredFieldsIndexesNumber) {
85
+            $requiredFields = array_map(function($index) use ($fields) {
86
+                    return $fields[$index]->name;
87
+                }, $requiredFieldsIndexes);
88
+            $requiredFields = implode($separator, $requiredFields);
89
+            throw new ArgumentParserException("Required field missing: {$tokensNumber} given "
90
+                . "({$token}) but {$requiredFieldsIndexesNumber} required ({$requiredFields})");
91
+        }
92
+
93
+        $givenOptionalFieldsIndexes = array_slice(
94
+            $optionalFieldsIndexes, 0, $tokensNumber - $requiredFieldsIndexesNumber);
95
+        $notPresentFieldsIndexes = array_slice(
96
+            $optionalFieldsIndexes, $tokensNumber - $requiredFieldsIndexesNumber);
97
+        $givenFieldsIndexes = array_merge($requiredFieldsIndexes, $givenOptionalFieldsIndexes);
98
+        sort($givenFieldsIndexes);
99
+
100
+        // Fill the given fields
101
+        for($i = 0; $i < $tokensNumber; $i ++) {
102
+            $fieldFormat = $fields[$givenFieldsIndexes[$i]];
103
+            $result[$fieldFormat->name] = $this->parseToken($tokens[$i], $fieldFormat);
104
+        }
105
+
106
+        // Fill other fields with default values
107
+        foreach($notPresentFieldsIndexes as $index) {
108
+            $fieldFormat = $fields[$index];
109
+            $result[$fieldFormat->name] = $fieldFormat->default;
110
+        }
111
+
112
+        return $result;
113
+    }
114 114
 
115 115
 }
Please login to merge, or discard this patch.
src/Template/Template.php 1 patch
Indentation   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -5,58 +5,58 @@
 block discarded – undo
5 5
 
6 6
 class Template {
7 7
 
8
-	protected $loader;
9
-
10
-	protected $text;
11
-
12
-	protected $data;
13
-
14
-	protected $compiled;
15
-
16
-	protected $dirty;
17
-
18
-	public function __construct(TemplateLoader $loader, $text)
19
-	{
20
-		$this->loader = $loader;
21
-		$this->text = $text;
22
-		$this->compiled = '';
23
-		$this->data = [];
24
-		$this->dirty = true;
25
-	}
26
-
27
-	public function clean()
28
-	{
29
-		$this->data = [];
30
-		$this->dirty = false;
31
-		return $this;
32
-	}
33
-
34
-	public function with($data = [])
35
-	{
36
-		if($data)
37
-			$this->dirty = true;
38
-		foreach ($data as $key => $value) {
39
-			$this->data[$key] = $value;
40
-		}
41
-		return $this;
42
-	}
43
-
44
-	public function get()
45
-	{
46
-		if($this->dirty){
47
-			$this->compile();
48
-			$this->dirty = false;
49
-		}
50
-		return $this->compiled;
51
-	}
52
-
53
-	public function compile()
54
-	{
55
-		$this->compiled = $this->text;
56
-		foreach($this->data as $key => $value){
57
-			$this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled);
58
-		}
59
-		return $this;
60
-	}	
8
+    protected $loader;
9
+
10
+    protected $text;
11
+
12
+    protected $data;
13
+
14
+    protected $compiled;
15
+
16
+    protected $dirty;
17
+
18
+    public function __construct(TemplateLoader $loader, $text)
19
+    {
20
+        $this->loader = $loader;
21
+        $this->text = $text;
22
+        $this->compiled = '';
23
+        $this->data = [];
24
+        $this->dirty = true;
25
+    }
26
+
27
+    public function clean()
28
+    {
29
+        $this->data = [];
30
+        $this->dirty = false;
31
+        return $this;
32
+    }
33
+
34
+    public function with($data = [])
35
+    {
36
+        if($data)
37
+            $this->dirty = true;
38
+        foreach ($data as $key => $value) {
39
+            $this->data[$key] = $value;
40
+        }
41
+        return $this;
42
+    }
43
+
44
+    public function get()
45
+    {
46
+        if($this->dirty){
47
+            $this->compile();
48
+            $this->dirty = false;
49
+        }
50
+        return $this->compiled;
51
+    }
52
+
53
+    public function compile()
54
+    {
55
+        $this->compiled = $this->text;
56
+        foreach($this->data as $key => $value){
57
+            $this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled);
58
+        }
59
+        return $this;
60
+    }	
61 61
 
62 62
 }
Please login to merge, or discard this patch.
src/Template/TemplateLoader.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -7,27 +7,27 @@
 block discarded – undo
7 7
 
8 8
 class TemplateLoader {
9 9
 	
10
-	protected $fs;
10
+    protected $fs;
11 11
 
12
-	protected $loaded;
12
+    protected $loaded;
13 13
 
14
-	public function __construct(Filesystem $fs)
15
-	{
16
-		$this->fs = $fs;
17
-		$this->loaded = [];
18
-	}
14
+    public function __construct(Filesystem $fs)
15
+    {
16
+        $this->fs = $fs;
17
+        $this->loaded = [];
18
+    }
19 19
 
20
-	public function load($name)
21
-	{
22
-		if(! isset($this->loaded[$name])){
23
-			$path = __DIR__ . "/../../templates/{$name}.wnt";
24
-			try {
25
-				$this->loaded[$name] = $this->fs->get($path);
26
-			} catch(\Exception $e) {
27
-				throw new TemplateException("Unable to read the file '{$path}'");
28
-			}
29
-		}
30
-		return new Template($this, $this->loaded[$name]);
31
-	}
20
+    public function load($name)
21
+    {
22
+        if(! isset($this->loaded[$name])){
23
+            $path = __DIR__ . "/../../templates/{$name}.wnt";
24
+            try {
25
+                $this->loaded[$name] = $this->fs->get($path);
26
+            } catch(\Exception $e) {
27
+                throw new TemplateException("Unable to read the file '{$path}'");
28
+            }
29
+        }
30
+        return new Template($this, $this->loaded[$name]);
31
+    }
32 32
 
33 33
 }
Please login to merge, or discard this patch.