This class extends the {{yii\helpers\StringHelper}} class by some helpful methods.
Since: 1.0.0-beta7
Author: Basil Suter (basil@nadar.io)
Inheritance: extends yii\helpers\StringHelper
Ejemplo n.º 1
0
 private function ensureNumber($number)
 {
     if (!StringHelper::startsWith($number, '+')) {
         $number = '+' . $number;
     }
     return str_replace(" ", "", $number);
 }
Ejemplo n.º 2
0
 /**
  * Setter method for $blockName, ensure the correct block name.
  *
  * @param string $name The name of the block.
  */
 public function setBlockName($name)
 {
     if (!StringHelper::endsWith($name, 'Block')) {
         $name .= 'Block';
     }
     $this->_blockName = Inflector::camelize($name);
 }
Ejemplo n.º 3
0
 public function onAfterListFind($event)
 {
     $value = StringHelper::typeCast($event->sender->getAttribute($this->name));
     foreach ($this->data as $item) {
         if ($item['value'] === $value) {
             $event->sender->setAttribute($this->name, $item['label']);
         }
     }
 }
Ejemplo n.º 4
0
 public function testArrayStrictContains()
 {
     $this->assertTrue(StringHelper::contains(['foo', 'bar'], 'hello foo bar', true));
     // enabled $strict mode
     $this->assertFalse(StringHelper::contains(['notexistings', 'bar'], 'hello bar foo', true));
     // enabled $strict mode
     $this->assertFalse(StringHelper::contains(['bar', 'notexistings'], 'hello bar foo', true));
     // enabled $strict mode
     $this->assertFalse(StringHelper::contains(['notexistings'], 'hello bar foo', true));
     // enabled strict mode
     $this->assertTrue(StringHelper::contains(['a', 'b', 'c'], 'thesmallabc', true));
 }
Ejemplo n.º 5
0
 /**
  * Generate the Link Tag.
  *
  * @param string $value The Brackets value `[]`.
  * @param string $sub The optional Parentheses value `()`
  * @see \luya\tag\TagInterface::parse()
  * @return string The parser tag.
  */
 public function parse($value, $sub)
 {
     if (substr($value, 0, 2) == '//') {
         $value = StringHelper::replaceFirst('//', Url::base(true) . '/', $value);
         $external = false;
     } else {
         $external = true;
     }
     $value = Url::ensureHttp($value);
     $label = empty($sub) ? $value : $sub;
     return Html::a($label, $value, ['class' => $external ? 'link-external' : 'link-internal', 'target' => $external ? '_blank' : null]);
 }
Ejemplo n.º 6
0
 public static function addToIndex($url, $title = null, $urlFoundOnPage = null)
 {
     $model = self::findOne(['url' => $url]);
     if ($model) {
         return false;
     }
     $model = new self();
     $model->url = $url;
     $model->title = StringHelper::truncate($title, 197);
     $model->url_found_on_page = $urlFoundOnPage;
     $model->crawled = 0;
     return $model->save(false);
 }
Ejemplo n.º 7
0
 public function actionClasses()
 {
     if (Config::has('rc1_block_classes_renameing')) {
         return $this->outputError("You already have run the classes updater, so your system should be up-to-date already.");
     }
     foreach (Block::find()->all() as $block) {
         $ns = $block->class;
         foreach ($this->_classMapping as $old => $new) {
             if (StringHelper::startsWith($ns, $old)) {
                 $this->outputError('old: ' . $ns);
                 $newNs = StringHelper::replaceFirst($old, $new, $ns);
                 $block->updateAttributes(['class' => $newNs]);
                 $this->outputSuccess('new: ' . $newNs);
             }
         }
     }
     Config::set('rc1_block_classes_renameing', true);
     return $this->outputSuccess('OK. You can now run the import command.');
 }
Ejemplo n.º 8
0
 public function getLinks()
 {
     try {
         $crawler = $this->getCrawler();
         if (!$crawler) {
             return [];
         }
         $links = $crawler->filterXPath('//a')->each(function ($node, $i) {
             return $node->extract(array('_text', 'href'))[0];
         });
         foreach ($links as $key => $item) {
             if (StringHelper::contains(['@'], $item[1])) {
                 unset($links[$key]);
                 continue;
             }
             $url = parse_url($item[1]);
             if (!isset($url['host']) || !isset($url['scheme'])) {
                 $base = $this->baseHost;
             } else {
                 $base = $url['scheme'] . '://' . $url['host'];
             }
             $path = null;
             if (isset($url['path'])) {
                 $path = $url['path'];
             }
             $url = rtrim($base, "/") . "/" . ltrim($path, "/");
             $links[$key][0] = self::cleanupString($links[$key][0]);
             $links[$key][1] = http_build_url($url, ['query' => isset($host['query']) ? $host['query'] : []], HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT);
         }
         return $links;
     } catch (\Exception $e) {
         return [];
     }
 }
Ejemplo n.º 9
0
 public function onAfterListFind($event)
 {
     $value = $event->sender->getAttribute($this->name);
     if (!$this->i18n) {
         $value = $this->jsonDecode($value);
     }
     $value = StringHelper::typeCast($value);
     if (!empty($value)) {
         $results = [];
         foreach ($this->getItems()['items'] as $item) {
             foreach ($value as $k => $v) {
                 if (isset($v['value']) && $item['value'] === $v['value']) {
                     $results[] = $item['label'];
                 }
             }
         }
         $event->sender->setAttribute($this->name, implode(", ", $results));
     }
 }