This is a helper class for working with strings.
Inheritance: extends Webiny\Component\StdLib\StdObject\AbstractStdObject, implements ArrayAccess, use trait ManipulatorTrait, use trait ValidatorTrait
Beispiel #1
0
 /**
  * @param $interval
  *
  * @return \DateInterval
  * @throws DateTimeObjectException
  */
 private function parseDateInterval($interval)
 {
     try {
         if (!$this->isInstanceOf($interval, 'DateInterval')) {
             $interval = new StringObject($interval);
             if ($interval->startsWith('P')) {
                 $interval = new \DateInterval($interval);
             } else {
                 $interval = \DateInterval::createFromDateString($interval);
             }
         }
     } catch (\Exception $e) {
         throw new DateTimeObjectException(DateTimeObjectException::MSG_INVALID_DATE_INTERVAL, [$interval]);
     }
     return $interval;
 }
 /**
  * Parse attribute key recursively
  *
  * @param array        $parsedFields Reference to array of parsed fields
  * @param StringObject $key Current key to parse
  */
 private function buildFields(&$parsedFields, StringObject $key)
 {
     if ($key->contains('.')) {
         $parts = $key->explode('.', 2)->val();
         if (!isset($parsedFields[$parts[0]])) {
             $parsedFields[$parts[0]] = [];
         }
         $this->buildFields($parsedFields[$parts[0]], $this->str($parts[1]));
     } else {
         $parsedFields[$key->val()] = '';
     }
 }
Beispiel #3
0
 /**
  * This function parses the format provided by Config and sets the default formatting for getting date information
  * like day, month, year, etc..
  *
  * @throws DateTimeObjectException
  */
 private function parseDateTimeFormat()
 {
     try {
         if ($this->isNull($this->format)) {
             $this->format = self::$defaultFormat;
         }
         $str = new StringObject($this->format);
         $chunks = $str->split();
         $this->buildFormatterList();
         foreach ($chunks as $c) {
             foreach (self::$formatters as $fk => $f) {
                 if ($f->inArray($c)) {
                     $this->dateTimeFormat[$fk] = $c;
                 }
             }
         }
         $this->dateTimeFormat = new ArrayObject($this->dateTimeFormat);
     } catch (\Exception $e) {
         throw new DateTimeObjectException(DateTimeObjectException::MSG_INVALID_DATE_FORMAT, [$this->format]);
     }
 }
Beispiel #4
0
 /**
  * Set url path.
  *
  * @param StringObject|string $path Url path.
  *
  * @throws UrlObjectException
  * @return $this
  */
 public function setPath($path)
 {
     try {
         $path = new StringObject($path);
     } catch (\Exception $e) {
         throw new UrlObjectException($e->getMessage());
     }
     if ($path != '') {
         $path->trimLeft('/');
         $this->path = '/' . $path->val();
     } else {
         $this->path = $path->val();
     }
     $this->rebuildUrl();
     return $this;
 }
Beispiel #5
0
 /**
  * Change the case of all keys in current ArrayObject.
  *
  * @param string $case Case to which you want to covert array keys. Can be 'lower' or 'upper'.
  *
  * @return $this
  * @throws ArrayObjectException
  */
 public function changeKeyCase($case)
 {
     // validate case
     $case = new StringObject($case);
     $case->caseLower();
     $realCase = '';
     if ($case->equals('lower')) {
         $realCase = CASE_LOWER;
     } else {
         if ($case->equals('upper')) {
             $realCase = CASE_UPPER;
         } else {
             throw new ArrayObjectException(ArrayObjectException::MSG_PARAM_VALUE_OUT_OF_SCOPE, ['$case', '"lower" or "upper"']);
         }
     }
     $this->val(array_change_key_case($this->val(), $realCase));
     return $this;
 }
Beispiel #6
0
 private function validateSection(StringObject $section)
 {
     $tmp = $section->explode($this->delimiter);
     if ($tmp->first()->contains('-') || $this->isNumber($tmp->first()->val())) {
         throw new ConfigException(sprintf('Invalid config key "%s"', $section->val()));
     }
 }
 public function testLongerThan2()
 {
     $s = new StringObject('I had 10 dollars.');
     $this->assertFalse($s->longerThan(30));
 }
Beispiel #8
0
 /**
  * @dataProvider caseStrings
  */
 public function testSentenceCase($test)
 {
     $s = new StringObject($test);
     $this->assertEquals('This needs to be converted', $s->sentenceCase()->val());
 }