Completed
Pull Request — master (#39)
by
unknown
02:27
created

ArticleValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 23 5
A getMessages() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Admin\Validator;
5
6
class ArticleValidator implements \Admin\Validator\ValidatorInterface
7
{
8
    /**
9
     * @var \Zend\Stdlib\ArrayObject
10
     */
11
    private $messages;
12
13
    /**
14
     * Perform data validation for article entity.
15
     *
16
     * @param array $postData request post data
17
     *
18
     * @throws ValidatorException
19
     *
20
     * @return void
21
     */
22
    public function validate($postData)
23
    {
24
        $textValidator = new \Zend\Validator\StringLength();
25
        $textValidator->setMin(4);
26
27
        $this->messages = new \Zend\Stdlib\ArrayObject();
28
        if (!$textValidator->isValid($postData['title'])) {
29
            $this->messages->offsetSet('title', array_values($textValidator->getMessages()));
30
        }
31
32
        if (!$textValidator->isValid($postData['lead'])) {
33
            $this->messages->offsetSet('lead', array_values($textValidator->getMessages()));
34
        }
35
36
        //@TODO implement custom validator for article body (html structure) validation
37
        if (!$textValidator->isValid($postData['body'])) {
38
            $this->messages->offsetSet('body', array_values($textValidator->getMessages()));
39
        }
40
41
        if ($this->messages->count()) {
42
            throw new \Admin\Validator\ValidatorException('Data is not valid');
43
        }
44
    }
45
46
    /**
47
     * Messages getter method.
48
     *
49
     * @return \Zend\Stdlib\ArrayObject
50
     */
51
    public function getMessages() : \Zend\Stdlib\ArrayObject
52
    {
53
        return $this->messages;
54
    }
55
}
56