public static function instance($className, $constructArgs) { return function () use($className, $constructArgs) { $constructArgs = array_map(function ($arg) { return $arg instanceof Closure ? $arg() : $arg; }, $constructArgs); return !Data::is_assoc($constructArgs) ? (new ReflectionClass($className))->newInstanceArgs($constructArgs) : (new ReflectionClass($className))->newInstance($constructArgs); }; }
public static function apply($closure, $params = null, $context = null) { if ($context) { $closure = $closure->bindTo($context); } if ($params) { if (is_object($params) || Data::is_assoc($params)) { $params = self::keyword2position($closure, $params); } return call_user_func_array($closure, $params); } return $closure(); }
public function __get($collection) { if (empty($this->_config[$collection])) { $this->_config = Data::pushRight($this->_config); if (empty($this->_config[$collection])) { throw new Exception("collection \"{$collection}\" not defined"); } } $config = $this->_config[$collection]; if (empty($this->_cachedConnections[$config['url']])) { $this->_cachedConnections[$collection] = isset($config['options']) ? new MongoClient($config['url'], $config['options']) : new MongoClient($config['url']); } $db = $this->_cachedConnections[$collection]->selectDB($config['database']); $this->_currentCollection = $db->selectCollection($collection); return $this; }
public function update($configs) { if (isset($configs['components'])) { $configs['components'] = array_map(function ($value) { return ['class' => '\\' == $value[0][0] ? $value[0] : '\\zf\\components\\' . $value[0], 'constructArgs' => isset($value[1]) ? $value[1] : []]; }, Data::pushLeft($configs['components'])); } if ($this->_configs) { foreach ($configs as $key => $value) { if (is_array($value) && isset($this->_configs[$key])) { $this->_configs[$key] = $value + $this->_configs[$key]; } else { $this->_configs[$key] = $value; } } } else { $this->_configs = $configs; } }
public function resolvePath(...$args) { return $this->config->basedir . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, Data::flatten($args)); }
public function testZip() { $this->assertSame(Data::zip([1, 2, 3], [4, 5, 6]), [[1, 4], [2, 5], [3, 6]]); }
private function column($rows) { $cols = Data::transform($rows); $lens = array_map(function ($col) { return max(array_map('strlen', $col)); }, $cols); return array_map(function ($row) use($lens) { return implode("\t", array_map('str_pad', $row, $lens)); }, $rows); }
<?php use zf\Data; return ['schema' => function ($schema) { if ($this->errors = $this->validator->validate($this->body, $schema)) { $this->emit('validationfailed', ['errors' => $this->errors]); } }, 'param' => function (...$args) { $line = implode(',', $args); list($type, $name, $comment) = Data::explode(' ', $line, 3, ''); $name = ltrim($name, '$'); if (isset($this->params->{$name})) { if ($type === 'int') { $this->params->{$name} = intval($this->params->{$name}); } elseif ($type === 'float') { $this->params->{$name} = floatval($this->params->{$name}); } elseif ($type === 'bool') { $this->params->{$name} = boolval($this->params->{$name}); } elseif ($type === 'array') { if (!is_array($this->params->{$name})) { return 400; } else { if ($type !== 'string') { return [500, 'unknow param type ' . $type]; } } } } else { $this->params->{$name} = null; } }, 'mockup' => function ($mockup) {