startsWith() public static method

public static startsWith ( string $haystack, string $needle, boolean $ignoreCase = false ) : boolean
$haystack string
$needle string
$ignoreCase boolean
return boolean
Beispiel #1
0
 /**
  * Mwt constructor.
  *
  * @param array $options
  */
 public function __construct($options = [])
 {
     foreach (get_object_vars($this) as $field => $_) {
         if (!Text::startsWith($field, '_')) {
             $this->_fields[] = $field;
         }
     }
     if (isset($options['type'])) {
         $this->_type = $options['type'];
     }
     if (isset($options['keys'])) {
         $this->_keys = $options['keys'];
     } else {
         $this->_keys = [$this->configure->getSecretKey('mwt:' . $this->_type)];
     }
     if (isset($options['ttl'])) {
         $this->_ttl = $options['ttl'];
     }
 }
Beispiel #2
0
 /**
  * Filter constructor.
  *
  * @param array $options
  */
 public function __construct($options = [])
 {
     foreach (get_class_methods($this) as $method) {
         if (Text::startsWith($method, '_filter_')) {
             $this->_filters[substr($method, 8)] = [$this, $method];
         }
     }
     if (is_object($options)) {
         $options = (array) $options;
     }
     if (isset($options['messages'])) {
         $this->_messages = $options['messages'];
     }
     if (isset($options['defaultMessage'])) {
         $this->_defaultMessage = $options['defaultMessage'];
     }
     if (isset($options['xssByReplace'])) {
         $this->_xssByReplace = $options['xssByReplace'];
     }
 }
Beispiel #3
0
 protected function _getSourceFiles($dir)
 {
     $files = [];
     $dh = opendir($dir);
     while ($file = readdir($dh)) {
         if (Text::startsWith($file, '.')) {
             continue;
         }
         $file = str_replace('\\', '/', $dir) . '/' . $file;
         if (is_dir($file)) {
             $files = array_merge($files, $this->_getSourceFiles($file));
         } else {
             if (fnmatch('*.php', $file)) {
                 $files[] = $file;
             }
         }
     }
     closedir($dh);
     return $files;
 }
Beispiel #4
0
 /**
  * Handles routing information received from the rewrite engine
  *
  *<code>
  * //Read the info from the rewrite engine
  * $router->handle();
  *
  * //Manually passing an URL
  * $router->handle('/posts/edit/1');
  *</code>
  *
  * @param string $uri
  * @param string $host
  * @param bool   $silent
  *
  * @return bool
  * @throws \ManaPHP\Mvc\Router\Exception
  * @throws \ManaPHP\Mvc\Router\NotFoundRouteException
  */
 public function handle($uri = null, $host = null, $silent = true)
 {
     if ($uri === null) {
         $uri = $this->getRewriteUri();
     }
     if ($this->_removeExtraSlashes) {
         $uri = rtrim($uri, '/');
     }
     $refinedUri = $uri === '' ? '/' : $uri;
     $this->fireEvent('router:beforeCheckRoutes');
     $module = null;
     $routeFound = false;
     for ($i = count($this->_groups) - 1; $i >= 0; $i--) {
         $group = $this->_groups[$i];
         $path = $group['path'];
         $module = $group['module'];
         if ($path === '' || $path[0] === '/') {
             $checkedUri = $refinedUri;
         } else {
             $checkedUri = (strpos($path, '://') ? $this->request->getScheme() . '://' : '') . $_SERVER['HTTP_HOST'] . $refinedUri;
         }
         /**
          * strpos('/','')===false NOT true
          */
         if ($path !== '' && !Text::startsWith($checkedUri, $path)) {
             continue;
         }
         /**
          * substr('a',1)===false NOT ''
          */
         $handledUri = strlen($checkedUri) === strlen($path) ? '/' : substr($checkedUri, strlen($path));
         /**
          * @var \ManaPHP\Mvc\Router\Group $groupInstance
          */
         if ($group['groupInstance'] === null) {
             $group['groupInstance'] = $this->_dependencyInjector->get($group['groupClassName']);
         }
         $groupInstance = $group['groupInstance'];
         $parts = $groupInstance->match($handledUri);
         $routeFound = $parts !== false;
         if ($routeFound) {
             break;
         }
     }
     $this->_wasMatched = $routeFound;
     if ($routeFound) {
         $this->_module = $module;
         $this->_controller = isset($parts['controller']) ? basename($parts['controller'], 'Controller') : 'index';
         $this->_action = isset($parts['action']) ? basename($parts['action'], 'Action') : 'index';
         $params = [];
         if (isset($parts['params'])) {
             $params_str = trim($parts['params'], '/');
             if ($params_str !== '') {
                 $params = explode('/', $params_str);
             }
         }
         unset($parts['controller'], $parts['action'], $parts['params']);
         $this->_params = array_merge($params, $parts);
     }
     $this->fireEvent('router:afterCheckRoutes');
     if (!$routeFound && !$silent) {
         throw new NotFoundRouteException('router does not have matched route for `:uri`', ['uri' => $uri]);
     }
     return $routeFound;
 }
Beispiel #5
0
 /**
  * @param array        $rows
  * @param string       $attachmentName
  * @param array|string $columns
  *
  * @return static
  */
 public function setCsvContent($rows, $attachmentName, $columns = null)
 {
     if (is_string($columns)) {
         $columns = explode(',', $columns);
     }
     if (pathinfo($attachmentName, PATHINFO_EXTENSION) !== 'csv') {
         $attachmentName .= '.csv';
     }
     $this->setAttachment($attachmentName);
     $file = fopen('php://temp', 'r+');
     fprintf($file, "");
     if ($columns !== null) {
         if (Text::startsWith($columns[0], 'ID')) {
             $columns[0] = strtolower($columns[0]);
         }
         fputcsv($file, $columns);
     }
     foreach ($rows as $row) {
         if (is_object($row)) {
             if (method_exists($row, 'toArray')) {
                 $data = $row->toArray();
             } else {
                 $data = (array) $row;
             }
         } elseif (!is_array($row)) {
             $data = [$row];
         } else {
             $data = $row;
         }
         fputcsv($file, $data);
     }
     rewind($file);
     $content = stream_get_contents($file);
     fclose($file);
     $this->setContentType('text/csv');
     $this->setContent($content);
     return $this;
 }
Beispiel #6
0
 /**
  * Gets most possible client IPv4 Address. This method search in $_SERVER['REMOTE_ADDR'] and optionally in $_SERVER['HTTP_X_FORWARDED_FOR']
  *
  * @return string
  */
 public function getClientAddress()
 {
     if ($this->_clientAddress === null) {
         if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
             $this->_clientAddress = $_SERVER['REMOTE_ADDR'];
         } else {
             $client_address = $_SERVER['REMOTE_ADDR'];
             if (Text::startsWith($client_address, '127.0.') || Text::startsWith($client_address, '192.168.') || Text::startsWith($client_address, '10.')) {
                 $this->_clientAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
             } else {
                 $this->_clientAddress = $_SERVER['REMOTE_ADDR'];
             }
         }
     }
     return $this->_clientAddress;
 }
Beispiel #7
0
 /**
  * @param string $task
  *
  * @return void
  */
 public function reset($task)
 {
     $rc = new \ReflectionClass($this);
     foreach ($rc->getConstants() as $n => $field) {
         if (!Text::startsWith($n, 'FIELD_')) {
             continue;
         }
         $this->adapter->delete($this->_formatKey($task, $field));
     }
 }