Esempio n. 1
0
 public function fromGlobals()
 {
     global $argv;
     $this->params(isset($argv) ? $argv : []);
     $this->servers($_SERVER);
     $this->protocol(strtoupper($this->server('SERVER_PROTOCOL')));
     $this->method(strtoupper($this->server('REQUEST_METHOD')));
     foreach ($this->servers() as $name => $value) {
         if (preg_match('/^HTTP_(.*)$/', $name, $match)) {
             $this->header(str_replace('_', '-', $match[1]), $value);
         }
     }
     if (function_exists('apache_request_headers')) {
         foreach (apache_request_headers() as $name => $value) {
             $this->header($name, $value);
         }
     }
     $this->scheme($this->server('HTTPS') == 'on' ? self::SCHEME_HTTPS : self::SCHEME_HTTP);
     $this->host($this->server('SERVER_NAME'));
     $this->port($this->server('SERVER_PORT'));
     list($full) = explode('?', $this->server('REQUEST_URI'));
     $path = isset($_GET['_']) ? $_GET['_'] : ltrim($full, '/');
     $full = explode('/', $full);
     $path = explode('/', $path);
     $base = array_slice($full, 0, count($full) - count($path));
     $this->base(implode('/', $base) . '/');
     $this->path(implode('/', $path));
     $this->queryParams($this->_clean($_GET));
     $this->bodyParams(\Coast\array_merge_smart($this->_clean($_POST), $this->_restructure($_FILES)));
     $this->body(file_get_contents('php://input'));
     $this->cookies($_COOKIE);
     return $this;
 }
Esempio n. 2
0
 public function options($options = null)
 {
     if (isset($options)) {
         $this->_options = \Coast\array_merge_smart($this->_options, $options);
         return $this;
     }
     return $this->_options;
 }
Esempio n. 3
0
 protected static function _defineMetadata($class)
 {
     return \Coast\array_merge_smart(parent::_defineMetadata($class), array('fields' => array('mailtoEmailAddress' => array('type' => 'string', 'nullable' => true), 'mailtoSubject' => array('type' => 'string', 'nullable' => true), 'mailtoBody' => array('type' => 'text', 'nullable' => true))));
 }
Esempio n. 4
0
 protected static function _defineMetadata($class)
 {
     return \Coast\array_merge_smart(parent::_defineMetadata($class), array('fields' => array('filters' => array('type' => 'array'), 'type' => array('type' => 'string'), 'sort' => array('values' => ['modifyDate,DESC' => 'Updated', 'publishDate' => 'Published', 'status' => 'Status']), 'subtypes' => array('type' => 'array', 'nullable' => true), 'contentIds' => array('type' => 'string', 'nullable' => true), 'contentNew' => array('type' => 'string', 'nullable' => true), 'createDateMin' => array('type' => 'string', 'nullable' => true, 'values' => ['today' => 'Today', '-1 hour' => 'Past Hour', '-1 day' => 'Past Day', '-2 day' => 'Past 2 Days', '-1 week' => 'Past Week', '-2 week' => 'Past 2 Weeks', '-1 month' => 'Past Month', '-3 month' => 'Past 3 Months', '-6 month' => 'Past 6 Months', '-1 year' => 'Past Year']), 'createDateMax' => array('type' => 'string', 'nullable' => true), 'modifyDateMin' => array('type' => 'string', 'nullable' => true, 'values' => ['today' => 'Today', '-1 hour' => 'Past Hour', '-1 day' => 'Past Day', '-2 day' => 'Past 2 Days', '-1 week' => 'Past Week', '-2 week' => 'Past 2 Weeks', '-1 month' => 'Past Month', '-3 month' => 'Past 3 Months', '-6 month' => 'Past 6 Months', '-1 year' => 'Past Year']), 'modifyDateMax' => array('type' => 'string', 'nullable' => true), 'publishDateMin' => array('type' => 'string', 'nullable' => true, 'values' => ['today' => 'Today', '-1 hour' => 'Past Hour', '-1 day' => 'Past Day', '-2 day' => 'Past 2 Days', '-1 week' => 'Past Week', '-2 week' => 'Past 2 Weeks', '-1 month' => 'Past Month', '-3 month' => 'Past 3 Months', '-6 month' => 'Past 6 Months', '-1 year' => 'Past Year']), 'publishDateMax' => array('type' => 'string', 'nullable' => true), 'statuses' => array('type' => 'array', 'nullable' => true, 'values' => [Chalk::STATUS_DRAFT => 'Draft', Chalk::STATUS_PUBLISHED => 'Published', Chalk::STATUS_ARCHIVED => 'Archived']), 'entity' => array('type' => 'text', 'nullable' => true), 'batch' => array('type' => 'string', 'nullable' => true, 'values' => ['publish' => 'Publish', 'archive' => 'Archive', 'restore' => 'Restore', 'delete' => 'Delete'])), 'associations' => ['contents' => array('type' => 'oneToMany', 'entity' => '\\Chalk\\Core\\Content')]));
 }
Esempio n. 5
0
 protected function _parseQueryParams(array $params = array(), $reset = false)
 {
     if (!$reset && isset($this->req)) {
         $params = \Coast\array_merge_smart($this->req->queryParams(), $params);
     }
     return \Coast\array_filter_null_recursive($params);
 }
