1 | <?php |
||
27 | use Psi\Component\Grid\Column\MoneyColumn; |
||
28 | |||
29 | final class GridFactoryBuilder |
||
30 | { |
||
31 | private $agentFinder; |
||
32 | private $formFactoryBuilder; |
||
33 | |||
34 | private $actions = []; |
||
35 | private $columns = []; |
||
36 | private $filters = []; |
||
37 | private $metadataDrivers = []; |
||
38 | |||
39 | public static function create( |
||
40 | AgentFinder $agentFinder, |
||
41 | FormFactoryBuilderInterface $formFactoryBuilder = null |
||
42 | ) { |
||
43 | $instance = new self(); |
||
44 | $instance->agentFinder = $agentFinder; |
||
45 | $instance->formFactoryBuilder = $formFactoryBuilder ?: Forms::createFormFactoryBuilder(); |
||
46 | |||
47 | return $instance; |
||
48 | } |
||
49 | |||
50 | public static function createWithDefaults(AgentFinder $agentFinder, FormFactoryBuilderInterface $formFactoryBuilder = null) |
||
51 | { |
||
52 | return self::create($agentFinder, $formFactoryBuilder) |
||
53 | ->addAction(new DeleteAction(), 'delete') |
||
54 | ->addColumn(new PropertyColumn(), 'property') |
||
55 | ->addColumn(new SelectColumn($agentFinder), 'select') |
||
56 | ->addColumn(new DateTimeColumn(), 'datetime') |
||
57 | ->addColumn(new MoneyColumn(), 'money') |
||
58 | ->addColumn(new BooleanColumn(), 'boolean') |
||
59 | ->addFilter(new StringFilter(), 'string') |
||
60 | ->addFilter(new BooleanFilter(), 'boolean') |
||
61 | ->addFilter(new NumberFilter(), 'number'); |
||
62 | } |
||
63 | |||
64 | public function addArrayDriver(array $mapping) |
||
65 | { |
||
66 | $this->metadataDrivers[] = new ArrayDriver($mapping); |
||
67 | |||
68 | return $this; |
||
69 | } |
||
70 | |||
71 | public function addAnnotationDriver() |
||
72 | { |
||
73 | $this->metadataDrivers[] = new AnnotationDriver(); |
||
74 | |||
75 | return $this; |
||
76 | } |
||
77 | |||
78 | public function addAction(ActionInterface $action, $alias = null): self |
||
79 | { |
||
80 | $this->actions[$alias] = $action; |
||
81 | |||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | public function addFilter(FilterInterface $filter, $alias = null) |
||
86 | { |
||
87 | $this->filters[$alias] = $filter; |
||
88 | |||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | public function addColumn(ColumnInterface $column, $alias = null) |
||
93 | { |
||
94 | $this->columns[$alias] = $column; |
||
95 | |||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | public function createGridFactory() |
||
100 | { |
||
101 | if (empty($this->metadataDrivers)) { |
||
102 | throw new \InvalidArgumentException( |
||
103 | 'You must add at least one metadata driver (e.g. ->addArrayDriver, ->addAnnotationDriver, ->addXmlDriver)' |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | $actionRegistry = new ActionRegistry(); |
||
108 | foreach ($this->actions as $alias => $action) { |
||
109 | $actionRegistry->register($action, $alias); |
||
110 | } |
||
111 | $columnRegistry = new ColumnRegistry(); |
||
112 | foreach ($this->columns as $alias => $column) { |
||
113 | $columnRegistry->register($column, $alias); |
||
114 | } |
||
115 | $filterRegistry = new FilterRegistry(); |
||
116 | foreach ($this->filters as $alias => $filter) { |
||
117 | $filterRegistry->register($filter, $alias); |
||
118 | } |
||
119 | |||
120 | $metadataDriver = new DriverChain($this->metadataDrivers); |
||
121 | $columnFactory = new ColumnFactory($columnRegistry); |
||
122 | $metadataFactory = new MetadataFactory($metadataDriver); |
||
123 | |||
124 | $validator = Validation::createValidator(); |
||
125 | |||
126 | $formFactory = $this->formFactoryBuilder |
||
127 | ->addExtension(new ValidatorExtension($validator)) |
||
128 | ->addExtension(new GridExtension( |
||
129 | $columnRegistry, |
||
130 | $filterRegistry |
||
131 | )) |
||
132 | ->getFormFactory(); |
||
133 | |||
134 | $filterFactory = new FilterBarFactory($formFactory, $filterRegistry); |
||
135 | $queryFactory = new QueryFactory($metadataFactory); |
||
136 | |||
137 | $gridViewFactory = new GridViewFactory( |
||
138 | $columnFactory, |
||
139 | $filterFactory, |
||
140 | $queryFactory |
||
141 | ); |
||
142 | |||
143 | $actionPerformer = new ActionPerformer($actionRegistry); |
||
144 | |||
145 | return new GridFactory( |
||
146 | $this->agentFinder, |
||
147 | $metadataFactory, |
||
148 | $gridViewFactory, |
||
149 | $actionPerformer |
||
150 | ); |
||
151 | } |
||
153 |