Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.48% covered (success)
90.48%
38 / 42
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseColumn
90.48% covered (success)
90.48%
38 / 42
33.33% covered (danger)
33.33%
2 / 6
9.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
3
 setName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 shouldSkip
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toModelColumn
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 fromProperty
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2namespace Lucent\Database\Attributes;
3
4use Attribute;
5use Deprecated;
6use ReflectionProperty;
7
8#[Attribute(Attribute::TARGET_PROPERTY)]
9class DatabaseColumn
10{
11    public private(set) array $column;
12
13    public function __construct(array $properties)
14    {
15        trigger_error(
16            sprintf('%s is deprecated. Use %s instead.', self::class, \Lucent\Model\Column::class),
17            E_USER_DEPRECATED
18        );
19
20        //Define our column defaults
21        $this->column = [
22            "NAME" => null,
23            "ALLOW_NULL" => true,
24            "LENGTH" => 255,
25            "AUTO_INCREMENT" => false,
26            "TYPE" => null,
27            "PRIMARY_KEY" => false,
28            "DEFAULT" => null,
29            "VALUES" => [],
30            "UNIQUE_KEY_TO" => null,
31            "ON_UPDATE" => null,
32            "REFERENCES" => null,
33            "UNIQUE" => null,
34            "UNSIGNED" => false
35        ];
36
37        //Loop over all our properties and translate them into our column
38        foreach (array_keys($this->column) as $item) {
39            if (array_key_exists($item, $properties)) {
40                $this->column[$item] = $properties[$item];
41            }
42        }
43    }
44
45    public function setName(string $name): void
46    {
47        $this->column["NAME"] = $name;
48    }
49
50    public function getName(): string
51    {
52        return $this->column["NAME"];
53    }
54
55    public function shouldSkip(): bool
56    {
57        return $this->column["AUTO_INCREMENT"];
58    }
59
60    public function toModelColumn(): \Lucent\Model\Column
61    {
62        return new \Lucent\Model\Column(
63            type: \Lucent\Model\ColumnType::from($this->column['TYPE']),
64            name: $this->column['NAME'],
65            nullable: $this->column['ALLOW_NULL'],
66            length: $this->column['LENGTH'],
67            autoIncrement: $this->column['AUTO_INCREMENT'],
68            primaryKey: $this->column['PRIMARY_KEY'],
69            default: $this->column['DEFAULT'],
70            values: $this->column['VALUES'],
71            references: $this->column['REFERENCES'],
72            unique: $this->column['UNIQUE'],
73            unsigned: $this->column['UNSIGNED']
74        );
75    }
76
77    public static function fromProperty(ReflectionProperty $property): ?self
78    {
79        $attributes = $property->getAttributes(self::class);
80        if (empty($attributes)) {
81            return null;
82        }
83
84        return $attributes[0]->newInstance();
85    }
86}