Skip to content

Instantly share code, notes, and snippets.

@fabiopaiva
Last active September 11, 2015 18:01
Show Gist options
  • Save fabiopaiva/9a4d14eda1968cc48b1f to your computer and use it in GitHub Desktop.
Save fabiopaiva/9a4d14eda1968cc48b1f to your computer and use it in GitHub Desktop.
Apigility + Doctrine + ManyToMany
{
"scopes": [1, 2, 4, 5, 6, 7]
}
<?php
return array(
'service_manager' => array(
'invokables' => array(
'mynamespace.v1.extract.scopes' => 'MyNamespace\\V1\\Rest\\OauthScopes\\OauthScopesStrategy',
),
),
'doctrine-hydrator' => array(
'MyNamespace\\V1\\Rest\\Usuario\\UsuarioHydrator' => array(
'entity_class' => 'MyNamespace\\V1\\Entity\\Usuario',
'object_manager' => 'doctrine.entitymanager.orm_default',
'by_value' => true,
'strategies' => array(
'scopes' => 'mynamespace.v1.extract.scopes',
),
'use_generated_hydrator' => true,
),
)
);
<?php
namespace MyNamespace\V1\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* OauthScopes
*
* @ORM\Table(name="oauth_scopes")
* @ORM\Entity
*/
class OauthScopes
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="scope", type="string", length=2000, nullable=false)
*/
protected $scope;
/**
* @var string
*
* @ORM\Column(name="client_id", type="string", length=80, nullable=false)
*/
protected $clientId;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255, nullable=false)
*/
protected $type = 'supported';
/**
* @var integer
*
* @ORM\Column(name="is_default", type="smallint", nullable=true)
*/
protected $isDefault;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getScope()
{
return $this->scope;
}
public function getClientId()
{
return $this->clientId;
}
public function getType()
{
return $this->type;
}
public function getIsDefault()
{
return $this->isDefault;
}
public function setScope($scope)
{
$this->scope = $scope;
return $this;
}
public function setClientId($clientId)
{
$this->clientId = $clientId;
return $this;
}
public function setType($type)
{
$this->type = $type;
return $this;
}
public function setIsDefault($isDefault)
{
$this->isDefault = $isDefault;
return $this;
}
}
<?php
namespace MyNamespace\V1\Rest\OauthScopes;
use DoctrineModule\Stdlib\Hydrator\Strategy\AbstractCollectionStrategy;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
class OauthScopesStrategy extends AbstractCollectionStrategy implements ObjectManagerAwareInterface
{
/**
*
* @var \Doctrine\ORM\EntityManager
*/
protected $objectManager;
public function extract($value)
{
$hydrator = new DoctrineObject($this->getObjectManager());
$results = [];
if (is_array($value)) {
foreach ($value as $item) {
$results[] = $hydrator->extract($item);
}
}
return $results;
}
public function hydrate($value)
{
$object = $this->getObject();
if(method_exists($object, 'setScopes')){
$object->setScopes($value);
}
return $value;
}
public function getObjectManager()
{
return $this->objectManager;
}
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
return $this;
}
}
<?php
namespace MyNamespace\V1\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
*/
class Usuario
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="OauthScopes")
* @var OauthScopes
*/
protected $scopes;
public function __construct()
{
$this->scopes = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function addScope(OauthScopes $scope)
{
$this->scopes[] = $scope;
return $this;
}
/**
*
* @return OauthScopes
*/
public function getScopes()
{
return $this->scopes->getValues();
}
public function setScopes($scopes)
{
$this->scopes = $scopes;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment