Skip to content

Instantly share code, notes, and snippets.

@manuakasam
Created December 21, 2012 12:44
Show Gist options
  • Save manuakasam/4352572 to your computer and use it in GitHub Desktop.
Save manuakasam/4352572 to your computer and use it in GitHub Desktop.
<?php
namespace Haushaltportal\Form\Element;
use DoctrineModule\Form\Element\Proxy as BaseProxy;
class Proxy extends BaseProxy
{
/**
* @var string
*/
protected $labelConnector = ' - ';
/**
* @param $options Sets the options frmo an array of parameters
*/
public function setOptions($options)
{
if (isset($options['object_manager'])) {
$this->setObjectManager($options['object_manager']);
}
if (isset($options['target_class'])) {
$this->setTargetClass($options['target_class']);
}
if (isset($options['property'])) {
$this->setProperty($options['property']);
}
if (isset($options['label_connector'])) {
$this->setLabelConnector($options['label_connector']);
}
if (isset($options['find_method'])) {
$this->setFindMethod($options['find_method']);
}
if (isset($options['is_method'])) {
$this->setIsMethod($options['is_method']);
}
}
/**
* @param $labelConnector string Connects together an array of properties to display as value of the select element
*/
public function setLabelConnector($labelConnector)
{
$this->labelConnector = $labelConnector;
}
/**
* @return string Returns the Connector String to connect an array of properties for the value of the select element
*/
public function getLabelConnenctor()
{
return $this->labelConnector;
}
protected function loadValueOptions()
{
if (!($om = $this->objectManager)) {
throw new RuntimeException('No object manager was set');
}
if (!($targetClass = $this->targetClass)) {
throw new RuntimeException('No target class was set');
}
$metadata = $om->getClassMetadata($targetClass);
$identifier = $metadata->getIdentifierFieldNames();
$objects = $this->getObjects();
$options = array();
if (empty($objects)) {
$options[''] = '';
} else {
foreach ($objects as $key => $object) {
if (($property = $this->property)) {
if (is_array($property)) {
$labelParts = array();
foreach ($property as $prop) {
$labelParts[] = $this->loadObjectValueOption($object, $prop);
}
$label = implode($this->getLabelConnenctor(), $labelParts);
} elseif (is_string($property)) {
$label = $this->loadObjectValueOption($object, $property);
}
} else {
if (!is_callable(array($object, '__toString'))) {
throw new RuntimeException(sprintf(
'%s must have a "__toString()" method defined if you have not set a property or method to use.',
$targetClass
));
}
$label = (string) $object;
}
if (count($identifier) > 1) {
$value = $key;
} else {
$value = current($metadata->getIdentifierValues($object));
}
$options[] = array('label' => $label, 'value' => $value);
}
}
$this->valueOptions = $options;
}
/**
* @param $object \Doctrine\Common\Persistence\ObjectManager
* @param $property string Property to load as a label string
* @return string Label string
* @throws RuntimeException
*/
protected function loadObjectValueOption($object, $property)
{
if (!($om = $this->objectManager)) {
throw new RuntimeException('No object manager was set');
}
if (!($targetClass = $this->targetClass)) {
throw new RuntimeException('No target class was set');
}
$metadata = $om->getClassMetadata($targetClass);
if ($this->isMethod == false && !$metadata->hasField($property)) {
throw new RuntimeException(sprintf(
'Property "%s" could not be found in object "%s"',
$property,
$targetClass
));
}
$getter = 'get' . ucfirst($property);
if (!is_callable(array($object, $getter))) {
throw new RuntimeException(sprintf(
'Method "%s::%s" is not callable',
$this->targetClass,
$getter
));
}
return $object->{$getter}();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment