Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

src/Kunstmaan/AdminListBundle/AdminList/Field.php (1 issue)

Check that @param annotations have the correct type.

Documentation Informational

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\AdminList;
4
5
use Symfony\Component\PropertyAccess\PropertyAccess;
6
7
/**
8
 * Field
9
 */
10
class Field
11
{
12
    /**
13
     * @var string
14
     */
15
    private $header;
16
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @var bool
24
     */
25
    private $sort;
26
27
    /**
28
     * @var null|string
29
     */
30
    private $template;
31
32
    /**
33
     * @var null|FieldAlias
34
     */
35
    private $alias;
36
37
    /**
38
     * @param string     $name     The name
39
     * @param string     $header   The header
40
     * @param bool       $sort     Sort or not
41
     * @param string     $template The template
42
     * @param FieldAlias $alias    The alias
0 ignored issues
show
Should the type for parameter $alias not be null|FieldAlias?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
43
     */
44
    public function __construct($name, $header, $sort = false, $template = null, FieldAlias $alias = null)
45
    {
46
        $this->name = $name;
47
        $this->header = $header;
48
        $this->sort = $sort;
49
        $this->template = $template;
50
        $this->alias = $alias;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getName()
57
    {
58
        return $this->name;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getHeader()
65
    {
66
        return $this->header;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function isSortable()
73
    {
74
        return $this->sort;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getTemplate()
81
    {
82
        return $this->template;
83
    }
84
85
    /**
86
     * @return FieldAlias|null
87
     */
88
    public function getAlias()
89
    {
90
        return $this->alias;
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    public function hasAlias()
97
    {
98
        if (is_null($this->alias)) {
99
            return false;
100
        }
101
102
        return true;
103
    }
104
105
    public function getAliasObj($item)
106
    {
107
        $relation = $this->alias->getRelation();
108
        $accessor = PropertyAccess::createPropertyAccessor();
109
110
        if ($accessor->isReadable($item, $relation)) {
111
            $item = $accessor->getValue($item, $relation);
112
        }
113
114
        return $item;
115
    }
116
117
    public function getColumnName($column)
118
    {
119
        $abbr = $this->alias->getAbbr().'.';
120
121
        if (strpos($column, $abbr) !== false) {
122
            $column = str_replace($abbr, '', $column);
123
        } else {
124
            throw new \Exception(" '".$abbr."' can not be found in your column name: '".$column."' ");
125
        }
126
127
        return $column;
128
    }
129
}
130