예제 #1
0
 /**
  * Check if the constraint is met.
  *
  * @param mixed $value
  * @param array $context
  *
  * @return mixed
  */
 public function check($value, array $context = [])
 {
     if (is_string($value) || $value instanceof Rope) {
         $lower = Rope::of($value)->toLower();
         return $lower->equals(Rope::of('true')) || $lower->equals(Rope::of('false'));
     } elseif (is_int($value)) {
         return $value === 0 || $value === 1;
     } elseif (is_float($value)) {
         return $value === 0.0 || $value === 1.0;
     }
     return is_bool($value);
 }
예제 #2
0
 /**
  * Says hello.
  *
  * @return Rope
  */
 public function sayHello()
 {
     return Rope::of('hello there');
 }
예제 #3
0
 public function testUpperWords()
 {
     $this->assertEqualsMatrix([['Omg Words', Rope::of('omg words')->upperWords()->toString()], ['Lower 今日は', Rope::of('Lower 今日は')->upperWords()->toString()]]);
 }
예제 #4
0
파일: Std.php 프로젝트: chromabits/nucleus
 /**
  * Attempt to cast a value into a bool.
  *
  * @param mixed $mixed
  *
  * @return bool
  * @throws CoreException
  */
 public static function castToBool($mixed)
 {
     if (is_string($mixed) || $mixed instanceof Rope) {
         $lower = Rope::of($mixed)->toLower();
         if ($lower->equals(Rope::of('true'))) {
             return true;
         } elseif ($lower->equals(Rope::of('false'))) {
             return false;
         }
         throw new CoreException('Unable to cast into a bool.');
     } elseif (is_int($mixed)) {
         if ($mixed === 1) {
             return true;
         } elseif ($mixed === 0) {
             return false;
         }
         throw new CoreException('Unable to cast into a bool.');
     } elseif (is_float($mixed)) {
         if ($mixed === 1.0) {
             return true;
         } elseif ($mixed === 0.0) {
             return false;
         }
         throw new CoreException('Unable to cast into a bool.');
     } elseif (is_bool($mixed)) {
         return $mixed;
     }
     throw new CoreException('Unable to cast into a bool.');
 }
 public function testCheck()
 {
     $instance = new ClassTypeConstraint(Rope::class);
     $this->assertEqualsMatrix([[false, $instance->check(null)], [false, $instance->check('hello world')], [false, $instance->check('hello world' . null)], [false, $instance->check(27645)], [false, $instance->check(276.564)], [false, $instance->check(new stdClass())], [true, $instance->check(Rope::of('some random string'))]]);
 }
예제 #6
0
파일: Str.php 프로젝트: chromabits/nucleus
 /**
  * Return whether or not the subject contains the inner string.
  *
  * @param string $subject
  * @param string $inner
  * @param null|string $encoding
  *
  * @return bool
  */
 public static function contains($subject, $inner, $encoding = null)
 {
     return Rope::of($subject, $encoding)->contains($inner);
 }