On Thu, Oct 10, 2019, 6:42 PM Anton Fedonjuk <[email protected]>
wrote:
> Class contains method setRequest(RequestInterface $request), his abstact
> code:
> Request body already sets: $request->getBody()->write('...');
> I want send this request with other body content, but dont know how:
> 1. StreamInterface don't have methods as PHP ftruncate()
> 2. Constructor not defined in StreamInterface: can't use
> $request->withBody() because dont know how create clone of that Steam
> object without contents.
>
To provide new contents, you provide a new body to the request, which means
using a new StreamInterface instance.
To get a new StreamInterface instance, you either instantiate I've
directly, per the PSR-7 *implementation* you have installed, or you use a
PSR-17 StreamFactoryInterface instance to create it. This is something you
would compose into your class as a constructor dependency.
In each case, you need an *implementation* of one or both of the specs
installed, which is already likely the case.
In the first case, if using Diactoros, your code might look like this:
$request = $this->getRequest()
->withBody(new \Zend\Diactoros\Stream('php://temp', 'wb+'));
In the second, it would look like this:
$request = $this->getRequest()
->withBody($this->streamFactory->createStream());
>From there, you can call on the stream's write() method. Alternately, if
using the factory, create the entire stream *with contents* to save
yourself the step.
Regarding your point that there is no StreamInterface constructor,
the constructor is not defined in any of the interfaces because:
- We did not want to restrict how implementations wrote constructors.
- PHP ALWAYS allows classes implementing interfaces to override the
signature of a constructor.
As such, you always have to look at how the *implementation* to determine
the signature of constructors.
This is one large reason PSR-17 exists - to standardize how instances of
psr-7 interfaces are created.
Regardless, each of the above examples give you an instance with an empty
body.
> --
> You received this message because you are subscribed to the Google Groups
> "PHP Framework Interoperability Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/php-fig/84a56774-1dbf-4ec3-b906-b29e8b322add%40googlegroups.com
> <https://groups.google.com/d/msgid/php-fig/84a56774-1dbf-4ec3-b906-b29e8b322add%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups "PHP
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/php-fig/CAJp_myUCo3reWr8z_WOHNvs1JNOVxPTkD_gps2d_fKh0ZnHiqQ%40mail.gmail.com.