Loop::__construct()   A
last analyzed

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
/**
4
 * @package Cadmium\Framework\Template
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Template {
11
12
	class Loop {
13
14
		private $block = null, $items = null;
15
16
		/**
17
		 * Constructor
18
		 */
19
20
		public function __construct(string $contents = '') {
21
22
			$this->block = new Block($contents); $this->items = new Group;
23
		}
24
25
		/**
26
		 * Cloner
27
		 */
28
29
		public function __clone() {
30
31
			$this->items = clone $this->items;
32
		}
33
34
		/**
35
		 * Add an item
36
		 *
37
		 * @return Template\Loop : the current loop object
38
		 */
39
40
		public function addItem(array $data) : Loop {
41
42
			$this->items->addItem((clone $this->block)->setArray($data));
43
44
			return $this;
45
		}
46
47
		/**
48
		 * Add multiple items
49
		 *
50
		 * @return Template\Loop : the current loop object
51
		 */
52
53
		public function addItems(array $items) : Loop {
54
55
			foreach ($items as $item) if (is_array($item)) $this->addItem($item);
56
57
			return $this;
58
		}
59
60
		/**
61
		 * Clear the items list
62
		 *
63
		 * @return Template\Loop : the current loop object
64
		 */
65
66
		public function removeItems() : Loop {
67
68
			$this->items->removeItems();
69
70
			return $this;
71
		}
72
73
		/**
74
		 * Set items list
75
		 *
76
		 * @return Template\Loop : the current loop object
77
		 */
78
79
		public function setItems(array $items) : Loop {
80
81
			$this->removeItems(); $this->addItems($items);
82
83
			return $this;
84
		}
85
86
		/**
87
		 * Get the items list
88
		 */
89
90
		public function getItems() : array {
91
92
			return $this->items->getItems();
93
		}
94
95
		/**
96
		 * Get the items count
97
		 */
98
99
		public function getCount() : int {
100
101
			return $this->items->getCount();
102
		}
103
104
		/**
105
		 * Get the loop contents
106
		 */
107
108
		public function getContents() : string {
109
110
			return $this->items->getContents();
111
		}
112
	}
113
}
114