/** * Contains loading of fields, that initialize after object initialization (lazy load). * Must be overriden in child Classes * In this abstract class only the simplest case is implemented only * @param Business $business * @param string $fieldName * @return mixed * @throws BaseException */ public function lazyload($business, $fieldName) { if (false === $business instanceof Business) { throw new BaseException("Object `{$business}` is not instance of `Business` class"); } if ($business instanceof Entity) { if (substr($fieldName, -3) == '_id') { //$name = ucfirst(substr($fieldName, 0, -3)); //fixed by A-25 //mappers of field names with _ should have camelCase names //for example, street_type_id => Mapper_StreetType //or street_type_id => Mapper_Street_Type $name = substr($fieldName, 0, -3); $nameParts = explode('_', $name); foreach ($nameParts as &$namePart) { $namePart = ucfirst($namePart); } $name = implode('', $nameParts); $mapperClassName1Variant = 'Mapper_' . $name; $mapperClassName2Variant = 'Mapper_' . implode('_', $nameParts); if (class_exists($mapperClassName1Variant)) { $mapperClassName = $mapperClassName1Variant; } else { //simply it was variant2 $mapperClassName = $mapperClassName2Variant; } if (class_exists($mapperClassName)) { $mapper = Manager::getInstance()->getRepository()->get($mapperClassName); $fieldValue = $business->getInfo($fieldName); return $fieldValue !== null ? $mapper->getEntityById($fieldValue) : null; } else { throw new BaseException("{$mapperClassName} not found"); } } } throw new BaseException("Can't lazy load field {$fieldName} in mapper {$this}"); }
/** * @param Business $business */ public function set(Business $business) { $key = $this->generator->generate($business->getCacheKey()); $this->adapter->set($key, $this->packer->pack($business), $business->getExpires()); }