1: <?php
2:
3: namespace BN;
4:
5: use BN\Compiler\Grammar\OperatorBuilder;
6:
7: class OperatorsFactory
8: {
9: private $operators;
10: private $aggregation;
11:
12: public function __construct()
13: {
14: $this->aggregation = new AggregateFunctions();
15: $this->operators = array(
16: 'abs' => $this->unaryOperator('abs')->leftAssociative()->precedence(6),
17: 'neg' => $this->unaryOperator('negate')->leftAssociative()->precedence(6),
18: 'floor' => $this->unaryOperator('roundDown')->leftAssociative()->precedence(5),
19: 'ceil' => $this->unaryOperator('roundUp')->leftAssociative()->precedence(5),
20: 'round' => $this->binaryOperator('round')->rightAssociative()->precedence(5),
21: 'roundTo' => $this->binaryOperator('roundToNumber')->rightAssociative()->precedence(5),
22: '^' => $this->binaryOperator('power')->rightAssociative()->precedence(4),
23: 'sqrt' => $this->unaryOperator('sqrt')->leftAssociative()->precedence(4),
24: '*' => $this->binaryOperator('multiply')->leftAssociative()->precedence(3),
25: '/' => $this->binaryOperator('divide')->leftAssociative()->precedence(3),
26: '\\' => $this->binaryOperator('quotient')->leftAssociative()->precedence(3),
27: '%' => $this->binaryOperator('modulo')->leftAssociative()->precedence(2),
28: '+' => $this->binaryOperator('add')->leftAssociative()->precedence(2),
29: '-' => $this->binaryOperator('subtract')->leftAssociative()->precedence(2),
30: 'sum' => $this->aggregateFunction('sum')->leftAssociative()->precedence(1),
31: 'count' => $this->aggregateFunction('count')->leftAssociative()->precedence(1),
32: 'avg' => $this->aggregateFunction('avg')->leftAssociative()->precedence(1),
33: 'min' => $this->aggregateFunction('min')->leftAssociative()->precedence(1),
34: 'max' => $this->aggregateFunction('max')->leftAssociative()->precedence(1)
35: );
36: }
37:
38: public function create($symbol)
39: {
40: return $this->operators[$symbol];
41: }
42:
43: public function getAll()
44: {
45: return $this->operators;
46: }
47:
48: private function binaryOperator($method)
49: {
50: $operator = new OperatorBuilder();
51: return $operator
52: ->binary(function ($a, $b) use ($method) {
53: return $a->$method($b);
54: });
55: }
56:
57: private function unaryOperator($method)
58: {
59: $operator = new OperatorBuilder();
60: return $operator
61: ->unary(function ($a) use ($method) {
62: return $a->$method();
63: });
64: }
65:
66: private function aggregateFunction($method)
67: {
68: $operator = new OperatorBuilder();
69: return $operator->aggregate(array($this->aggregation, $method));
70: }
71: }
72: