-
Notifications
You must be signed in to change notification settings - Fork 109
/
Builder.php
108 lines (92 loc) · 2.3 KB
/
Builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Sleimanx2\Plastic\Map;
use Closure;
use Sleimanx2\Plastic\Connection;
use Sleimanx2\Plastic\Exception\InvalidArgumentException;
class Builder
{
/**
* Plastic connection instance.
*
* @var Connection
*/
protected $connection;
/**
* Map grammar instance.
*
* @var Grammar
*/
protected $grammar;
/**
* Blueprint resolver callback.
*
* @var Closure
*/
protected $resolver;
/**
* Schema constructor.
*
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
$this->grammar = $connection->getMapGrammar();
}
/**
* Create a map on your elasticsearch index.
*
* @param string $type
* @param string $index
* @param Closure $callback
*/
public function create($type, Closure $callback, $index = null)
{
if (!is_string($type)) {
throw new InvalidArgumentException('type should be a string');
}
if ($index and !is_string($index)) {
throw new InvalidArgumentException('index should be a string');
}
$blueprint = $this->createBlueprint($type, $closure = null, $index);
$blueprint->create();
$callback($blueprint);
$this->build($blueprint);
}
/**
* Execute the blueprint to build.
*
* @param Blueprint $blueprint
*/
protected function build(Blueprint $blueprint)
{
$blueprint->build($this->connection, $this->grammar);
}
/**
* Create a new command set with a Closure.
*
* @param string $type
* @param Closure|null $callback
* @param null $index
*
* @return mixed|Blueprint
*/
protected function createBlueprint($type, Closure $callback = null, $index = null)
{
if (isset($this->resolver)) {
return call_user_func($this->resolver, $type, $callback, $index);
}
return new Blueprint($type, $callback, $index);
}
/**
* Set the Schema Blueprint resolver callback.
*
* @param \Closure $resolver
*
* @return void
*/
public function blueprintResolver(Closure $resolver)
{
$this->resolver = $resolver;
}
}