1: <?php
2: namespace NGS\Patterns;
3:
4: require_once(__DIR__.'/../Converter/PrimitiveConverter.php');
5: require_once(__DIR__.'/../Client/DomainProxy.php');
6: require_once(__DIR__.'/OlapCube.php');
7: require_once(__DIR__.'/../Utils.php');
8:
9: use \NGS\Client\DomainProxy;
10: use \NGS\Converter\PrimitiveConverter;
11: use \NGS\Patterns\OlapCube;
12: use \NGS\Utils;
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class CubeBuilder
27: {
28: protected $cube;
29: protected $dimensions = array();
30: protected $facts = array();
31: protected $order = array();
32: protected $specification;
33:
34: 35: 36: 37: 38:
39: public function __construct(OlapCube $cube)
40: {
41: $this->cube = $cube;
42: }
43:
44: 45: 46: 47: 48: 49: 50: 51:
52: public function dimension($dimension)
53: {
54: if (!is_string($dimension)) {
55: throw new \InvalidArgumentException('Dimension must be a string. Invalid type "'.Utils::getType($dimension).'" given for cube "'.get_class($this->cube).'"');
56: }
57: if (!in_array($dimension, $this->cube->getDimensions())) {
58: throw new \InvalidArgumentException('Property '.$dimension.' is not a valid dimension for cube '.get_class($this->cube));
59: }
60: if (!in_array($dimension, $this->dimensions)) {
61: $this->dimensions[] = $dimension;
62: }
63: return $this;
64: }
65:
66: 67: 68: 69: 70: 71: 72:
73: public function dimensions(array $dimensions)
74: {
75: foreach ($dimensions as $dim) {
76: $this->dimension($dim);
77: }
78: return $this;
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88:
89: public function fact($fact)
90: {
91: if (!is_string($fact)) {
92: throw new \InvalidArgumentException('Fact must be a string. Invalid type "'.Utils::getType($fact).'" given for cube "'.get_class($this->cube).'"');
93: }
94: if (!in_array($fact, $this->cube->getFacts())) {
95: throw new \InvalidArgumentException('Property '.$fact.' is not a valid fact for cube '.get_class($this->cube));
96: }
97: if (!in_array($fact, $this->facts)) {
98: $this->facts[] = $fact;
99: }
100: return $this;
101: }
102:
103: 104: 105: 106: 107: 108: 109: 110:
111: public function facts(array $facts)
112: {
113: foreach ($facts as $fact) {
114: $this->fact($fact);
115: }
116: return $this;
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126:
127: public function add($dimensionOrFact)
128: {
129: if (!is_string($dimensionOrFact)) {
130: throw new \InvalidArgumentException('Dimension or fact must be a string, invalid type "'.Utils::getType($dimensionOrFact).'" given for cube '.get_class($this->cube));
131: }
132: if (in_array($dimensionOrFact, $this->cube->getDimensions())) {
133: $this->dimension($dimensionOrFact);
134: } elseif (in_array($dimensionOrFact, $this->cube->getFacts())) {
135: $this->fact($dimensionOrFact);
136: } else {
137: throw new \InvalidArgumentException('Invalid fact or dimension "'.$dimensionOrFact.'" for cube '.get_class($this->cube));
138: }
139: return $this;
140: }
141:
142: 143: 144: 145: 146: 147:
148: public function ascending ($dimensionOrFact)
149: {
150: $this->validateDimensionOrFact($dimensionOrFact);
151: $this->order[$dimensionOrFact] = true;
152: return $this;
153: }
154:
155: 156: 157:
158: public function asc($dimensionOrFact)
159: {
160: return $this->ascending($dimensionOrFact);
161: }
162:
163: 164: 165: 166: 167: 168:
169: public function descending ($dimensionOrFact)
170: {
171: $this->validateDimensionOrFact($dimensionOrFact);
172: $this->order[$dimensionOrFact] = false;
173: return $this;
174: }
175:
176: 177: 178:
179: public function desc($dimensionOrFact)
180: {
181: return $this->descending($dimensionOrFact);
182: }
183:
184: 185: 186: 187: 188: 189:
190: public function with(Specification $specification)
191: {
192: $this->specification = $specification;
193: return $this;
194: }
195:
196: 197: 198: 199: 200:
201: public function analyze()
202: {
203: return $this->cube->analyze(
204: $this->dimensions,
205: $this->facts,
206: $this->order,
207: $this->specification
208: );
209: }
210:
211: 212: 213: 214: 215: 216:
217: private function validateDimensionOrFact($name)
218: {
219: if (!is_string($name)) {
220: throw new \InvalidArgumentException('Dimension or fact for cube "'.get_class($this->cube).'"" was not a string');
221: }
222: if (!in_array($name, $this->cube->getDimensions()) && !in_array($name, $this->cube->getFacts())) {
223: throw new \InvalidArgumentException('Invalid fact or dimension "'.$name.'" for cube '.get_class($this->cube));
224: }
225: }
226: }
227: