| 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 | * @throws InvalidArgumentException |
||
| 29 | */ |
||
| 30 | 2 | public function setBody($body) |
|
| 31 | { |
||
| 32 | 2 | if (is_string($body)) { |
|
| 33 | 1 | return $this->setStringBody($body); |
|
|
|
|||
| 34 | } |
||
| 35 | |||
| 36 | 2 | if (!is_resource($body)) { |
|
| 37 | throw new InvalidArgumentException('Body must be a stream !'); |
||
| 38 | } |
||
| 39 | 2 | if (!stream_is_local($body)) { |
|
| 40 | throw new InvalidArgumentException('Body must be a local stream !'); |
||
| 41 | } |
||
| 42 | |||
| 43 | 2 | rewind($body); |
|
| 44 | 2 | $this->body = $body; |
|
| 45 | 2 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Check if the current body is a stream or not |
||
| 49 | * @return boolean |
||
| 50 | */ |
||
| 51 | 1 | public function hasBodyStream() |
|
| 55 | } |
||
| 56 |
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
Idableprovides a methodequalsIdthat 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.