protected function _populateModelList(array $itemsRaw) { $modelList = CM_Model_Abstract::factoryGenericMultiple($itemsRaw, $this->_getModelType()); $this->_modelList = []; foreach ($itemsRaw as $index => $itemRaw) { $this->_modelList[serialize($itemRaw)] = $modelList[$index]; } }
/** * @param array $idTypeList [['type' => int, 'id' => int|array],...] | [int|array,...] Pass an array of ids if $modelType is used * @param int|null $modelType * @return CM_Model_Abstract[] Can contain null-entries when model doesn't exist * @throws CM_Exception_Invalid */ public static function factoryGenericMultiple(array $idTypeList, $modelType = null) { $modelType = null !== $modelType ? (int) $modelType : null; $modelList = array(); $idTypeMap = array(); $serializedKeyMap = array(); $storageTypeList = array('cache' => array(), 'persistence' => array()); $noPersistenceList = array(); foreach ($idTypeList as $originalKey => $idType) { if (null === $modelType) { if (!is_array($idType)) { throw new CM_Exception_Invalid('`idType` should be an array if `modelType` is not defined', null, ['idType' => CM_Util::var_line($idType)]); } $type = (int) $idType['type']; $id = $idType['id']; } else { $type = $modelType; $id = $idType; } if (!is_array($id)) { $id = array('id' => $id); } $id = self::_castIdRaw($id); $idType = array('type' => $type, 'id' => $id); $serializedKey = serialize($idType); $serializedKeyMap[$originalKey] = $serializedKey; $modelList[$serializedKey] = null; $idTypeMap[$serializedKey] = $idType; /** @var CM_Model_Abstract $modelClass */ $modelClass = CM_Model_Abstract::_getClassName($type); if ($cacheStorageClass = $modelClass::getCacheClass()) { $storageTypeList['cache'][$cacheStorageClass][$serializedKey] = $idType; } if ($persistenceStorageClass = $modelClass::getPersistenceClass()) { $storageTypeList['persistence'][$persistenceStorageClass][$serializedKey] = $idType; } else { $noPersistenceList[$serializedKey] = $idType; } } foreach ($storageTypeList as $storageType => $adapterTypeList) { $searchItemList = array_filter($modelList, function ($value) { return null === $value; }); foreach ($adapterTypeList as $adapterClass => $adapterItemList) { /** @var CM_Model_StorageAdapter_AbstractAdapter $storageAdapter */ $storageAdapter = new $adapterClass(); $result = $storageAdapter->loadMultiple(array_intersect_key($adapterItemList, $searchItemList)); foreach ($result as $serializedKey => $modelData) { $model = null; if (null !== $modelData) { $dataFromPersistence = 'persistence' === $storageType; $model = self::factoryGeneric($idTypeMap[$serializedKey]['type'], $idTypeMap[$serializedKey]['id'], $modelData, $dataFromPersistence); } $modelList[$serializedKey] = $model; } } } // no persistence foreach ($noPersistenceList as $serializedKey => $idType) { if (!isset($modelList[$serializedKey])) { try { $model = self::factoryGeneric($idType['type'], $idType['id']); } catch (CM_Exception_Nonexistent $ex) { $model = null; } $modelList[$serializedKey] = $model; } } $resultList = array(); foreach ($serializedKeyMap as $originalKey => $serializedKey) { $resultList[] = $modelList[$serializedKeyMap[$originalKey]]; } return $resultList; }
protected function _onDelete() { self::_deleteCacheByAbbreviation($this->getAbbreviation()); parent::_onDelete(); }
public function testCreateType() { $user = CM_Model_Abstract::createType(CM_Model_User::getTypeStatic()); $this->assertInstanceOf('CM_Model_User', $user); }
protected function _changeContainingCacheables() { parent::_changeContainingCacheables(); CM_Cache_Local::getInstance()->delete(CM_CacheConst::LanguageKey_Tree); }
public function toArray() { $array = parent::toArray(); $array['displayName'] = $this->getDisplayName(); $array['visible'] = $this->getVisible(); return $array; }
/** * @param CM_Model_Abstract $model */ public static function reinstantiateModel(CM_Model_Abstract &$model) { $model = CM_Model_Abstract::factoryGeneric($model->getType(), $model->getIdRaw()); }
/** * @param string $key * @param mixed $value */ protected function _cacheSet($key, $value) { $this->_model->_set(get_class($this) . ':' . $key, $value); }
public function jsonSerialize() { $array = parent::jsonSerialize(); $array['key'] = $this->getKey(); $array['type'] = $this->getType(); return $array; }
public function toArray() { $array = parent::toArray(); $array['path'] = $this->getPath(); return $array; }
/** * @param int $actionType * @param int $actionVerb */ public function __construct($actionType, $actionVerb) { parent::_construct(array('actionType' => (int) $actionType, 'actionVerb' => (int) $actionVerb)); }
protected function _getContainingCacheables() { $cacheables = parent::_getContainingCacheables(); $cacheables[] = new CM_Paging_Language_All(); $cacheables[] = new CM_Paging_Language_Enabled(); return $cacheables; }
public function jsonSerialize() { $array = parent::jsonSerialize(); $array['path'] = $this->getPath(); return $array; }
public function jsonSerialize() { $array = parent::jsonSerialize(); $array['displayName'] = $this->getDisplayName(); $array['visible'] = $this->getVisible(); return $array; }
protected function _getContainingCacheables() { $cacheables = parent::_getContainingCacheables(); $cacheables[] = new CM_Paging_Splitfeature_All(); return $cacheables; }
/** * @param string $key * @param mixed $value * @return mixed * @throws CM_Exception_Invalid * @throws CM_Model_Exception_Validation */ public function decodeField($key, $value) { $key = (string) $key; if ($this->hasField($key)) { $schemaField = $this->_schema[$key]; if (null !== $value) { $type = isset($schemaField['type']) ? $schemaField['type'] : null; if (null !== $type) { switch ($type) { case 'integer': case 'int': $value = (int) $value; break; case 'float': $value = (double) $value; break; case 'string': break; case 'boolean': case 'bool': $value = (bool) $value; break; case 'array': break; case 'DateTime': $value = DateTime::createFromFormat('U', $value); break; default: $id = CM_Params::jsonDecode($value); if (!is_array($id)) { $id = array('id' => $id); } $value = CM_Model_Abstract::factoryGeneric($type::getTypeStatic(), $id); } } } } return $value; }
/** * @param int $type * @return string */ protected function _getCollectionName($type) { $className = CM_Model_Abstract::getClassName($type); /** @var CM_Model_Abstract $className */ return $className::getTableName(); }
/** * @param string $key * @param mixed $value * @return mixed * @throws CM_Exception_Invalid * @throws CM_Model_Exception_Validation */ public function decodeField($key, $value) { $key = (string) $key; if ($this->hasField($key)) { $schemaField = $this->_schema[$key]; if (null !== $value) { $type = isset($schemaField['type']) ? $schemaField['type'] : null; if (null !== $type) { switch ($type) { case 'integer': case 'int': $value = (int) $value; break; case 'float': $value = (double) $value; break; case 'string': $value = (string) $value; break; case 'boolean': case 'bool': $value = (bool) $value; break; case 'array': break; case 'DateTime': $value = DateTime::createFromFormat('U', $value); break; default: if (!class_exists($type)) { throw new CM_Model_Exception_Validation('Field type is not a valid class/interface', null, ['type' => $type]); } $className = $type; if (is_a($className, 'CM_Model_Abstract', true)) { /** @var CM_Model_Abstract $type */ if ($this->_isJson($value)) { $value = CM_Util::jsonDecode($value); } $id = $value; if (!is_array($id)) { $id = ['id' => $id]; } $value = CM_Model_Abstract::factoryGeneric($type::getTypeStatic(), $id); } elseif (is_subclass_of($className, 'CM_ArrayConvertible', true)) { /** @var CM_ArrayConvertible $className */ $value = CM_Util::jsonDecode($value); $value = $className::fromArray($value); } else { throw new CM_Model_Exception_Validation('Class is neither CM_Model_Abstract nor CM_ArrayConvertible', null, ['className' => $className]); } if (!$value instanceof $className) { throw new CM_Model_Exception_Validation('Value is not an instance of the class', null, ['value' => CM_Util::var_line($value), 'className' => $className]); } } } } } return $value; }