/** * Transforms entities into choice keys * * @param Collection|object A collection of entities, a single entity or * NULL * @return mixed An array of choice keys, a single key or * NULL */ protected function transform($collectionOrEntity) { if (null === $collectionOrEntity) { return $this->getOption('multiple') ? array() : ''; } if (count($this->identifier) > 1) { // load all choices $availableEntities = $this->getEntities(); if ($collectionOrEntity instanceof Collection) { $result = array(); foreach ($collectionOrEntity as $entity) { // identify choices by their collection key $key = array_search($entity, $availableEntities); $result[] = $key; } } else { $result = array_search($collectionOrEntity, $availableEntities); } } else { if ($collectionOrEntity instanceof Collection) { $result = array(); foreach ($collectionOrEntity as $entity) { $result[] = current($this->getIdentifierValues($entity)); } } else { $result = current($this->getIdentifierValues($collectionOrEntity)); } } return parent::transform($result); }