ignace
-
Posts
6,457 -
Joined
-
Last visited
-
Days Won
26
Community Answers
-
ignace's post in Implications of changing an object from the inside? was marked as the answer
This design has no cohesion. If you look at ClientCollection and UnregisteredClient you see they are both aware of the internals of your clientList, that it has a property Client and that it is a stdClass.
Every class involved is made aware of a LengthPrefixStream.
Do not branch out of your code until it is absolutely necessary. Keep the clientList and connStream and all of that inside $server.
With your Client class you are on the right track.
-
ignace's post in API json redirecting to login was marked as the answer
You need to programmatically log in. Something like this:
function getSessionCookie($username, $password) { $context = [ 'http' => [ 'method' => 'POST', 'content' => http_build_query(['username_formfield' => $username, 'password_formfield' => $password]), ] ]; $contents = file_get_contents('http://apiwebsite.com/login', false, $context); if (false === $contents) { return false; } foreach ($http_response_header as $header) { if (0 === strpos($header, 'Set-Cookie')) { return explode(':', $header)[1]; } } return false; } function queryApi($sessionCookie, $apiCall) { $context = [ 'http' => [ 'header' => ["Cookie: $sessionCookie"], ] ]; $contents = file_get_contents('http://apiwebsite.com/blah?latest', false, $context); // your code } You'll still need to handle if the cookie expires etc.. and probably also need to parse the set-cookie header instead of simply copy-pasting. But this should get you started. -
ignace's post in Symfony and FormTypes was marked as the answer
Yes. Your field name should explain it's value:
public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add( 'game', EntityType::class, [ 'class' => 'AppBundle:Game', 'choice_label' => 'name', 'label' => 'name' ] ) ->add( 'type', EntityType::class, [ 'class' => 'AppBundle:Type', 'choice_label' => 'name', 'multiple' => false, 'expanded' => false ] ); } Afterwards you can access these fields like:
$form->get('game') // and $form->get('type') -
ignace's post in Do I understand MVC correctly now? was marked as the answer
Kinda, it would be something like:
class Quote { private $id; // .. other fields private $price; public function setId($id) { $this->id = $id; } public function getId() { return $this->id; } // get*, set* for the other fields public function setPrice($price) { $this->price = $price; } public function getPrice() { return $this->price; } }Then you would create a query and populate this object:
$sql = 'SELECT id, .., price FROM quotes'; $stmt = $this->db->prepare($sql); foreach ($stmt->fetchAll() as $row) { $quotes[] = $quote = new Quote; $quote->setId($row['id']); $quote->setPrice($row['price']); } return $quotes;At this point you can work with this object as:
$quote->getId(); $quote->getPrice();Now before you start writing all of this yourself, there are frameworks available online that do this sort of thing for you. A list can be found here:https://github.com/ziadoz/awesome-php#database