Completed
Push — master ( a378d5...7b6893 )
by Flo
28s
created

FormError::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class FormError | FormError.php
4
 * @package Faulancer\View\Helper
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\View\Helper;
8
9
use Faulancer\Session\SessionManager;
10
use Faulancer\View\AbstractViewHelper;
11
use Faulancer\View\ViewController;
12
13
/**
14
 * Class FormError
15
 */
16
class FormError extends AbstractViewHelper
17
{
18
19
    /** @var ViewController */
20
    protected $view;
21
22
    /** @var string */
23
    protected $field;
24
25
    /**
26
     * Initializing form error helper
27
     *
28
     * @param ViewController $view
29
     * @param string         $field
30
     * @return $this
31
     */
32
    public function __invoke(ViewController $view, string $field)
33
    {
34
        $this->view  = $view;
35
        $this->field = $field;
36
37
        return $this;
38
    }
39
40
    /**
41
     * Get an error for specific field
42
     *
43
     * @return string
44
     */
45
    public function get()
46
    {
47
        $error = SessionManager::instance()->getFlashbagError($this->field);
48
49
        $result = '';
50
51
        if (!empty($error)) {
52
53
            $result = '<div class="form-error ' . $this->field . '">';
54
55
            foreach ($error as $err) {
56
                $result .= '<span>' . $this->view->translate($err['message']) . '</span>';
0 ignored issues
show
Documentation Bug introduced by
The method translate does not exist on object<Faulancer\View\ViewController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
57
            }
58
59
            $result .= '</div>';
60
61
        }
62
63
        return $result;
64
    }
65
66
    /**
67
     * Check if a field name has an error
68
     *
69
     * @return bool
70
     */
71
    public function has()
72
    {
73
        return SessionManager::instance()->hasFlashbagErrorsKey($this->field);
74
    }
75
76
}