Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP] add startWith method for util #23

Merged
merged 1 commit into from Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion util/main.tea
Expand Up @@ -30,4 +30,4 @@ static function serialize(events: any): any;
* @param prefix the prefix string
* @return the result
*/
static function startWith(origin: string, prefix: string): bool;
static function startWith(origin: string, prefix: string): boolean;
26 changes: 20 additions & 6 deletions util/php/src/Client.php
Expand Up @@ -20,18 +20,18 @@ class Client
public static function getStringToSign($request)
{
$pathname = $request->pathname ? $request->pathname : '';
$query = $request->query ? $request->query : [];
$query = $request->query ? $request->query : [];

$contentMD5 = isset($request->headers['content-md5']) ? $request->headers['content-md5'] : '';
$contentMD5 = isset($request->headers['content-md5']) ? $request->headers['content-md5'] : '';
$contentType = isset($request->headers['content-type']) ? $request->headers['content-type'] : '';
$date = isset($request->headers['date']) ? $request->headers['date'] : '';
$date = isset($request->headers['date']) ? $request->headers['date'] : '';

$result = $request->method . "\n" .
$contentMD5 . "\n" .
$contentType . "\n" .
$date . "\n";

$canonicalizedHeaders = self::getCanonicalizedHeaders($request->headers);
$canonicalizedHeaders = self::getCanonicalizedHeaders($request->headers);
$canonicalizedResource = self::getCanonicalizedResource($pathname, $query);

return $result . $canonicalizedHeaders . $canonicalizedResource;
Expand All @@ -41,7 +41,7 @@ public static function getStringToSign($request)
* Get signature according to stringToSign, secret.
*
* @param string $stringToSign the signed string
* @param string $secret accesskey secret
* @param string $secret accesskey secret
*
* @return string the signature
*/
Expand All @@ -63,7 +63,7 @@ public static function serialize($events)
$events = json_decode(json_encode($events), true);
}
foreach ($events as $k => &$v) {
if (is_object($v) && $v instanceof Model) {
if (\is_object($v) && $v instanceof Model) {
$v = $v->toMap();
}
if (\is_array($v)) {
Expand All @@ -82,6 +82,7 @@ public static function serialize($events)
}
}
}

return $events;
}

Expand Down Expand Up @@ -114,4 +115,17 @@ public static function getCanonicalizedResource($pathname, $query)

return $pathname . '?' . implode('&', $tmp);
}

/**
* Judge if the origin is start with the prefix.
*
* @param string $origin the original string
* @param string $prefix the prefix string
*
* @return bool the result
*/
public static function startWith($origin, $prefix)
{
return 0 === strpos($origin, $prefix);
}
}
8 changes: 7 additions & 1 deletion util/php/tests/ClientTest.php
Expand Up @@ -33,7 +33,7 @@ public function testGetStringToSign()
'content-md5' => 'md5',
'content-type' => 'application/json',
'date' => 'date',
'x-acs-custom-key' => 'any value',
'x-acs-custom-key' => 'any value',
];
$this->assertEquals("GET\nmd5\napplication/json\ndate\nx-acs-custom-key:any value\n/", Client::getStringToSign($request));

Expand All @@ -59,4 +59,10 @@ public function testSerialize()
['datacontenttype'=> 'text/json', 'foo'=>'bar'],
], $result);
}

public function testStartWith()
{
$this->assertTrue(Client::startWith('x-test-string', 'x-test'));
$this->assertFalse(Client::startWith('x-test-string', 'test-string'));
}
}