Created
May 21, 2013 19:36
-
-
Save VanTanev/5622561 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DG\CommonBundle\Model; | |
/** | |
* QueryFactory to satisfy PHPSpec | |
* | |
* @author Ivan Plamenov Tanev aka CraftyShadow <[email protected]> | |
*/ | |
class QueryFactory | |
{ | |
protected $ns_prefix; | |
public function setNamespacePrefix($ns_prefix) | |
{ | |
$this->ns_prefix = $ns_prefix; | |
} | |
/** | |
* @param string $name | |
* @param string $modelAlias | |
* @param Criteria $criteria | |
* | |
* @return ModelCriteria | |
*/ | |
public function create($name, $modelAlias = null, $criteria = null) | |
{ | |
if (false === strpos($name, '\\')) { | |
$name = $this->ns_prefix . '\\' . $name; | |
} | |
return call_user_func(array($name, 'create'), $modelAlias, $criteria); | |
} | |
public function __call($name, $arguments) | |
{ | |
if (0 === strpos($name, 'create')) { | |
return $this->create( | |
substr($name, 6), | |
isset($arguments[0]) ? $arguments[0] : null, | |
isset($arguments[1]) ? $arguments[1] : null | |
); | |
} | |
throw new \Exception('Call to undefined method: ' . $name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use DG\GradiniBundle\Model\Webservice\QueryFactory; | |
class UsesQueries | |
{ | |
/** @var QueryFactory **/ | |
protected $qf; | |
public function __construct(QueryFactory $qf) | |
{ | |
$this->qf = $qf; | |
} | |
public function doSomething() | |
{ | |
//$q is the proper type and has autocompletion for all methods of RegionQuery | |
$q = $this->qf->createRegionQuery(); | |
$q->find(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace spec\DG\GradiniBundle\Model\Webservice; | |
use PhpSpec\ObjectBehavior; | |
use Prophecy\Argument; | |
class QueryFactorySpec extends ObjectBehavior | |
{ | |
/** | |
* @param DG\GradiniBundle\Model\Webservice\QueryFactory $qf; | |
*/ | |
public function let($qf) | |
{ | |
$this->beConstructedWith($qf) | |
} | |
/** | |
* @param DG\GradiniBundle\Model\Webservice\QueryFactory $qf; | |
* @param DG\GradiniBundle\Model\Webservice\RegionQuery $q; | |
*/ | |
public function it_tests_do_something($qf, $q) | |
{ | |
$q->find()->willReturn(array('whatever')); | |
$qf->getRegionQuery()->willReturn($q); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DG\GradiniBundle\Model\Webservice; | |
use DG\CommonBundle\Model\QueryFactory as BaseQueryFactory; | |
/** | |
* QueryFactory for the GradiniBundle Webservice Model | |
* | |
* @author Ivan Plamenov Tanev aka CraftyShadow <[email protected]> | |
*/ | |
class QueryFactory extends BaseQueryFactory | |
{ | |
protected $ns_prefix = 'DG\\GradiniBundle\\Model\\Webservice'; | |
/** | |
* @param string $modelAlias | |
* @param \Criteria|\ModelCriteria $criteria | |
* @return RegionQuery | |
*/ | |
public function createRegionQuery($modelAlias = null, $criteria = null) | |
{ | |
return $this->create('RegionQuery', $modelAlias, $criteria); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment