public function add($field, $value) { if (is_object($value)) { $value = \System\Std\Object::getPropertyValue($value, $field); } $validationStatck = new ValidationStack($value); $this->fields[$field] = $validationStatck; return $validationStatck; }
public static function getMeta($entityName) { $tableName = new String(String::set($entityName)->toLower()->split('\\.')->last()); $entityName = String::set($entityName)->replace('.', '\\'); $refClass = new \ReflectionClass((string) $entityName); $tokens = token_get_all(file_get_contents($refClass->getFileName())); $meta = array('System.Data.Entity.Attributes.Table' => new Attributes\Table((string) $tableName), 'System.Data.Entity.Attributes.Key' => new Attributes\Key((string) $tableName->append('_id')), 'Columns' => array()); $tmp = array(); $exitLoop = false; foreach ($tokens as $token) { switch ($token[0]) { case T_DOC_COMMENT: $lines = explode(PHP_EOL, $token[1]); foreach ($lines as $line) { $strAttr = trim(str_replace(array('*', '/'), '', $line)); if ($strAttr) { $attribute = String::set($strAttr)->get('@', '('); $args = String::set($strAttr)->get('(', ')'); if (!$attribute->indexOf('.')) { $attribute = $attribute->prepend('System.Data.Entity.Attributes.'); } try { $meta[(string) $attribute] = Object::getInstance($attribute, str_getcsv($args, ',')); } catch (\Exception $e) { throw new \System\Data\Entity\EntityException('The attribute "' . (string) $attribute . '" does not exist.'); } } } break; case T_COMMENT: $attribute = String::set($token[1])->subString(2)->get('@', '('); $args = String::set($token[1])->get('(', ')', true); if (!$attribute->indexOf('.')) { $attribute = $attribute->prepend('System.Data.Entity.Attributes.'); } if ((string) $args) { $tmp[] = Object::getInstance($attribute, str_getcsv($args, ',')); } else { $tmp[] = Object::getInstance($attribute); } break; case T_VARIABLE: $property = String::set($token[1])->subString(1); $meta['Columns'][(string) $property] = $tmp; $tmp = array(); break; case T_FUNCTION: $exitLoop = true; break; } if ($exitLoop) { break; } } $metaData = new EntityMeta((string) $entityName, $meta); return $metaData; }
public function __construct($name, $value = '', array $attributes = array()) { parent::__construct($name, $value); if (is_object($value)) { $value = \System\Std\Object::getPropertyValue($value, $name); } $attributes['value'] = $value; $attributes['name'] = !array_key_exists('name', $attributes) ? $name : $attributes['name']; $attributes['id'] = !array_key_exists('id', $attributes) ? $name : $attributes['id']; $this->attributes = array_merge($this->attributes, $attributes); }
public function render() { if (is_object($this->params)) { $this->params = \System\Std\Object::getProperties($this->params); } if (is_array($this->params)) { $href = $this->attributes['href']; foreach ($this->params as $param => $val) { $href = \System\Std\String::set($href)->replace('@' . $param, $val); } $this->attributes['href'] = $href; } return $this->control->append('<a ')->append($this->renderAttributes())->append('>')->append($this->title)->append('</a>')->toString(); }
private function toXml($collection, $rootNode) { foreach ($collection as $key => $value) { $key = is_numeric($key) ? 'item' : $key; if (is_array($value)) { $subNode = $rootNode->addChild($key); $this->toXml($value, $subNode); } elseif (is_object($value)) { $segments = explode('\\', get_class($value)); $subNode = $rootNode->addChild(array_pop($segments)); $this->toXml(\System\Std\Object::getProperties($value), $subNode); } else { $rootNode->addChild($key, htmlentities($value)); } } return $rootNode; }
public function saveChanges() { $log = array(); foreach ($this->dbSets as $set) { $meta = $set->getMeta(); $entities = $set->getEntities(); foreach ($entities as $entityContext) { $entity = $entityContext->getEntity(); $properties = Object::getProperties($entity); foreach ($properties as $property => $value) { if (is_object($value)) { $parentEntityHash = spl_object_hash($value); if ($this->persistedEntities->hasKey($parentEntityHash)) { $parentEntityContext = $this->persistedEntities->get($parentEntityHash); $parentMeta = $this->dbSets[$parentEntityContext->getName()]->getMeta(); $properties[$property] = Object::getPropertyValue($value, $parentMeta->getKey()->getKeyName()); } } if ($value instanceof \System\Std\Date) { $properties[$property] = $value->toString('yyyy-MM-dd HH:mm:ss'); } $columnAttributes = $meta->getColumnAttributes($property); foreach ($columnAttributes as $attribute) { if ($attribute instanceof \System\Data\Entity\Attributes\ConstraintAttribute) { $attribute->setColumnName($property); $attribute->setValue($value); if (!$attribute->isValid()) { throw new Attributes\ConstraintAttributeException($attribute->getErrorMessage()); } } if ($attribute instanceof \System\Data\Entity\Attributes\DefaultValue) { if (is_null($value)) { $properties[$property] = $attribute->getDefaultValue(); Object::setPropertyValue($entity, $property, $attribute->getDefaultValue()); } } } } switch ($entityContext->getState()) { case EntityContext::PERSIST: $result = $this->conn->insert($meta->getTable()->getTableName(), $properties); $log[] = $result; if ($result) { Object::setPropertyValue($entity, $meta->getKey()->getKeyName(), $this->conn->getInsertId($meta->getKey()->getKeyName())); $entityContext->setState(EntityContext::PERSISTED); $this->persistedEntities->add($entityContext->getHashCode(), $entityContext); } break; case EntityContext::PERSISTED: $updateParams = array($meta->getKey()->getKeyName() => $properties[$meta->getKey()->getKeyName()]); $log[] = $this->conn->update($meta->getTable()->getTableName(), $properties, $updateParams); break; case EntityContext::DELETE: $updateParams = array($meta->getKey()->getKeyName() => $properties[$meta->getKey()->getKeyName()]); $log[] = $this->conn->delete($meta->getTable()->getTableName(), $properties, $updateParams); $this->persistedEntities->removeAt($entityContext->getHashCode()); $set->detach($entity); break; } } } return array_sum($log); }
/** * Dispatches a controller. This method is declared final and cannot be overriden. * * @method run * @return void */ public final function run() { if ($this->routes->count() == 0) { throw new \RuntimeException('One or more routes must be registered.'); } $controllerDispacthed = false; foreach ($this->routes as $route) { $route->setHttpRequest($this->httpContext->getRequest()); $routeData = $route->execute(); if ($routeData) { $namespace = ''; if (Environment::getRootPath() != Environment::getControllerPath()) { $namespace = String::set(Environment::getControllerPath())->replace(Environment::getRootPath(), '')->trim('/'); } $moduleName = $routeData->get('module') ? $routeData->get('module') . '.' : ''; $class = String::set(sprintf('%s.%sControllers.%sController', $namespace, ucfirst(strtolower($moduleName)), ucfirst(strtolower($routeData->get('controller'))))); try { $controller = Object::getInstance($class); $controller->getRegistry()->merge(get_object_vars($this)); } catch (\ReflectionException $e) { throw new Mvc\ControllerNotFoundException(sprintf("The controller '%s' does not exist.", $class)); } if (!$controller instanceof Mvc\Controller) { throw new Mvc\MvcException(sprintf("The controller '%s' does not inherit from System\\Web\\Mvc\\Controller.", $class)); } $moduleClassName = String::set(sprintf('%s.%sControllers.%s', $namespace, ucfirst($moduleName), 'Module')); $this->authenticateRequest($controller); $this->preAction($controller); $moduleInstance = Object::getInstance($moduleClassName, null, false); if ($moduleInstance) { if (method_exists($moduleInstance, 'load')) { $moduleInstance->load($controller); } } $controller->execute($this->httpContext); if ($moduleInstance) { if (method_exists($moduleInstance, 'unload')) { $moduleInstance->unload($controller); } } $this->postAction($controller); $controllerDispacthed = true; break; } } if (false === $controllerDispacthed) { throw new Mvc\MvcException("Unable to dispatch a controller. None of the registered routes matched the request URI."); } }
public function max($field) { $this->sortBy($field); $count = $this->count() - 1; if (isset($this->collection[$count])) { $item = \System\Std\Object::getProperties($this->collection[$count]); return $item[$field]; } }