Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Refactor fieldMaps out of config #18

Merged
merged 1 commit into from

1 participant

@jeremiahsmall

There seemed to be no advantage to injecting the maps, as opposed to defining the maps directly in the entity. Provide abstract methods on AbstractEntity for fieldMaps. Update Version due to BC break.

@jeremiahsmall jeremiahsmall Refactor fieldMaps out of config because there seemed to be no advant…
…age to injecting the maps, as opposed to defining the maps directly in the entity. Provide abstract methods on AbstractEntity for fieldMaps. Update Version due to BC break.
641d2f3
@jeremiahsmall jeremiahsmall merged commit 660a9bf into soliantconsulting:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2013
  1. @jeremiahsmall

    Refactor fieldMaps out of config because there seemed to be no advant…

    jeremiahsmall authored
    …age to injecting the maps, as opposed to defining the maps directly in the entity. Provide abstract methods on AbstractEntity for fieldMaps. Update Version due to BC break.
This page is out of date. Refresh to see the latest.
View
2  library/Soliant/SimpleFM/Version.php
@@ -11,5 +11,5 @@
final class Version
{
- const VERSION = '2.0.0beta6';
+ const VERSION = '2.0.0beta7';
}
View
72 library/Soliant/SimpleFM/ZF2/Entity/AbstractEntity.php
@@ -31,11 +31,6 @@
/**
* @var array
*/
- protected $fieldMap;
-
- /**
- * @var array
- */
protected $entityAsArray;
/**
@@ -48,27 +43,9 @@
/**
* @param array $simpleFMAdapterRow
*/
- public function __construct($fieldMap, $simpleFMAdapterRow = array())
+ public function __construct($simpleFMAdapterRow = array())
{
$this->simpleFMAdapterRow = $simpleFMAdapterRow;
-
- if (empty($fieldMap)){
- throw new InvalidArgumentException(get_class($this) . ' is empty or missing.');
- }
-
- if (!array_key_exists(get_class($this), $fieldMap)){
- throw new InvalidArgumentException(get_class($this) . ' is missing from $fieldMap.');
- }
-
- $this->fieldMap = $fieldMap;
- if (!array_key_exists('writeable', $this->fieldMap[get_class($this)])){
- throw new InvalidArgumentException(get_class($this) . ' fieldMap must contain a "writeable" array.');
- }
-
- if (!array_key_exists('readonly', $this->fieldMap[get_class($this)])){
- throw new InvalidArgumentException(get_class($this) . ' fieldMap must contain a "readonly" array.');
- }
-
$this->isSerializable = TRUE;
if (!empty($this->simpleFMAdapterRow)) $this->unserialize();
}
@@ -142,7 +119,35 @@ public function setIsSerializable ($isSerializable)
}
/**
- * Default FileMaker layout for the Entity which should include all the writable fields
+ * An array of properties and FileMaker field names each maps to which are writeable/serializable .
+ * List fields in this map which the web app can modify, such as text and number fields. Normally
+ * it will be most convenient to define the array directly in the method implementation.
+ * Example: return array ('myEntityName' => 'My Entity Name', 'status' => 'SomeTableOccurance::Status');
+ * @return array
+ */
+ abstract public function getFieldMapWriteable();
+
+ /**
+ * An array of properties and FileMaker field names each maps to which are readonly. List fields
+ * in this map which cannot be updated by the web app, such a s primary keys and calc fields.
+ * Normally it will be most convenient to define the array directly in the method implementation.
+ * Example: return array ('id' => 'PrimaryKey', 'total' => 'Total');
+ * @return array
+ */
+ abstract public function getFieldMapReadonly();
+
+ /**
+ * Utility function combines both field maps into a single map.
+ * @return array
+ */
+ public function getFieldMapMerged()
+ {
+ return array_merge($this->getFieldMapWriteable(), $this->getFieldMapReadOnly());
+ }
+
+ /**
+ * Default FileMaker layout for the Entity. This layout should usually at least include all the
+ * writable fields, but it may also include readonly fields and portals/associations.
*/
abstract public function getDefaultWriteLayoutName();
@@ -161,11 +166,11 @@ public function unserialize()
$this->unserializeField('recid', 'recid');
$this->unserializeField('modid', 'modid');
- foreach ($this->fieldMap[get_class($this)]['writeable'] as $property=>$field) {
+ foreach ($this->getFieldMapWriteable() as $property=>$field) {
$this->unserializeField($property, $field, true);
}
- foreach ($this->fieldMap[get_class($this)]['readonly'] as $property=>$field) {
+ foreach ($this->getFieldMapReadOnly() as $property=>$field) {
$this->unserializeField($property, $field, false);
}
}
@@ -182,14 +187,9 @@ public function serialize()
$this->serializeField('-recid', 'getRecid');
$this->serializeField('-modid', 'getModid');
- foreach ($this->fieldMap[get_class($this)]['writeable'] as $property=>$field) {
+ foreach ($this->getFieldMapMerged() as $property=>$field) {
$this->serializeField($field, $property);
}
-
- foreach ($this->fieldMap[get_class($this)]['readonly'] as $property=>$field) {
- $this->serializeField($field, $property);
- }
-
}
/**
@@ -200,11 +200,7 @@ public function toArray() {
$this->addPropertyToEntityAsArray('recid');
$this->addPropertyToEntityAsArray('modid');
- foreach ($this->fieldMap[get_class($this)]['writeable'] as $property=>$field) {
- $this->addPropertyToEntityAsArray($property);
- }
-
- foreach ($this->fieldMap[get_class($this)]['readonly'] as $property=>$field) {
+ foreach ($this->getFieldMapMerged() as $property=>$field) {
$this->addPropertyToEntityAsArray($property);
}
View
49 library/Soliant/SimpleFM/ZF2/Gateway/AbstractGateway.php
@@ -36,11 +36,6 @@
protected $entityLayout;
/**
- * @var array
- */
- protected $fieldMap;
-
- /**
* @var \Soliant\SimpleFM\Adapter
*/
protected $simpleFMAdapter;
@@ -50,30 +45,12 @@
* @param AbstractEntity $entity
* @param SimpleFMAdapter $simpleFMAdapter
*/
- public function __construct($fieldMap, AbstractEntity $entity, SimpleFMAdapter $simpleFMAdapter, Identity $identity=NULL, $encryptionKey=NULL )
+ public function __construct(AbstractEntity $entity, SimpleFMAdapter $simpleFMAdapter, Identity $identity=NULL, $encryptionKey=NULL )
{
- if (!is_array($fieldMap)){
- throw new InvalidArgumentException('$fieldMap must be an array.');
- }
-
$this->setSimpleFMAdapter($simpleFMAdapter);
$this->setEntityName(get_class($entity));
$this->setEntityLayout($entity->getDefaultWriteLayoutName());
- if (!array_key_exists($this->getEntityName(), $fieldMap)){
- throw new InvalidArgumentException($this->getEntityName() . ' is missing from $fieldMap.');
- }
-
- $this->fieldMap = $fieldMap;
-
- if (!array_key_exists('writeable', $this->fieldMap[$this->getEntityName()])){
- throw new InvalidArgumentException($this->getEntityName() . ' fieldMap is missing from "writeable" array.');
- }
-
- if (!array_key_exists('readonly', $this->fieldMap[$this->getEntityName()])){
- throw new InvalidArgumentException($this->getEntityName() . ' fieldMap is missing from "readonly" array.');
- }
-
if (!empty($identity) && !empty($encryptionKey)) {
$this->simpleFMAdapter->setUsername($identity->getUsername());
$this->simpleFMAdapter->setPassword($identity->getPassword($encryptionKey));
@@ -101,7 +78,7 @@ public function find($recid)
->setCommandArray($commandArray)
->setLayoutname($this->getEntityLayout());
$result = $this->handleAdapterResult($this->simpleFMAdapter->execute());
- $entity = new $this->entityName($this->fieldMap, $result['rows'][0]);
+ $entity = new $this->entityName($result['rows'][0]);
return $entity;
}
@@ -118,7 +95,7 @@ public function findOneBy(array $search)
->setCommandArray($commandArray)
->setLayoutname($this->getEntityLayout());
$result = $this->handleAdapterResult($this->simpleFMAdapter->execute());
- $entity = new $this->entityName($this->fieldMap, $result['rows'][0]);
+ $entity = new $this->entityName($result['rows'][0]);
return $entity;
}
@@ -164,7 +141,7 @@ public function create(AbstractEntity $entity)
->setCommandArray($commandArray)
->setLayoutname($this->getEntityLayout());
$result = $this->handleAdapterResult($this->simpleFMAdapter->execute());
- $entity = new $this->entityName($this->fieldMap, $result['rows'][0]);
+ $entity = new $this->entityName($result['rows'][0]);
return $entity;
}
@@ -181,7 +158,7 @@ public function edit(AbstractEntity $entity)
->setLayoutname($this->getEntityLayout());
$result = $this->handleAdapterResult($this->simpleFMAdapter->execute());
- $entity = new $this->entityName($this->fieldMap, $result['rows'][0]);
+ $entity = new $this->entityName($result['rows'][0]);
return $entity;
}
@@ -257,20 +234,6 @@ public function setEntityName ($entityName)
}
/**
- * @return the $fieldMap
- */
- public function getFieldMap() {
- return $this->fieldMap;
- }
-
- /**
- * @param multitype: $fieldMap
- */
- public function setFieldMap($fieldMap) {
- $this->fieldMap = $fieldMap;
- }
-
- /**
* @param int $max
* @param int $skip
* @return array:
@@ -346,7 +309,7 @@ protected function rowsToArrayCollection(array $rows)
$collection = new ArrayCollection();
if (!empty($rows)){
foreach($rows as $row){
- $collection[] = new $this->entityName($this->fieldMap, $row);
+ $collection[] = new $this->entityName($row);
}
}
Something went wrong with that request. Please try again.