Skip to content

Commit

Permalink
Merge pull request #2 from SocialConnect/support_user_entities
Browse files Browse the repository at this point in the history
Support user entities
  • Loading branch information
ovr committed Nov 3, 2014
2 parents 789f373 + 0985e8d commit bc6cf21
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ if ($result) {
}
```

## Custom entities

```php
class MyUserEntitiy extends \SocialConnect\Vk\Entity\User {
public function myOwnMethod()
{
//do something
}
}

$vkService->getEntityUser(new MyUserEntitiy());
$user = $vkService->getUser(1);

if ($user) {
$user->myOwnMethod();
}
```

License
-------

Expand Down
86 changes: 82 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ class Client extends \SocialConnect\Common\ClientAbstract

const VK_API_VERSION = 5.24;

/**
* @var Entity\User
*/
protected $entityUser;

/**
* @var Entity\Audio
*/
protected $entityAudio;

/**
* @var Entity\Friend
*/
protected $entityFriend;

/**
* {@inheritdoc}
*/
public function __construct($appId, $appSecret, $accessToken = null)
{
parent::__construct($appId, $appSecret, $accessToken);

/**
* Init base entities
*/
$this->entityUser = new Entity\User();
$this->entityAudio = new Entity\Audio();
$this->entityFriend = new Entity\Friend();
}

/**
* @var array
*/
Expand Down Expand Up @@ -129,7 +159,7 @@ public function getUser($id, array $fields = array('id', 'first_name', 'last_nam
if ($result) {
$result = $result[0];

return $this->getHydrator(new Entity\User())->hydrate($result);
return $this->getHydrator(clone $this->entityUser)->hydrate($result);
}

return false;
Expand Down Expand Up @@ -162,7 +192,7 @@ public function getUsers(array $ids, array $fields = array('id', 'first_name', '

if ($result) {
return new Response\Collection(
$this->hydrateCollection($result, $this->getHydrator(new Entity\User())),
$this->hydrateCollection($result, $this->getHydrator(clone $this->entityUser)),
count($result),
function() {}
);
Expand Down Expand Up @@ -202,7 +232,7 @@ public function getFriends($id = null, array $fields = array('first_name', 'last

if ($result) {
return new Response\Collection(
$this->hydrateCollection($result->items, $this->getHydrator(new Entity\Friend())),
$this->hydrateCollection($result->items, $this->getHydrator(clone $this->entityFriend)),
$result->count,
function() {}
);
Expand Down Expand Up @@ -292,12 +322,60 @@ public function getAudio($ownerId)

if ($result) {
return new Response\Collection(
$this->hydrateCollection($result->items, $this->getHydrator(new Entity\Audio())),
$this->hydrateCollection($result->items, $this->getHydrator(clone $this->entityAudio)),
$result->count,
function() {}
);
}

return false;
}

/**
* @return Entity\User
*/
public function getEntityUser()
{
return $this->entityUser;
}

/**
* @param Entity\User $entityUser
*/
public function setEntityUser(Entity\User $entityUser)
{
$this->entityUser = $entityUser;
}

/**
* @return Entity\Audio
*/
public function getEntityAudio()
{
return $this->entityAudio;
}

/**
* @param Entity\Audio $entityAudio
*/
public function setEntityAudio(Entity\Audio $entityAudio)
{
$this->entityAudio = $entityAudio;
}

/**
* @return Entity\Friend
*/
public function getEntityFriend()
{
return $this->entityFriend;
}

/**
* @param Entity\Friend $entityFriend
*/
public function setEntityFriend(Entity\Friend $entityFriend)
{
$this->entityFriend = $entityFriend;
}
}

0 comments on commit bc6cf21

Please sign in to comment.