Skip to content

Commit b5ad3bf

Browse files
committed
Initial commit
0 parents  commit b5ad3bf

26 files changed

+1609
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
index.php
3+
vendor

composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "krlove/code-generator",
3+
"description": "Code Generator",
4+
"license": "MIT",
5+
"autoload": {
6+
"psr-4": {
7+
"Krlove\\Generator": "src/"
8+
}
9+
}
10+
}

src/Collection/Collection.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Collection;
4+
5+
use Krlove\Generator\Exception\GeneratorException;
6+
7+
/**
8+
* Class Collection
9+
* @package Krlove\Generator
10+
*/
11+
class Collection implements CollectionInterface
12+
{
13+
/**
14+
* @var array
15+
*/
16+
protected $elements = [];
17+
18+
/**
19+
* {@inehritDoc}
20+
*/
21+
public function elements()
22+
{
23+
return $this->elements;
24+
}
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function isEmpty()
30+
{
31+
return count($this->elements) === 0;
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function offsetExists($offset)
38+
{
39+
return array_key_exists($offset, $this->elements);
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public function offsetGet($offset)
46+
{
47+
if ($this->offsetExists($offset)) {
48+
return $this->elements[$offset];
49+
}
50+
throw new GeneratorException('Invalid offset');
51+
}
52+
53+
/**
54+
* {@inheritDoc}
55+
*/
56+
public function offsetSet($offset, $value)
57+
{
58+
if ($offset === null) {
59+
$this->elements[] = $value;
60+
} else {
61+
$this->elements[$offset] = $value;
62+
}
63+
}
64+
65+
/**
66+
* {@inheritDoc}
67+
*/
68+
public function offsetUnset($offset)
69+
{
70+
unset($this->elements[$offset]);
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
public function current()
77+
{
78+
return current($this->elements);
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*/
84+
public function next()
85+
{
86+
return next($this->elements);
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
public function key()
93+
{
94+
return key($this->elements);
95+
}
96+
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public function valid()
101+
{
102+
return key($this->elements) !== null;
103+
}
104+
105+
/**
106+
* {@inheritDoc}
107+
*/
108+
public function rewind()
109+
{
110+
return reset($this->elements);
111+
}
112+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Collection;
4+
5+
/**
6+
* Interface CollectionInterface
7+
* @package Krlove\Generator\Collection
8+
*/
9+
interface CollectionInterface extends \ArrayAccess, \Iterator
10+
{
11+
/**
12+
* @return array
13+
*/
14+
public function elements();
15+
16+
/**
17+
* @return bool
18+
*/
19+
public function isEmpty();
20+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Collection;
4+
5+
/**
6+
* Class DecoratedCollection
7+
* @package Krlove\Generator\Collection
8+
*/
9+
class DecoratorCollection implements CollectionInterface
10+
{
11+
/**
12+
* @var LineCollection
13+
*/
14+
protected $collection;
15+
16+
/**
17+
* IndentLineCollection constructor.
18+
* @param CollectionInterface $collection
19+
*/
20+
public function __construct(CollectionInterface $collection)
21+
{
22+
$this->collection = $collection;
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function elements()
29+
{
30+
return $this->collection->elements();
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function isEmpty()
37+
{
38+
return $this->collection->isEmpty();
39+
}
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function offsetExists($offset)
45+
{
46+
return $this->collection->offsetExists($offset);
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function offsetGet($offset)
53+
{
54+
return $this->collection->offsetGet($offset);
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public function offsetSet($offset, $value)
61+
{
62+
$this->collection->offsetSet($offset, $value);
63+
}
64+
65+
/**
66+
* {@inheritDoc}
67+
*/
68+
public function offsetUnset($offset)
69+
{
70+
$this->collection->offsetUnset($offset);
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
public function current()
77+
{
78+
return $this->collection->current();
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*/
84+
public function next()
85+
{
86+
return $this->collection->next();
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
public function key()
93+
{
94+
return $this->collection->key();
95+
}
96+
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public function valid()
101+
{
102+
return $this->collection->valid();
103+
}
104+
105+
/**
106+
* {@inheritDoc}
107+
*/
108+
public function rewind()
109+
{
110+
return $this->collection->rewind();
111+
}}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Collection;
4+
5+
use Krlove\Generator\Line\IndentLine;
6+
use Krlove\Generator\Line\LineInterface;
7+
8+
/**
9+
* Class IndentLineCollection
10+
* @package Krlove\Generator\Collection
11+
*/
12+
class IndentLineCollection extends DecoratorCollection implements LineInterface
13+
{
14+
/**
15+
* @var int
16+
*/
17+
protected $indent;
18+
19+
/**
20+
* @var LineCollection
21+
*/
22+
protected $collection;
23+
24+
/**
25+
* IndentLineCollection constructor.
26+
* @param LineCollection $collection
27+
* @param int $indent
28+
*/
29+
public function __construct(LineCollection $collection, $indent)
30+
{
31+
parent::__construct($collection);
32+
$this->indent = $indent;
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function __toString()
39+
{
40+
foreach ($this->collection as $key => $element) {
41+
if ($element instanceof LineCollection) {
42+
$element = new self($element, $this->indent);
43+
} else {
44+
$element = new IndentLine($element, $this->indent);
45+
}
46+
$this->collection[$key] = $element;
47+
}
48+
49+
return $this->collection->__toString();
50+
}
51+
}

src/Collection/LineCollection.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Collection;
4+
5+
use Krlove\Generator\Exception\GeneratorException;
6+
use Krlove\Generator\Line\Line;
7+
use Krlove\Generator\Line\LineInterface;
8+
9+
/**
10+
* Class LineCollection
11+
* @package Krlove\Generator
12+
*/
13+
class LineCollection extends Collection implements LineInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
protected $delimiter;
19+
20+
/**
21+
* LineCollection constructor.
22+
* @param string $delimiter
23+
*/
24+
public function __construct($delimiter = PHP_EOL)
25+
{
26+
$this->delimiter = $delimiter;
27+
}
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function __toString()
33+
{
34+
$output = [];
35+
/** @var LineInterface $element */
36+
foreach ($this->elements as $element) {
37+
$output[] = $element->__toString();
38+
}
39+
40+
return implode($this->delimiter, $output);
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function offsetSet($offset, $value)
47+
{
48+
if (!$value instanceof LineInterface) {
49+
if (is_scalar($value)) {
50+
$value = new Line($value);
51+
} else {
52+
throw new GeneratorException('Invalid value');
53+
}
54+
}
55+
56+
parent::offsetSet($offset, $value);
57+
}
58+
}

0 commit comments

Comments
 (0)