Completed
Push — master ( c7b947...966759 )
by Thomas
08:09
created

EmberClassGenerator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
namespace keeko\tools\generator\ember;
3
4
use phootwork\collection\ArrayList;
5
use phootwork\collection\Map;
6
use gossi\codegen\utils\Writer;
7
8
class EmberClassGenerator {
9
	
10
	private $name;
0 ignored issues
show
Unused Code introduced by
The property $name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
	private $parent;
12
	private $imports;
13
	private $properties;
14
	
15
	public function __construct($parent = null) {
16
		$this->imports = new ArrayList();
17
		$this->properties = new Map();
18
		
19
		if ($parent === null) {
20
			$parent = 'Ember.Object';
21
			$this->addImport('Ember', 'ember');
22
		}
23
		$this->parent = $parent;
24
	}
25
	
26
	public function addImport($import, $from) {
27
		$this->imports->add(['import' => $import, 'from' => $from]);
28
	}
29
	
30
	public function setProperty($property, $value) {
31
		$this->properties->set($property, $value);
32
	}
33
	
34
	public function generate() {
35
		$writer = new Writer();
36
		
37
		// add imports first
38
		foreach ($this->imports as $import) {
39
			$writer->writeln(sprintf('import %s from \'%s\';', $import['import'], $import['from']));
40
		}
41
		
42
		if ($this->imports->size() > 0) {
43
			$writer->writeln('');
44
		}
45
		
46
		// class signature
47
		$writer->writeln(sprintf('export default %s.extend({', $this->parent));
48
		$writer->indent();
49
		
50
		// properties
51
		$propper = new Writer();
52
		foreach ($this->properties as $prop => $value) {
53
			$propper->writeln(sprintf('%s: %s,', $prop, $value));
54
		}
55
		$writer->writeln(rtrim($propper->getContent(), ", \n"));
56
57
		// class foot
58
		$writer->outdent();
59
		$writer->writeln('});');
60
		
61
		return $writer->getContent();
62
	}
63
}