Skip to content

Commit ad6da95

Browse files
committed
with with* method: withName
1 parent fba32f8 commit ad6da95

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
use ApiClients\Client\Twitter\AsyncClient;
4+
use ApiClients\Client\Twitter\Resource\ProfileInterface;
5+
use React\EventLoop\Factory;
6+
use function ApiClients\Foundation\resource_pretty_print;
7+
8+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
9+
$config = require 'resolve_config.php';
10+
11+
if (!isset($argv[1])) {
12+
echo 'This example requires you to pass a username for example \'php profile-update-name-async.php "Cees-Jan %s Kiewiet"\'.', PHP_EOL;
13+
echo 'The %s in there will be replaced with a random emoji.', PHP_EOL;
14+
exit(255);
15+
}
16+
17+
$loop = Factory::create();
18+
$client = (new AsyncClient(
19+
$config['consumer']['key'],
20+
$config['consumer']['secret'],
21+
$loop
22+
))->withAccessToken(
23+
$config['access_token']['token'],
24+
$config['access_token']['secret']
25+
)->profile()->done(function (ProfileInterface $profile) use ($argv) {
26+
resource_pretty_print($profile);
27+
$emojis = [
28+
'😈 ',
29+
'👾 ',
30+
'🤖 ',
31+
'🦄 ',
32+
'🐯 ',
33+
'🦁 ',
34+
'🐆 ',
35+
'🐅 ',
36+
'🐃 ',
37+
'🐦 ',
38+
'🦎 ',
39+
'🐲 ',
40+
'🐉 ',
41+
'🦋 ',
42+
'🐞 ',
43+
'🕷 ',
44+
'🕸 ',
45+
'🍀 ',
46+
'🍔 ',
47+
'🥞 ',
48+
'🌭 ',
49+
'🍕 ',
50+
'🍺 ',
51+
'🍻 ',
52+
'🥃',
53+
'🌍',
54+
'🌎',
55+
'🌏',
56+
'🌐',
57+
'🗺',
58+
'🏔',
59+
'',
60+
'🌋',
61+
'',
62+
'🌪',
63+
'🌀',
64+
'🌈',
65+
'',
66+
'',
67+
'',
68+
'🔥',
69+
'🎃',
70+
'🎮',
71+
'🔊',
72+
'🎵',
73+
'🎶',
74+
];
75+
$profile = $profile->withName(sprintf(
76+
$argv[1],
77+
$emojis[random_int(0, count($emojis) - 1)]
78+
));
79+
resource_pretty_print($profile);
80+
});
81+
82+
$loop->run();

src/Resource/EmptyProfile.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@ public function description() : string
6161
{
6262
return null;
6363
}
64+
65+
66+
/**
67+
* @param string $name
68+
* @return ProfileInterface
69+
*/
70+
public function withName(string $name): ProfileInterface
71+
{
72+
return clone $this;
73+
}
6474
}

src/Resource/Profile.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,15 @@ public function description() : string
100100
{
101101
return $this->description;
102102
}
103+
104+
/**
105+
* @param string $name
106+
* @return ProfileInterface
107+
*/
108+
public function withName(string $name): ProfileInterface
109+
{
110+
$clone = clone $this;
111+
$clone->name = $name;
112+
return $clone;
113+
}
103114
}

src/Resource/ProfileInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ public function profileLocation() : string;
4242
* @return string
4343
*/
4444
public function description() : string;
45+
46+
/**
47+
* @param string $name
48+
* @return static
49+
*/
50+
public function withName(string $name);
4551
}

0 commit comments

Comments
 (0)