Listing::valid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Saxulum\Crud\Listing;
4
5
use Saxulum\Crud\Listing\Type\TypeInterface;
6
7
class Listing implements \Iterator
8
{
9
    /**
10
     * @var TypeInterface[]
11
     */
12
    protected $types;
13
14
    /**
15
     * @var \ReflectionClass
16
     */
17
    protected $reflectionClass;
18
19
    /**
20
     * @var Field[]
21
     */
22
    protected $fields;
23
24
    /**
25
     * @var int
26
     */
27
    protected $fieldPointer = 0;
28
29
    /**
30
     * @param TypeInterface[] $types
31
     * @param string          $class
32
     */
33 View Code Duplication
    public function __construct(array $types, $class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        foreach ($types as $type) {
36
            if (!$type instanceof TypeInterface) {
37
                throw new \InvalidArgumentException(sprintf(
38
                    'Type must be an instance of %s, %s given!',
39
                    'Saxulum\Crud\Listing\Type\TypeInterface',
40
                    is_object($type) ? get_class($type) : gettype($type)
41
                ));
42
            }
43
            $this->types[$type->getName()] = $type;
44
        }
45
        $this->reflectionClass = new \ReflectionClass($class);
46
    }
47
48
    /**
49
     * @param string $name
50
     * @param string $type
51
     * @param array  $options
52
     *
53
     * @return $this
54
     */
55
    public function add($name, $type = 'string', array $options = array())
56
    {
57
        $getMethod = 'get'.ucfirst($name);
58
        $isMethod = 'is'.ucfirst($name);
59
60
        $isAccessible = false;
61
        if (($this->reflectionClass->hasProperty($name) && $this->reflectionClass->getProperty($name)->isPublic()) ||
62
           ($this->reflectionClass->hasMethod($getMethod) && $this->reflectionClass->getMethod($getMethod)->isPublic()) ||
63
           ($this->reflectionClass->hasMethod($isMethod) && $this->reflectionClass->getMethod($isMethod)->isPublic())) {
64
            $isAccessible = true;
65
        }
66
67
        if (!$isAccessible) {
68
            throw new \InvalidArgumentException(sprintf('There is no public property, get or is method for: %s!', $name));
69
        }
70
71
        if (!$type instanceof TypeInterface) {
72
            $type = $this->getType($type);
73
        }
74
75
        $this->fields[] = new Field(
76
            $name,
77
            $type->getName(),
78
            $type->getTemplate(),
79
            $options
80
        );
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param string $type
87
     *
88
     * @return TypeInterface
89
     *
90
     * @throws \InvalidArgumentException
91
     */
92
    protected function getType($type)
93
    {
94
        if (!isset($this->types[$type])) {
95
            throw new \InvalidArgumentException(sprintf('There is no type with name %s', $type));
96
        }
97
98
        return $this->types[$type];
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104
    public function current()
105
    {
106
        return $this->fields[$this->fieldPointer];
107
    }
108
109
    /**
110
     */
111
    public function next()
112
    {
113
        $this->fieldPointer++;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function key()
120
    {
121
        return $this->fields[$this->fieldPointer]->getName();
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function valid()
128
    {
129
        return array_key_exists($this->fieldPointer, $this->fields);
130
    }
131
132
    /**
133
     */
134
    public function rewind()
135
    {
136
        $this->fieldPointer = 0;
137
    }
138
}
139