Skip to content

Commit

Permalink
ResourceOwnerでより多くの情報を取れるように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
polidog committed Dec 5, 2016
1 parent f49ff10 commit 3152ea7
Showing 2 changed files with 57 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/YConnectResourceOwner.php
Original file line number Diff line number Diff line change
@@ -26,27 +26,47 @@ public function __construct(array $response = [])
$this->response = $response;
}

public function __call($name, $arguments)
{
$get = substr($name,0, 3);
if ($get !== 'get') {
return null;
}
$parameter = function($name) {
return ltrim(strtolower(preg_replace('/[A-Z]/', '_\0', str_replace("get", "", $name))), '_');
};
return $this->getResource($parameter($name));
}


public function getId()
{
return $this->response['user_id'] ?: null;
return $this->getResource('user_id');
}

public function getName()
{
return $this->response['name'] ?: null;
return $this->getResource('name');
}

public function getEmail()
{
return $this->response['email'] ?: null;
return $this->getResource('email');
}


public function getAddress()
{
return $this->getResource('address');
}

public function toArray()
{
return $this->response;
}

protected function getResource($name)
{
return isset($this->response[$name]) ? $this->response[$name] : null;
}

}
33 changes: 33 additions & 0 deletions test/src/YConnectResourceOwnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Created by PhpStorm.
* User: polidog
* Date: 2016/12/06
*/

namespace Tavii\OAuth2\Client\Provider\Test;


use Tavii\OAuth2\Client\Provider\YConnectResourceOwner;

class YConnectResourceOwnerTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function notExistGetterMethodCalled()
{
$resource = new YConnectResourceOwner(['given_name' => 'polidog']);
$actual = $resource->getGivenName();
$this->assertEquals('polidog', $actual);
}

/**
* @test
*/
public function notExistUserId()
{
$resource = new YConnectResourceOwner([]);
$this->assertNull($resource->getId());
}
}

0 comments on commit 3152ea7

Please sign in to comment.