Reputation: 3657
I am right now working with the Doctrine 2 stuff and landed upon the Doctrine modules for validations like ObjectExists.php
and NoObjectExists.php
.
My question is that from the original code which can be found here.
/**
* Constructor
*
* @param array $options required keys are `object_repository`, which must be an instance of
* Doctrine\Common\Persistence\ObjectRepository, and `fields`, with either
* a string or an array of strings representing the fields to be matched by the validator.
* @throws \Zend\Validator\Exception\InvalidArgumentException
*/
public function __construct(array $options)
{
if (!isset($options['object_repository']) || !$options['object_repository'] instanceof ObjectRepository) {
if (!array_key_exists('object_repository', $options)) {
$provided = 'nothing';
} else {
if (is_object($options['object_repository'])) {
$provided = get_class($options['object_repository']);
} else {
$provided = getType($options['object_repository']);
}
}
throw new Exception\InvalidArgumentException(sprintf(
'Option "object_repository" is required and must be an instance of'
. ' Doctrine\Common\Persistence\ObjectRepository, %s given',
$provided
));
}
$this->objectRepository = $options['object_repository'];
if (!isset($options['fields'])) {
throw new Exception\InvalidArgumentException(
'Key `fields` must be provided and be a field or a list of fields to be used when searching for'
. ' existing instances'
);
}
$this->fields = $options['fields'];
$this->validateFields();
parent::__construct($options);
}
I cannot get the fact that here it is mentioned "$options
required keys are object_repository
, which must be an instance of Doctrine\Common\Persistence\ObjectRepository
"
Since Doctrine\Common\Persistence\ObjectRepository
is an interface, how should I decode that statement?
Or in other words how can I call this constructor of ObjectsExists
class and pass object_repository
, which must be an instance of Doctrine\Common\Persistence\ObjectRepository
?
Can somebody throw some light on this, I'm getting into this stuff, so do not be harsh on my question.
Thanks
Upvotes: 2
Views: 422
Reputation: 62914
The most common class that implements Doctrine\Common\Persistence\ObjectRepository is Doctrine\ORM\EntityRepository.
So, if you're using Doctrine ORM, the object_repository option should be something like your UserRepository (either explicitly defined, or just the generic one you get back from $em->getRepository('User')
)
The reason it's not explicitly defined is because [No]ObjectExists validators aren't in the DoctrineORMModule, but just the DoctrineModule. So if you're not using ORM, you just need to create your own UserRepository class that implements ObjectRepository.
Upvotes: 0
Reputation: 12420
Since Doctrine\Common\Persistence\ObjectRepository
is an interface you cannot instantiate it but you can implement it.
class MyObjectRepository implements Doctrine\Common\Persistence\ObjectRepository
{
// ...
}
Now every instance of MyObjectRepository
will met the object_repository
requirement of ObjectsExists
.
new ObjectsExists(array(
'object_repository' => new MyObjectRepository(),
// ...
));
More about instanceof
:
instanceof
is used to determine whether a PHP variable is an instantiated object of a certain class.
instanceof
can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class.
instanceof
can also be used to determine whether a variable is an instantiated object of a class that implements an interface.
Upvotes: 1