code: public function cast($object)
      {
          if ($object !== null && !$this->isInstance($object)) {
              $objectClass = get_class($object);
              throw new ClassCastException("'{$objectClass}' cannot be cast to '{$this->getName()}'");
          }
          return $object;
      }
docs: Check whether $object can be cast to $this->getName().
  Note, that null can be cast to anything. In other words, it returns null if the given object is null.
  
  @param object $object
  @return object $object itself
  @throws ClassCastException