Issues (12)

src/Helpers/Schema.php (1 issue)

Severity
1
<?php
2
3
/**
4
 * Database Helper class for structuring the database correctly.
5
 *
6
 * PHP version 7.4
7
 *
8
 * @category Helpers
9
 * @package  Chaospelt\Helpers
10
 *
11
 * @author  Stf Kolev <[email protected]>
12
 * @license BSD-3-Clause https://opensource.org/licenses/BSD-3-Clause
13
 *
14
 * @link https://github.com/stfkolev/chaospelt
15
 */
16
17
namespace Chaospelt\Helpers;
18
19
use Chaospelt\Kernel\Exceptions\ArgumentNotAClosureException;
20
use Chaospelt\Kernel\Exceptions\ClosureNoArgumentsException;
21
use Chaospelt\Kernel\Exceptions\BuilderInvalidCreateUsageException;
22
23
use Closure;
24
use ReflectionFunction;
25
26
/**
27
 * Database Helper class for structuring the database correctly
28
 * Validator for all type of requests.
29
 *
30
 * PHP version 7.4
31
 *
32
 * @category Helpers
33
 * @package  Chaospelt\Helpers
34
 *
35
 * @author  Stf Kolev <[email protected]>
36
 * @license BSD-3-Clause https://opensource.org/licenses/BSD-3-Clause
37
 *
38
 * @link https://github.com/stfkolev/chaospelt
39
 */
40
class Schema
41
{
42
    /**
43
     * The default string length for migrations.
44
     *
45
     * @var int
46
     */
47
    public static $defaultStringLength = 255;
48
    
49
    /**
50
     * Create a table
51
     * 
52
     * @param string $tableName The name of the table to create
53
     * @param mixed  $callback  The function to use
54
     * 
55
     * @return self
56
     */
57
    public static function create($tableName, Closure $callback) 
58
    {
59
        if ($callback instanceof Closure) {
0 ignored issues
show
$callback is always a sub-type of Closure.
Loading history...
60
            $reflectedClosure = new ReflectionFunction($callback);
61
            
62
            if (!empty($reflectedClosure->getParameters())) {
63
                $reflectedParameter = reset($reflectedClosure->getParameters());
64
65
                if ($reflectedParameter->getType() == \Chaospelt\Kernel\Database\Blueprint::class) {
66
                    $resolver = new \Chaospelt\Kernel\Database\Blueprint($tableName);
67
68
                    \call_user_func($callback, $resolver);
69
                }
70
            } else {
71
                throw new ClosureNoArgumentsException();
72
            }
73
        } else {
74
            throw new ArgumentNotAClosureException();
75
        }
76
    }
77
}