Esempio n. 6
0
 protected static function _defineMetadata($class)
 {
     return \Coast\array_merge_smart(parent::_defineMetadata($class), array('fields' => array('dataJson' => ['type' => 'text', 'nullable' => true], 'tagsText' => ['type' => 'string', 'nullable' => true])), self::_defineMetadataPublishable($class));
 }
Esempio n. 7
0
 public function reverse($name, array $params = array())
 {
     if (isset($this->_aliases[$name])) {
         $name = $this->_aliases[$name];
     }
     if (!isset($this->_routes[$name])) {
         throw new Router\Exception("Route '{$name}' does not exist");
     }
     $route = $this->_routes[$name];
     $params = $params + $this->_params;
     $parts = explode('/', $route['path']);
     $path = [];
     foreach ($parts as $i => $part) {
         if (preg_match('/^\\{([a-zA-Z0-9_-]+)(?::(.*))?\\}(\\?)?$/', $part, $match)) {
             $match = \Coast\array_merge_smart(array('', '', '', ''), $match);
             if (isset($params[$match[1]])) {
                 $value = $params[$match[1]];
             } else {
                 if ($match[3] == '?') {
                     $value = null;
                 } else {
                     throw new Router\Exception("Parameter '{$match[1]}' missing");
                 }
             }
         } else {
             $value = $part;
         }
         $path[$i] = $value;
     }
     while (count($path) > 0 && !isset($path[count($path) - 1])) {
         array_pop($path);
     }
     return implode('/', $path);
 }
Esempio n. 8
0
/**
 * Recursively merge arrays, overwriting keys when arrays are key/value pairs, merging when numeric.
 * @return array
 */
function array_merge_smart()
{
    $merged = [];
    $arrays = \func_get_args();
    foreach ($arrays as $array) {
        foreach ($array as $key => $value) {
            if (\is_array($value) && isset($merged[$key])) {
                if (!\Coast\is_array_assoc($merged[$key]) && !\Coast\is_array_assoc($value)) {
                    $merged[$key] = \array_merge($merged[$key], $value);
                } else {
                    $merged[$key] = \Coast\array_merge_smart($merged[$key], $value);
                }
            } else {
                $merged[$key] = $value;
            }
        }
    }
    return $merged;
}
Esempio n. 9
0
 /**
  * Import from an array.
  * @param  string $name
  * @return mixed
  */
 public function fromArray(array $opts)
 {
     $this->_opts = \Coast\array_merge_smart($this->_opts, $opts);
     return $this;
 }
Esempio n. 10
0
 protected static function _defineMetadata($class)
 {
     return \Coast\array_merge_smart(parent::_defineMetadata($class), array('fields' => array('categories' => array('type' => 'array'))));
 }
Esempio n. 11
0
 protected static function _parseMetadata($md)
 {
     $md = array_merge(array('fields' => array(), 'associations' => array(), 'properties' => array()), $md);
     foreach ($md['fields'] as $name => $field) {
         $field = \Coast\array_merge_smart(array('id' => false, 'type' => null, 'length' => null, 'nullable' => false, 'validator' => new Validator()), $field);
         $validator = new Validator();
         if (!$field['id'] && !$field['nullable']) {
             $validator->set()->break();
         }
         if ($field['type'] == 'integer' || $field['type'] == 'smallint' || $field['type'] == 'bigint') {
             $validator->integer();
         } else {
             if ($field['type'] == 'float' || $field['type'] == 'decimal') {
                 $validator->float();
             } else {
                 if ($field['type'] == 'boolean') {
                     $validator->boolean();
                 } else {
                     if ($field['type'] == 'array' || $field['type'] == 'simple_array' || $field['type'] == 'json_array') {
                         $validator->array();
                     } else {
                         if ($field['type'] == 'string' || $field['type'] == 'text' || $field['type'] == 'guid') {
                             $validator->string();
                         } else {
                             if ($field['type'] == 'date') {
                                 $validator->dateTime('Y-m-d');
                             } else {
                                 if ($field['type'] == 'time') {
                                     $validator->dateTime('H:i:s');
                                 } else {
                                     if ($field['type'] == 'datetime' || $field['type'] == 'datetimez') {
                                         $validator->dateTime('Y-m-d H:i:s');
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if (isset($field['length'])) {
             $validator->length(null, $field['length']);
         }
         $validator->steps($field['validator']->steps());
         $field['validator'] = $validator;
         $md['fields'][$name] = $field;
         $md['properties'][$name] = $md['fields'][$name];
     }
     foreach ($md['associations'] as $name => $assoc) {
         $assoc = \Coast\array_merge_smart(array('type' => null, 'entity' => null, 'nullable' => false, 'validator' => new Validator()), $assoc);
         $validator = new Validator();
         if (!$assoc['nullable'] && $assoc['type'] == 'manyToOne') {
             $validator->set()->break();
         }
         $validator->steps($assoc['validator']->steps());
         $assoc['validator'] = $validator;
         $md['associations'][$name] = $assoc;
         $md['properties'][$name] = $md['associations'][$name];
     }
     return $md;
 }