Overview

Namespaces

  • NGS
    • Client
      • Exception
    • Converter
    • Patterns
  • PHP

Classes

  • AggregateDomainEvent
  • AggregateRoot
  • CubeBuilder
  • DomainEvent
  • GenericSearch
  • Identifiable
  • OlapCube
  • Repository
  • Search
  • Searchable
  • SearchBuilder
  • Snapshot
  • Specification
  • Templater

Interfaces

  • IDomainObject
  • IIdentifiable
  • Overview
  • Namespace
  • Class
  • Tree
  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:  * Helper for createing and analyizing OLAP cubes
 16:  *
 17:  * Example:
 18:  * <code>
 19:  * $builder = new CubeBuilder('My\Cube');
 20:  * $rows = $builder->dimension('money')
 21:  *                 ->facts('total', 'average')
 22:  *                 ->descending('average')
 23:  *                 ->analyze();
 24:  * </code>
 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:      * Create new builder on cube
 36:      *
 37:      * @param OlapCube $cube
 38:      */
 39:     public function __construct(OlapCube $cube)
 40:     {
 41:         $this->cube = $cube;
 42:     }
 43: 
 44:     /**
 45:      * Add single dimension to cube
 46:      *
 47:      * @param $dimension A valid dimension
 48:      * @return $this
 49:      * @throws \InvalidArgumentException Invalid type or not a valid dimension
 50:      * for given cube
 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:      * Add multiple dimensions to cube
 68:      *
 69:      * @param array $dimensions
 70:      * @internal param array $dimension Valid dimensions
 71:      * @return $this
 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:      * Add single fact to cube
 83:      *
 84:      * @param $fact string A valid fact
 85:      * @return $this
 86:      * @throws \InvalidArgumentException Invalid type or not a valid fact for
 87:      * given cube
 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:      * Add multiple facts to cube
105:      *
106:      * @param $fact array Valid facts
107:      * @return $this
108:      * @throws \InvalidArgumentException Invalid type or not a valid fact
109:      * for given cube
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:      * Adds single dimension or facts to cube
121:      *
122:      * @param $dimensionOrFact string A valid dimension or fact
123:      * @return $this
124:      * @throws \InvalidArgumentException Invalid type or not a valid fact or
125:      * dimension for given cube
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:      * Order ascending by dimension or fact
144:      *
145:      * @param $dimensionOrFact string A valid dimension or fact
146:      * @return $this
147:      */
148:     public function ascending ($dimensionOrFact)
149:     {
150:         $this->validateDimensionOrFact($dimensionOrFact);
151:         $this->order[$dimensionOrFact] = true;
152:         return $this;
153:     }
154: 
155:     /**
156:      * @see CubeBuilder::ascending
157:      */
158:     public function asc($dimensionOrFact)
159:     {
160:         return $this->ascending($dimensionOrFact);
161:     }
162: 
163:     /**
164:      * Order descending by dimension or fact
165:      *
166:      * @param $dimensionOrFact string A valid dimension or fact
167:      * @return $this
168:      */
169:     public function descending ($dimensionOrFact)
170:     {
171:         $this->validateDimensionOrFact($dimensionOrFact);
172:         $this->order[$dimensionOrFact] = false;
173:         return $this;
174:     }
175: 
176:     /**
177:      * @see CubeBuilder::descending
178:      */
179:     public function desc($dimensionOrFact)
180:     {
181:         return $this->descending($dimensionOrFact);
182:     }
183: 
184:     /**
185:      * Use specification in cube analysis
186:      *
187:      * @param Specification
188:      * @return $this
189:      */
190:     public function with(Specification $specification)
191:     {
192:         $this->specification = $specification;
193:         return $this;
194:     }
195: 
196:     /**
197:      * Execute cube analysis with builder settings
198:      *
199:      * @return array Resulting rows
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:      * Check if dimension or fact is valid for cube used in builder
213:      *
214:      * @param $name
215:      * @throws \InvalidArgumentException
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: 
API documentation generated by ApiGen 2.8.0