public final function batch($method_name) { Kit::ensureString($method_name); $arg_list = func_get_args(); if (count($arg_list) > 1) { $arg_list = Kit::slice($arg_list, 1); } else { $arg_list = []; } $is_return_entity = NULL; $result = []; foreach ($this->getEntityList() as $index => $entity) { $result[] = $item = call_user_func_array([$entity, $method_name], array_merge($arg_list, [$index])); if (TRUE === is_null($is_return_entity)) { $is_return_entity = $item instanceof BaseEntity; } if ($is_return_entity !== $item instanceof BaseEntity) { throw new UserException('Inconsistent behavior of method.'); } } if (TRUE === $is_return_entity) { return $this->setEntityList($result); } else { return $result; } }
public function setEmail($email) { // @TODO: validate Kit::ensureString($email); $this->setInfo('Email', $email); return $this; }
public final function createEntity() { if (TRUE === is_null($this->entityClassName)) { $this->includeEntity(); } $entity_class_name = $this->entityClassName; Kit::ensureString(static::COLLECTION_NAME, TRUE); return new $entity_class_name(static::COLLECTION_NAME, static::ENTITY_PATH, FALSE); }
public static final function getInstance($collection_name, $entity_path) { Kit::ensureString($entity_path); if (FALSE === isset(self::$entityWrapperContainer)) { self::$entityWrapperContainer = new Container(); } if (TRUE === self::$entityWrapperContainer->has($entity_path)) { return self::$entityWrapperContainer->get($entity_path); } else { return self::$entityWrapperContainer->set($entity_path, new static($collection_name)); } }
public final function __construct($id_or_string) { if (TRUE === $id_or_string instanceof MongoDBId) { $this->id = $id_or_string->toMongoId(); } elseif (TRUE === $id_or_string instanceof MongoId) { $this->id = $id_or_string; } elseif (TRUE === MongoId::isValid($id_or_string)) { Kit::ensureString($id_or_string); try { $this->id = new MongoId($id_or_string); } catch (Exception $e) { throw new UserException("Invalid \$string({$id_or_string}) to construct a MongoId."); } } else { throw new UserException('Invalid $id_or_string.', $id_or_string); } }
/** * If $name is NOT assigned, $getData, $postData and $inputData will both be cleared. * @param string $name * @return boolean */ public static final function clear($name = NULL) { Kit::ensureString($name, TRUE); if (FALSE === is_null($name)) { if (TRUE === Kit::in($name, ['get', 'post', 'input'])) { $name .= 'Data'; self::${$name}->clear(); return TRUE; } else { throw new UserException("Invalid \$name({$name})."); } } else { self::$getData->clear(); self::$postData->clear(); self::$inputData->clear(); return TRUE; } }
protected function __construct($collection_name) { Kit::ensureString($collection_name); try { $this->collectionName = $collection_name; $this->collection = Loader::loadMongoDB()->selectCollection($collection_name); } catch (Exception $e) { throw new UserException('Initializing collection failed.', $collection_name, $e); } }
public final function passwordIs($password) { Kit::ensureString($password); return $this->infoFieldIs('Password', $password); }
private final function createEntityWithDocument($document) { // Kit::ensureDict($document); // @CAUTION Kit::ensureArray($document); if (FALSE === isset($document['_id']) or FALSE === $document['_id'] instanceof MongoId) { throw new UserException('_id is not set or proper in $document.', $document); } $document['_id'] = new MongoDBId($document['_id']); $entity_class_name = $this->entityClassName; Kit::ensureString($entity_class_name); return new $entity_class_name($this->collectionName, $this->entityPath, TRUE, $document); }
protected final function in($field_name, $field_value_list) { Kit::ensureString($field_name); Kit::ensureArray($field_value_list); // @CAUTION $criterion = [$field_name => ['$in' => $field_value_list]]; return $this->mergeCriterion($criterion); }
/** * Extracts args and handles the request, * by choosing the appropriate handler, * and calling the appropriate method * if $description CAN fit $this->uri. * @param string $description eg. '/project/(num)', '/(num)', '/', '/user/(any)', '(all)' * @param mixed $handler eg. 'Project', $this, an anonymous function * @param string|NULL $function eg. 'view' * @param boolean $is_time_consuming * @return boolean */ private final function fitGeneral($description, $handler, $function = NULL, $is_time_consuming = FALSE) { /** * eg. $description : '/project/(id:num)' => '/project/([0-9]+?)' * $this->uri : '/project/12' * $match_list : ['/project/12', '12'] */ $pattern = $this->getPattern($description); // It will attempt to match the whole $this->uri string. $uri = rtrim($this->uri, '/'); // $this->uri contains no GET args. if (1 === preg_match($pattern, $uri, $match_list)) { preg_match_all('@([^:\\(\\)]+):([^:\\(\\)]+)@', $description, $m, PREG_SET_ORDER); $mapping = []; foreach ($m as $value) { $mapping[$value[1]] = $value[2]; // 'id' => 'num' } $Input = Loader::loadInput(); foreach ($match_list as $key => $value) { // [0 => 12, 'id' => 12] if (TRUE === Kit::isInt($key)) { unset($match_list[$key]); } elseif ('num' === $mapping[$key]) { $Input->setInput($key, intval($value)); } else { $Input->setInput($key, $value); } } if (TRUE === Kit::isString($handler) or FALSE === $handler instanceof \Closure) { // $handler is a string or IS NOT an anonymous function, i.e., an instance. Kit::ensureString($function); $this->end(call_user_func_array([TRUE === Kit::isString($handler) ? Loader::loadService($handler) : $handler, $function], [$is_time_consuming])); } elseif (TRUE === is_callable($handler)) { // $handler is an anonymous function. $this->end(call_user_func_array($handler, [$is_time_consuming])); } // CAN FIT! return TRUE; } else { // CAN NOT FIT! return FALSE; } }
private final function has($path) { // Kit::ensureType($path, [ Kit::TYPE_STRING, Kit::TYPE_LIST ]); // @CAUTION // Kit::ensureType($path, [ Kit::TYPE_STRING, Kit::TYPE_ARRAY ]); Kit::ensureString($path); // Kit::ensureDict($this->document); // @CAUTION // Kit::ensureArray($this->document); // if (TRUE === Kit::isString($path)) { return FALSE === is_null($this->document[$path]); // } else throw new UserException('Can not support list-type $path yet.', $path); }
private final function getResult($type, $name) { if (FALSE === Kit::in($type, ['data', 'status', 'process'])) { throw new UserException('Invalid $type.', $type); } Kit::ensureString($name, TRUE); if (TRUE === is_null($name)) { return $this->result[$type]; } if (FALSE === isset($this->result[$type][$name])) { $msg = "Field({$name}) does not exist in {$type}."; throw new UserException($msg, $this->result[$type]); } return $this->result[$type][$name]; }
/** * eg. 'Collection/Content/ResourceCollection' => 'Content/Resource' * eg. 'Entity/Content/ResourceEntity' => 'Content/Resource' */ public static final function getModelPath($model_class_name, $delimiter = '\\') { Kit::ensureString($model_class_name); Kit::ensureString($delimiter); $handler_prefix = self::getHandlerPrefixFromPath($model_class_name); // 'Resource' $word_list = Kit::split($delimiter, $model_class_name); while (Kit::len($word_list) > 0 and 'Model' !== $word_list[0]) { $word_list = Kit::slice($word_list, 1); } $word_list = Kit::slice($word_list, 2); // [ 'Content', 'ResourceCollection' ] Kit::popList($word_list); // [ 'Content' ] $word_list[] = $handler_prefix; // [ 'Content', 'Resource' ] return Kit::join('/', $word_list); // 'Content/Resource' }