Skip to content

Instantly share code, notes, and snippets.

@metalvarez
Last active December 28, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metalvarez/7484157 to your computer and use it in GitHub Desktop.
Save metalvarez/7484157 to your computer and use it in GitHub Desktop.
<?php
/**
* @file Acme\CommonsBundle\Util\EntityReflectionGuesser.php
* @package AcmeCommonsBundle
*/
namespace Acme\CommonsBundle\Util;
/**
* Entity Reflection Guesser
*
* @author "Víctor Hugo Álvarez Pérez <metalvarez@gmail.com>"
* @package AcmeCommonsBundle
* @since 29/10/2013
* @version 0.1
*/
class EntityReflectionGuesser
{
/**
* @var \ReflectionClass
*/
protected $reflector;
public function inizialize(\Symfony\Bundle\FrameworkBundle\Controller\Controller $controller)
{
// clase Reflection
$this->reflector = new \ReflectionClass(get_class($controller));
return $this;
}
/**
*
* @return string
*/
public function getBundleName()
{
return ($p1 = strpos($ns = $this->getNamespace(), '\\')) === false ? $ns :
substr($ns, 0, ($p2 = strpos($ns, '\\', $p1 + 1)) === false ? strlen($ns) : $p2);
}
/**
*
* @return string
*/
public function getBundleShortName()
{
return str_replace('\\', '', $this->getBundleName());
}
/**
*
* @return string
*/
public function getNamespace() { return $this->reflector->getNamespaceName(); }
/**
*
* @return string
*/
public function getShortName() { return $this->reflector->getShortName(); }
/**
*
* @return string
*/
public function getName() { return $this->reflector->getName(); }
/**
*
* @return string
*/
public function guessEntityNamespace()
{
return ($pos = strrpos($ns = $this->getNamespace(), '\\')) === false ? $ns
: sprintf("%s\%s", substr($ns, 0, $pos), 'Entity');
}
/**
*
* @return string
*/
public function guessEntityShortName()
{
return ($pos = strpos($short = $this->getShortName(), 'Controller')) === false ? $short :
substr($short, 0, $pos);
}
/**
*
* @return string
*/
public function guessEntityName()
{
return sprintf('%s\\%s', $this->guessEntityNamespace(), $this->guessEntityShortName());
}
/**
*
* @return string
*/
public function guessEntityBundleShortName()
{
return $this->getBundleShortName().":".$this->guessEntityShortName();
}
/**
*
* @return string
*/
public function guessRepositoryNamespace()
{
return ($p = strrpos($ns = $this->getNamespace(), '\\')) === false ? $ns
: sprintf("%s\Repository", substr($ns, 0, $p));
}
/**
*
* @return string
*/
public function guessRepositoryShortName()
{
return sprintf(
'%sRepository',
($pos = strpos($s = $this->getShortName(), 'Controller')) === false ? $s : substr($s, 0, $pos)
);
}
/**
*
* @return string
*/
public function guessRepositoryName()
{
return sprintf('%s\\%s', $this->guessRepositoryNamespace(), $this->guessRepositoryShortName());
}
/**
*
* @return string
*/
public function guessFormTypeNamespace()
{
return ($p = strrpos($ns = $this->getNamespace(), '\\')) === false ? $ns
: sprintf("%s\Form\Type", substr($ns, 0, $p));
}
/**
*
* @return string
*/
public function guessFormTypeShortName()
{
return sprintf(
'%sFormType',
($pos = strpos($s = $this->getShortName(), 'Controller')) === false ? $s : substr($s, 0, $pos)
);
}
/**
*
* @return string
*/
public function guessFormTypeName()
{
return sprintf('%s\%s', $this->guessFormTypeNamespace(), $this->guessFormTypeShortName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment