1 | <?php |
||
19 | trait WithBodyStreamTrait |
||
20 | { |
||
21 | use WithBodyTrait { |
||
22 | WithBodyTrait::setBody as private setStringBody; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Set body with stream acceptance |
||
27 | * @param resource|string $body Request body |
||
28 | */ |
||
29 | 2 | public function setBody($body) |
|
30 | { |
||
31 | 2 | if (is_string($body)) { |
|
32 | 1 | return $this->setStringBody($body); |
|
|
|||
33 | } |
||
34 | |||
35 | 2 | if (!is_resource($body)) { |
|
36 | throw new InvalidArgumentException('Body must be a stream !'); |
||
37 | } |
||
38 | 2 | if (!stream_is_local($body)) { |
|
39 | throw new InvalidArgumentException('Body must be a local stream !'); |
||
40 | } |
||
41 | |||
42 | 2 | rewind($body); |
|
43 | 2 | $this->body = $body; |
|
44 | 2 | } |
|
45 | |||
46 | /** |
||
47 | * Check if the current body is a stream or not |
||
48 | * @return boolean |
||
49 | */ |
||
50 | 3 | public function hasBodyStream() |
|
54 | |||
55 | /** |
||
56 | * Retrieve stream meta data if available |
||
57 | * @return array|null |
||
58 | */ |
||
59 | 2 | public function getMetaData() |
|
66 | |||
67 | /** |
||
68 | * Retrieve the body length from string or stream |
||
69 | * @return integer |
||
70 | */ |
||
71 | 2 | public function getBodyLength() |
|
78 | } |
||
79 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.