Lab.php 4.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php
/**
 * gredu_labs
 * 
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

namespace GrEduLabs\Schools\InputFilter;

12
use GrEduLabs\Schools\Service\LabServiceInterface;
13
use Zend\Filter;
14
use Zend\InputFilter\FileInput;
15
16
17
18
19
20
21
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\Validator;

class Lab
{
    use InputFilterTrait;
kanellov's avatar
kanellov committed
22

23
    public function __construct(
kanellov's avatar
kanellov committed
24
        $uploadTmpPath,
25
26
        LabServiceInterface $labService,
        $attachmentSize
27
    ) {
28
29
30
31
32
33
34
35
36
37
38
39
40
        $id = new Input('id');
        $id->setRequired(false)
            ->getValidatorChain()
            ->attach(new Validator\Digits());

        $name = new Input('name');
        $name->setRequired(true)
            ->getFilterChain()
            ->attach(new Filter\StringTrim());
        $name->getValidatorChain()
            ->attach(new Validator\NotEmpty())
            ->attach(new Validator\StringLength(['min' => 3]));

41
42
43
        $labTypeId = new Input('labtype_id');
        $labTypeId->setRequired(true);
        $labTypeId->getValidatorChain()
44
45
            ->attach(new Validator\NotEmpty());

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
46
47
48
49
50
        $isNew = new Input('is_new');
        $isNew->setRequired(false)
            ->getFilterChain()
            ->attach(new Filter\ToInt());

51
52
        $responsibleId = new Input('responsible_id');
        $responsibleId->setRequired(false)
53
54
55
56
57
58
59
60
61
62
            ->getValidatorChain()
            ->attach(new Validator\Digits());

        $area = new Input('area');
        $area->setRequired(true)
            ->getValidatorChain()
            ->attach(new Validator\NotEmpty())
            ->attach(new Validator\Digits());

        $lessons = new Input('lessons');
63
64
        $lessons->setRequired(false);
        $lessons->getValidatorChain()
65
            ->attach(new Validator\NotEmpty());
kanellov's avatar
kanellov committed
66

67
68
69
70
        $attachment = new FileInput('attachment');
        $attachment->setRequired(false)
            ->getFilterChain()
            ->attach(new Filter\File\RenameUpload([
kanellov's avatar
kanellov committed
71
                'target'    => $uploadTmpPath,
72
73
74
                'randomize' => true,
            ]));
        $attachment->getValidatorChain()
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
            ->attach(new Validator\File\UploadFile())
            ->attach(new Validator\File\MimeType([
                'application/zip',
                'application/x-rar-compressed',
                'application/octet-stream',
                'application/pdf',
                'image/png',
                'image/jpeg',
                'image/gif',
                'image/bmp',
                'image/vnd.microsoft.icon',
                'image/tiff',
                'image/tiff',
                'image/svg+xml',
                'image/svg+xml',
                'image/vnd.adobe.photoshop',
91
92
93
            ]))
            ->attach(new Validator\File\Size([
                'max' => $attachmentSize,
94
            ]));
kanellov's avatar
kanellov committed
95

96
97
98
99
100
101
102
103
        $use_ext_program= new Input('use_ext_program');
        $use_ext_program->setRequired(false);

        $use_in_program = new Input('use_in_program');
        $use_in_program->setRequired(false);

        $has_network = new Input('has_network');
        $has_network->setRequired(false);
104
105
106
107
        $has_network->getValidatorChain()
            ->attach(new Validator\NotEmpty())->attach(new Validator\InArray([
                'haystack' => $labService->getHasNetworkValues(),
            ]));
kanellov's avatar
kanellov committed
108

109
110
111
112
113
114
115
        $has_server = new Input('has_server');
        $has_server->setRequired(false);
        $has_server->getValidatorChain()
            ->attach(new Validator\NotEmpty())
            ->attach(new Validator\InArray([
                'haystack' => $labService->getHasServerValues(),
            ]));
116
117
118
119
120
121


        $this->inputFilter = new InputFilter();
        $this->inputFilter
            ->add($id)
            ->add($name)
122
            ->add($labTypeId)
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
123
            ->add($isNew)
124
            ->add($responsibleId)
125
126
127
128
129
130
131
132
133
            ->add($area)
            ->add($lessons)
            ->add($attachment)
            ->add($use_in_program)
            ->add($use_ext_program)
            ->add($has_server)
            ->add($has_network);
    }
}