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

ArticleValidator::getMessages()   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
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