123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace Gedmo\Sortable\Mapping\Driver;
- use Gedmo\Mapping\Driver\Xml as BaseXml,
- Gedmo\Exception\InvalidMappingException;
- /**
- * This is a xml mapping driver for Sortable
- * behavioral extension. Used for extraction of extended
- * metadata from xml specificaly for Sortable
- * extension.
- *
- * @author Lukas Botsch <lukas.botsch@gmail.com>
- * @package Gedmo.Sortable.Mapping.Driver
- * @subpackage Xml
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class Xml extends BaseXml
- {
- /**
- * List of types which are valid for position field
- *
- * @var array
- */
- private $validTypes = array(
- 'integer',
- 'smallint',
- 'bigint'
- );
- /**
- * {@inheritDoc}
- */
- public function readExtendedMetadata($meta, array &$config)
- {
- /**
- * @var \SimpleXmlElement $xml
- */
- $xml = $this->_getMapping($meta->name);
- if (isset($xml->field)) {
- foreach ($xml->field as $mapping) {
- $mappingDoctrine = $mapping;
- /**
- * @var \SimpleXmlElement $mapping
- */
- $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
- $field = $this->_getAttribute($mappingDoctrine, 'name');
- if (isset($mapping->{'sortable-position'})) {
- if (!$this->isValidField($meta, $field)) {
- throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
- }
- $config['position'] = $field;
- }
- }
- $this->readSortableGroups($xml->field, $config, 'name');
- }
- // Search for sortable-groups in association mappings
- if (isset($xml->{'many-to-one'})) {
- $this->readSortableGroups($xml->{'many-to-one'}, $config);
- }
- // Search for sortable-groups in association mappings
- if (isset($xml->{'many-to-many'})) {
- $this->readSortableGroups($xml->{'many-to-many'}, $config);
- }
- if (!$meta->isMappedSuperclass && $config) {
- if (!isset($config['position'])) {
- throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
- }
- }
- }
- private function readSortableGroups($mapping, array &$config, $fieldAttr='field')
- {
- foreach ($mapping as $map) {
- $mappingDoctrine = $map;
- /**
- * @var \SimpleXmlElement $mapping
- */
- $map = $map->children(self::GEDMO_NAMESPACE_URI);
- $field = $this->_getAttribute($mappingDoctrine, $fieldAttr);
- if (isset($map->{'sortable-group'})) {
- if (!isset($config['groups'])) {
- $config['groups'] = array();
- }
- $config['groups'][] = $field;
- }
- }
- }
- /**
- * Checks if $field type is valid as Sortable Position field
- *
- * @param object $meta
- * @param string $field
- * @return boolean
- */
- protected function isValidField($meta, $field)
- {
- $mapping = $meta->getFieldMapping($field);
- return $mapping && in_array($mapping['type'], $this->validTypes);
- }
- }
|