Christopher Pitt
Writer and coder, working at ringier.co.za
Pushing a large rock up a steep hill...
$client = new Acme\Client(new GuzzleHttp\Client());
try {
$response = $client->fetchItems([
"limit" => 25,
"orderBy" => ["created", "desc"],
"filter" => [
"owner" => "Chris",
],
]);
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client(new GuzzleHttp\Client());
try {
$response = $client->fetchItems([
"limit" => 25,
"orderBy" => ["created", "desc"],
"filter" => [
"owner" => "Chris",
],
]);
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client(new GuzzleHttp\Client());
try {
$response = $client->fetchItems([
"limit" => 25,
"orderBy" => ["created", "desc"],
"filter" => [
"owner" => "Chris",
],
]);
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
namespace Acme;
use GuzzleHttp\Client as GuzzleClient;
class Client {
private $client;
public function __construct($client = null) {
if (is_null($client)) {
$client = new GuzzleClient();
}
$this->client = $client;
}
// ...snip
}
<?php
$client = new Acme\Client(new GuzzleHttp\Client());
try {
$response = $client->fetchItems([
"limit" => 25,
"orderBy" => ["created", "desc"],
"filter" => [
"owner" => "Chris",
],
]);
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
namespace Acme;
use GuzzleHttp\Client as GuzzleClient;
use Amp\Artax\DefaultClient as ArtaxClient;
class Client {
private $client;
public function __construct($client = null) {
if (is_null($client)) {
if (class_exists(GuzzleClient::class)) {
$client = new GuzzleClient();
}
if (class_exists(ArtaxClient::class)) {
$client = new ArtaxClient();
}
}
$this->client = $client;
}
// ...snip
}
$client = new Acme\Client();
try {
$response = $client->fetchItems([
"limit" => 25,
"orderBy" => ["created", "desc"],
"filter" => [
"owner" => "Chris",
],
]);
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client();
try {
$response = $client->fetchItems([
"limit" => 25,
"orderBy" => ["created", "desc"],
"filter" => [
"owner" => "Chris",
],
]);
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client();
try {
$response = $client->fetchItems([
"limit" => 25,
"orderBy" => ["created", "desc"],
"filter" => [
"owner" => "Chris",
],
]);
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
namespace Acme;
class Client {
protected $query = [];
public function limit(integer $limit): self {
$this->query["limit"] = $limit;
return $this;
}
public function orderBy(string $column, string $direction = "desc"): self {
$this->query["orderBy"] = [$column, $direction];
return $this;
}
public function filter(array $filters): self {
$this->query["filter"] = $filters;
return $this;
}
public function fetch() {
return $this->fetchItems($this->query);
}
// ...snip
}
$client = new Acme\Client();
try {
$response = $client
->limit(25)
->orderBy("created")
->filter([
"owner" => "Chris",
])
->fetch();
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client();
try {
$response = $client
->limit(25)
->orderBy("created")
->filter([
"owner" => "Chris",
])
->fetch();
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client();
try {
$response = $client
->limit(25)
->orderBy("created")
->filter([
"owner" => "Chris",
])
->fetch();
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
namespace Acme;
use Acme\Client\Response;
class Client {
public function fetchAll() {
$response = $this->internalFetchAll();
return new Response(
$response["status"] === 200,
$response["items"],
);
}
// ...snip
}
$client = new Acme\Client();
try {
$response = $client
->limit(25)
->orderBy("created")
->filter([
"owner" => "Chris",
])
->fetch();
if ($response->isOk() && $response->hasItems()) {
// render the items...
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client();
try {
$response = $client
->limit(25)
->orderBy("created")
->filter([
"owner" => "Chris",
])
->fetch();
if ($response->isOk() && $response->hasItems()) {
// render the items...
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client();
try {
$response = $client
->limit(25)
->orderBy("created")
->filter([
"owner" => "Chris",
])
->fetch();
if ($response->isOk() && $response->hasItems()) {
// render the items...
}
} catch (Acme\Client\RequestException $e) {
// handle this generic exception...
}
$client = new Acme\Client(new GuzzleHttp\Client());
try {
$response = $client->fetchItems([
"limit" => 25,
"orderBy" => ["created", "desc"],
"filter" => [
"owner" => "Chris",
],
]);
if ($response["status"] === 200) {
if (count($response["items"]) > 0) {
// render the items...
}
}
} catch (GuzzleHttp\Exception\RequestException $e) {
// handle this guzzle exception...
}
$client = new Acme\Client();
try {
$response = $client
->limit(25)
->orderBy("created")
->filter([
"owner" => "Chris",
])
->fetch();
if ($response->isOk() && $response->hasItems()) {
// render the items...
}
} catch (Acme\Client\RequestException $e) {
// handle this generic exception...
}
twitter.com/assertchris
By Christopher Pitt
Maintaining open source is a bit like being a monk. You give up your time and effort for "the greater good", and often have to deal with rudeness and entitlement along the way. It doesn't have to be like that. I want to talk to you about the how and why of creating open source code. Together, we'll look at what a good open source library looks like, and how it can pay for itself.