1 | <?php |
||
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 |