Passed
Branch master (7e4b7e)
by Mariano
04:51
created

RequestParser::addPropertyParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest;
19
20
use Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser;
21
22
/**
23
 * Abstract class with the common methods used by the current two RequestParser implementations.
24
 * This class contains the abstract method parse, which should be implemented in specific parser classes.
25
 * Basically it's a collection of PropertyParser objects that will be run for each property of the request.
26
 *
27
 * @author mcustiel
28
 */
29
abstract class RequestParser
30
{
31
    /**
32
     * @var PropertyParser[]
33
     */
34
    protected $propertyParsers;
35
    protected $requestObject;
36
37
    /**
38
     * Class constructor.
39
     */
40 97
    public function __construct()
41
    {
42 97
        $this->propertyParsers = [];
43 97
    }
44
45
    /**
46
     * Adds a property parser to the request parser.
47
     *
48
     * @param PropertyParser $parser
49
     */
50 95
    public function addPropertyParser(PropertyParser $parser)
51
    {
52 95
        $this->propertyParsers[$parser->getName()] = $parser;
53 95
    }
54
55
    /**
56
     * Sets the class in which the request must be converted.
57
     *
58
     * @param object $requestObject
59
     */
60 95
    public function setRequestObject($requestObject)
61
    {
62 95
        $this->requestObject = $requestObject;
63 95
    }
64
65
    /**
66
     * Returns the value of a property in the request.
67
     *
68
     * @param array|\stdClass $request
69
     * @param string          $propertyName
70
     */
71 96
    protected function getFromRequest($request, $propertyName)
72
    {
73 96
        return isset($request[$propertyName]) ? $request[$propertyName] : null;
74
    }
75
76 96
    protected function setProperty($request, $object, $propertyParser)
77
    {
78 96
        $propertyName = $propertyParser->getName();
79 96
        $value = $propertyParser->parse(
80 96
            $this->getFromRequest($request, $propertyName)
81 96
        );
82 94
        $method = 'set' . ucfirst($propertyName);
83 94
        $object->$method($value);
84 94
    }
85
86
    abstract public function parse($request);
87
}
88