/** * {@inheritdoc} */ protected function _compactResponse($apiResponse) { $dataResponse = new Data($apiResponse); $response = array('code' => $dataResponse->find('response.code', 0, 'int'), 'headers' => array_change_key_case((array) $dataResponse->get('headers', array()), CASE_LOWER), 'body' => $dataResponse->get('body')); $response = new Data($response); return $response; }
/** * User authorize. * * @param array $user * @param Request $request * @return bool */ public function authorize($user, Request $request) { $user = new Data($user); // Allow all for admin role. if (Role::ADMIN_ID == $user->get('role_id', Role::PUBLIC_ID)) { return true; } return (bool) $request->param('allowed'); }
/** * @param string $module * @param string $format * @return Data */ protected function _listFactory($module, $format) { $key = $module . '.' . $format; if ($list = $this->_storage->get($key)) { return $list; } if ($module === self::DEFAULT_MODULE) { $path = $module . ':langs/' . $this->_code . '.' . $format; } else { $path = $module . ':langs/' . $this->_code . '.' . $module . '.' . $format; } $listPath = $this->_path->get($path); // @codeCoverageIgnoreStart if ('php' === $format) { $list = new PHPArray($listPath); } elseif ('json' === $format) { $list = new JSON($listPath); } elseif ('ini' === $format) { $list = new Ini($listPath); } elseif ('yml' === $format) { $list = new Yml($listPath); } else { $list = new Data([]); } // @codeCoverageIgnoreEnd $this->_storage->set($key, $list); return $list; }
/** * Response constructor. * @param array|string $data */ public function __construct($data = array()) { $data['code'] = 0; $data['headers'] = array(); $data['body'] = ''; $this->_jsonData = null; parent::__construct($data); }
/** * @param Data $data */ public function __construct(Data $data) { $this->_id = $data->get('id', 0, 'int'); $this->_login = $data->get('login', '', 'low'); $this->_email = $data->get('email', '', 'email, low'); $this->_name = $data->get('name', '', 'trim'); $this->_isAdmin = $data->get('is_admin', false, 'bool'); }
/** * @return int */ protected function _setEnv() { // set limits & reporting if ($this->_isDebug()) { error_reporting(-1); } else { error_reporting(E_ERROR | E_WARNING); } $memory = $this->_globConfig->get('memory', '1024M'); $time = (int) $this->_globConfig->get('time', 1800); Sys::iniSet('display_errors', 1); Sys::setTime($time); Sys::setMemory($memory); }
/** * @return string */ protected function _getHash() { // Check depends $mixins = $this->_options->get('autoload', [], 'arr'); $hashes = []; foreach ($mixins as $mixin) { $hashes[$mixin] = md5_file($mixin); } ksort($hashes); $options = $this->_options->getArrayCopy(); $options['functions'] = array_keys($options['functions']); ksort($options); $hashed = ['less' => $this->_less, 'less_md5' => md5_file($this->_less), 'base' => $this->_base, 'mixins' => $hashes, 'options' => $options]; $hashed = serialize($hashed); $hash = md5($hashed); // md5 is faster than sha1! return $hash; }
/** * Stop or pause coverage proccess */ protected function _createReports() { if (!$this->_coverage) { return; } $reportXmlDir = $this->_config->get('build_xml'); if ($this->_config->get('xml', true, 'bool')) { $this->_checkDir($reportXmlDir); $report = new \PHP_CodeCoverage_Report_Clover(); $report->process($this->_coverage, $reportXmlDir . '/' . $this->_hash . '.xml'); } $reportCovDir = $this->_config->get('build_cov'); if ($this->_config->get('cov', false, 'bool')) { $this->_checkDir($reportCovDir); $report = new \PHP_CodeCoverage_Report_PHP(); $report->process($this->_coverage, $reportCovDir . '/' . $this->_hash . '.cov'); } $reportHtmlDir = $this->_config->get('build_html'); if ($this->_config->get('html', false, 'bool')) { $this->_checkDir($reportHtmlDir); $report = new \PHP_CodeCoverage_Report_HTML(); $report->process($this->_coverage, $reportHtmlDir . '/' . $this->_hash); } }
/** * {@inheritdoc} */ protected function _request($url, $args, Data $options) { $method = $options->get('method'); $headers = $options->get('headers'); $timeout = $options->get('timeout'); $sslVerify = $options->get('ssl_verify'); if (empty($headers)) { $headers = null; } if ($sslVerify) { // "curl" driver doesn't have such option $httpClient = \JHttpFactory::getHttp(); // try to find curl driver } else { $httpClient = \JHttpFactory::getHttp(null, 'stream'); } $httpClient->setOption('userAgent', $options->get('user_agent')); if (self::METHOD_GET === $method) { $apiResponse = $httpClient->get($url, $headers, $timeout); } elseif (self::METHOD_POST === $method) { $apiResponse = $httpClient->post($url, $args, $headers, $timeout); } elseif (self::METHOD_HEAD === $method) { $apiResponse = $httpClient->head($url, $headers, $timeout); } elseif (self::METHOD_PUT === $method) { $apiResponse = $httpClient->put($url, $args, $headers, $timeout); } elseif (self::METHOD_DELETE === $method) { $apiResponse = $httpClient->delete($url, $headers, $timeout); } elseif (self::METHOD_OPTIONS === $method) { $apiResponse = $httpClient->options($url, $headers, $timeout); } elseif (self::METHOD_PATCH === $method) { $apiResponse = $httpClient->patch($url, $args, $headers, $timeout); } else { $apiResponse = $httpClient->get($url, $headers, $timeout); } return $apiResponse; }
/** * @param string $name * @param mixed $default * @return mixed */ protected function _header($name, $default = null) { static $headers; if (is_null($headers)) { $headers = Http::getHeaders(); $headers = new Data($headers); } $name = strtoupper($name); $name = str_replace('-', '_', $name); return $headers->get($name, $default); }
public function testFilter() { $value = ' 123.456 string <i>qwerty</i> '; $data = new Data(array('key' => $value, 'array' => array('inner' => $value))); // Default value isSame(null, $data->get('undefined')); isSame(42.123, $data->get('undefined', 42.123)); isSame(42, $data->get('undefined', 42.123, 'int')); isSame(42.123, $data->get('undefined', '42.123', 'float')); // Get & find isSame($value, $data->get('key')); isSame($value, $data->find('key')); isSame($value, $data->find('array.inner')); // One filter isSame(123, $data->get('key', null, 'int')); isSame(123, $data->find('key', null, 'int')); isSame(123, $data->find('array.inner', null, 'int')); // Several filters isSame('stringqwerty', $data->get('key', null, 'strip, trim, alpha')); isSame('stringqwerty', $data->find('key', null, 'strip, trim, alpha')); isSame('stringqwerty', $data->find('array.inner', null, 'strip, trim, alpha')); // Several filters isSame('123.456 string qwerty', $data->get('key', null, function ($value) { return trim(strip_tags($value)); })); isSame('123.456 string qwerty', $data->find('key', null, function ($value) { return trim(strip_tags($value)); })); isSame('123.456 string qwerty', $data->find('array.inner', null, function ($value) { return trim(strip_tags($value)); })); }
public function testNumeric() { $data = new Data(array(0 => 0, 1 => 1, 'string' => 'test', 2 => array(1), 'nested' => array('0', 1))); isSame(0, $data->get(0)); isSame(1, $data->find('2.0')); isSame('0', $data->find('nested.0')); isSame(0, $data['0']); isSame(1, $data[2][0]); }
/** * @return bool */ protected function _isDebug() { return $this->_options->get('debug', false, 'bool'); }
/** * @param Data $response * @param string $resultType * @return mixed */ protected function _getResultByType(Data $response, $resultType) { // response type if ($resultType == self::RESULT_BODY && 200 === $response->get('code')) { $result = $response->get('body'); } elseif ($resultType == self::RESULT_HEAD) { $result = new Data($response->get('headers')); } elseif ($resultType == self::RESULT_CODE) { $result = $response->get('code'); } elseif ($resultType == self::RESULT_FULL) { $result = $response; } else { $result = $response->get('body'); } return $result; }
/** * Bet aro from request data. * * @return \Acl\Model\Entity\Aro */ protected function _getAro() { $userId = $this->_user->get('role_id', Role::PUBLIC_ID); $cacheKey = 'role_' . $userId; return $this->_table->Aros->find()->where(['model' => 'Roles', 'id' => $userId])->cache($cacheKey, $this->_cacheConfig)->first(); }
/** * @param string $fullPath * @param null $relPath */ public function setImportPath($fullPath, $relPath = null) { $relPath = $relPath ?: $this->_options->get('root_url'); $this->_driver->setImportPath($fullPath, $relPath); }
public function testForReadme() { $times = 10000; $this->_data = array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true), 'inner' => array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true), 'inner' => array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true)))); $array = $this->_data; $data = new Data($this->_data); $arrobj = new \ArrayObject($this->_data); Benchmark::compare(array('Array' => function () use($array) { $var = $array; // for clean experiment return $var; }, 'ArrayObject' => function () use($array) { $var = new \ArrayObject($array); return $var; }, 'Data' => function () use($array) { $var = new Data($array); return $var; }), array('name' => 'For Readme: Create', 'count' => $times)); Benchmark::compare(array('Array' => function () use($array) { return array_key_exists('prop', $array) ? $array['prop'] : null; }, 'ArrayObject' => function () use($arrobj) { return $arrobj->offsetGet('prop'); }, 'Data' => function () use($data) { return $data->get('prop'); }), array('name' => 'For Readme: Get by key', 'count' => $times)); Benchmark::compare(array('Array' => function () use($array) { if (array_key_exists('inner', $array) && array_key_exists('inner', $array['inner']) && array_key_exists('prop', $array['inner']['inner'])) { return $array['inner']['inner']['prop']; } return 42; }, 'ArrayObject' => function () use($arrobj) { if (array_key_exists('inner', $arrobj) && array_key_exists('inner', $arrobj['inner']) && array_key_exists('prop', $arrobj['inner']['inner'])) { return $arrobj['inner']['inner']['prop']; } return 42; }, 'Data' => function () use($data) { return $data->find('inner.inner.prop', 42); }), array('name' => 'For Readme: Find nested defined var', 'count' => $times)); Benchmark::compare(array('Array' => function () use($array) { if (array_key_exists('inner', $array) && array_key_exists('inner', $array['inner']) && array_key_exists('undefined', $array['inner']['inner'])) { return $array['inner']['inner']['prop']; } return 42; }, 'ArrayObject' => function () use($arrobj) { if (array_key_exists('inner', $arrobj) && array_key_exists('inner', $arrobj['inner']) && array_key_exists('undefined', $arrobj['inner']['inner'])) { return $arrobj['inner']['inner']['undefined']; } return 42; }, 'Data' => function () use($data) { return $data->find('inner.inner.undefined', 42); }), array('name' => 'For Readme: Find nested undefined var', 'count' => $times)); }