Completed
Push — master ( 31e846...f233e3 )
by Askupa
02:37
created

Component_number::validation()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 8
nop 2
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Amarkal\UI;
4
5
/**
6
 * Implements a number UI component.
7
 */
8
class Component_number
9
extends AbstractComponent
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
10
implements ValueComponentInterface, 
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
11
           DisableableComponentInterface,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
12
           FilterableComponentInterface,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
13
           ValidatableComponentInterface
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
14
{
15
    public function default_model() 
16
    {
17
        return array(
18
            'name'          => '',
19
            'id'            => '',
20
            'disabled'      => false,
21
            'size'          => null,
22
            'min'           => null,
23
            'max'           => null,
24
            'step'          => null,
25
            'required'      => false,
26
            'readonly'      => false,
27
            'default'       => null,
28
            'filter'        => array( $this, 'filter' ),
29
            'validation'    => array( $this, 'validation' )
30
        );
31
    }
32
    
33
    public function filter($v)
34
    {
35
        return floatval($v);
36
    }
37
    
38
    public function validation($v,&$e)
39
    {
40
        $max = $this->max;
0 ignored issues
show
Documentation introduced by
The property max does not exist on object<Amarkal\UI\Component_number>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
41
        $min = $this->min;
0 ignored issues
show
Documentation introduced by
The property min does not exist on object<Amarkal\UI\Component_number>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42
        
43
        if(null !== $max && $v > $max)
44
        {
45
            $e = "must be less than {$max}";
46
        }
47
48
        if(null !== $min && $v < $min) 
49
        {
50
            $e = "must be greater than {$min}";
51
        }
52
53
        return $e ? false : true;
54
    }
55
    
56
    public function required_arguments()
57
    {
58
        return array('name');
59
    }
60
    
61
    public function get_template_path() 
62
    {
63
        return __DIR__.'/template.phtml';
64
    }
65
}