/**
  * 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);
 }
Beispiel #2
0
 /**
  * Says hello.
  *
  * @return Rope
  */
 public function sayHello()
 {
     return Rope::of('hello there');
 }
Beispiel #3
0
 public function testUpperWords()
 {
     $this->assertEqualsMatrix([['Omg Words', Rope::of('omg words')->upperWords()->toString()], ['Lower 今日は', Rope::of('Lower 今日は')->upperWords()->toString()]]);
 }
Beispiel #4
0
 /**
  * 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'))]]);
 }
Beispiel #6
0
 /**
  * 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);
 }