/** * Generate php mapping class * @throw pdoMap_Parsing_Mapping_Exceptions_Field */ public function generate($file, $ns) { $this->mappingKey = $ns; $ns = ucfirst($ns); // Camelstyle /** * Defining Interface Service */ $this->open($file . '.class.php'); $this->writeLine('<?php // GEN ' . $ns . ' AT ' . date('Y-m-d H:i:s')); $this->writeLine('interface I' . $ns . 'Adapter {'); if (isset($this->adapter)) { if (isset($this->adapter->request)) { foreach ($this->adapter->request as $rq) { $rq->writeSignature(true); } } if (isset($this->adapter->transaction)) { foreach ($this->adapter->transaction as $rq) { $rq->writeSignature(true); } } } $this->writeLine('}'); /** * Defining Adapter Service */ $this->writeLine('class ' . $ns . 'AdapterImpl'); $this->writeLine('extends pdoMap_Mapping_Adapter'); $this->writeLine('implements I' . $ns . 'Adapter {'); $this->writeLine('public static $adapter = \'' . $this->mappingKey . '\';'); $this->writeLine('public function __construct() {'); $this->writeLine('parent::__construct(\'' . $this->mappingKey . '\');'); $this->writeLine('}'); if (isset($this->adapter)) { if (isset($this->adapter->request)) { foreach ($this->adapter->request as $rq) { $rq->toPhp(true); } } if (isset($this->adapter->transaction)) { foreach ($this->adapter->transaction as $rq) { $rq->toPhp(true); } } } $this->writeLine('}'); /** * Generating Item Entity Manager */ $this->writeLine('class ' . $ns . 'EntityImpl extends pdoMap_Mapping_Entity {'); $this->writeLine('public function __construct($values = null) {'); $this->writeLine('parent::__construct(\'' . $this->mappingKey . '\', $values);'); $this->writeLine('}'); foreach ($this->fields->field as $field) { if (isset($field->calculated)) { $field->calculated = str_replace('{', '$this->', $field->calculated); $field->calculated = str_replace('}', '', $field->calculated); $field->calculated = str_replace('.', '->', $field->calculated); $this->writeLine('/**'); $this->writeLine(' * Calculated field getter ' . $field->bind); $this->writeLine(' * Formula : ' . $field->calculated); $this->writeLine(' */'); $this->writeLine('public function getter' . $field->bind . '() {'); $this->writeLine('$this->setValue(\'' . $field->bind . '\', ' . $field->calculated . ');'); $this->writeLine('return $this->getValue(\'' . $field->bind . '\');'); $this->writeLine('}'); } } if (isset($this->entity)) { if (isset($this->entity->collection)) { foreach ($this->entity->collection as $rq) { $rq->toPhp(false); } } if (isset($this->entity->request)) { foreach ($this->entity->request as $rq) { $rq->toPhp(false); } } if (isset($this->entity->transaction)) { foreach ($this->entity->transaction as $rq) { $rq->toPhp(false); } } } $this->writeLine('}'); /** * Generating Structure Manager */ $this->open($file . '.structure.php'); $this->writeLine('<?php // GEN ' . $ns . ' AT ' . date('Y-m-d H:i:s')); // GET ENTITY MANAGER if (isset($this->entity) && isset($this->entity->class)) { $entity = $this->entity->class; } else { $entity = $ns . 'EntityImpl'; } // GET ADAPTER MANAGER if (isset($this->adapter) && isset($this->adapter->class)) { $adapter = ', \'' . $this->adapter->class . '\''; } else { $adapter = ', \'I' . $ns . 'Adapter\''; } // GET DATABASE MANAGER if ($this->use) { $db = ', \'' . $this->use . '\''; } else { $db = ''; } // START CONSTRUCT $this->writeLine('$return = new pdoMap_Mapping_Metadata_Table(\'' . $this->mappingKey . '\', \'' . $this->name . '\', \'' . $entity . '\'' . $adapter . $db . ');'); foreach ($this->fields->field as $field) { if (!isset($field->bind)) { throw new Exception('A field must contain a bind attribute !'); } if (!isset($field->type)) { throw new Exception('A field must contain a type attribute !'); } if (!isset($field->name)) { $field->name = $field->bind; } $this->writeLine('$return->fields[\'' . $field->bind . '\'] = new pdoMap_Mapping_Metadata_Field('); $this->writeData('\'' . $field->name . '\','); $this->writeLine('\'' . $field->bind . '\','); $this->writeData('\'' . ucfirst(strtolower($field->type)) . '\','); $props = $field->getProperties(); unset($props['name']); unset($props['bind']); unset($props['type']); if (isset($props['calculated'])) { unset($props['calculated']); } $this->writeData(var_export($props, true)); $this->writeLine(');'); } $this->writeLine('return $return;'); $this->close(); // TEST STRUCTURE $structure = (include $file . '.structure.php'); foreach ($structure->fields as $bind => $field) { try { pdoMap::field()->getMeta($field); // TEST FIELD META } catch (Exception $ex) { @unlink($file . '.structure.php'); @unlink($file . '.class.php'); throw new pdoMap_Parsing_Mapping_Exceptions_Field($field, $this->__file, $ex->getMessage()); } } return $structure; }
/** * Get the physical list of fields */ public function getPhysicalFields() { $ret = array(); foreach ($this->fields as $field) { try { foreach (pdoMap::field()->getMeta($field) as $meta) { $ret[] = $meta; } } catch (Exception $ex) { throw new Exception('Bad field exception on ' . $this->type . "\n" . $ex->getMessage() . "\n" . print_r($field, true)); } } return $ret; }
/** * Set a value to collection */ protected function __setter($key, $value) { if (!is_null($value)) { $value = pdoMap::field()->setter($this, $this->getStructure()->getField($key), $value); } $this->setValue($key, $value); return $value; }
/** * Validate entity values * @throw pdoMap_Mapping_Exceptions_EntityValidateIsNull * @throw pdoMap_Mapping_Exceptions_EntityValidateType */ public function Validate(pdoMap_Mapping_Entity &$entity) { $event = $this->raise('Validate', new pdoMap_Mapping_Adapter_Args($entity)); if ($event) { // AUTOMATIC FIELD VALIDATION foreach ($this->getStructure()->getFields() as $field) { // SET DEFAULT VALUE if (is_null($event->entity->getValue($field->bind)) && !is_null($field->DefaultValue())) { $event->entity->__set($field->bind, $field->DefaultValue()); } // READ VALUE try { $value = $event->entity->__get($field->bind); } catch (Exception $ex) { throw new pdoMap_Mapping_Exceptions_EntityValidateType($event->entity, $field); } // CHECK NULL CONSTRAINT if (!$field->IsNull() && is_null($value) && $field->type != 'Primary') { throw new pdoMap_Mapping_Exceptions_EntityValidateIsNull($event->entity, $field); } // VALIDATE FIELD VALUE if (!is_null($value) && !pdoMap::field()->Validate($event->entity, $field, $value)) { throw new pdoMap_Mapping_Exceptions_EntityValidateType($event->entity, $field); } } return true; } else { return false; } }
/** * Get the field manager */ public static function field() { if (!self::$field) { self::$field = new pdoMap_Database_Field(); } return self::$field; }