Bcp47Code
Simple interface representing languages which have a BCP 47 code
Loading...
Searching...
No Matches
Wikimedia\Bcp47Code\Bcp47Code Interface Reference

This interface defines an opaque object representing a language. More...

+ Inheritance diagram for Wikimedia\Bcp47Code\Bcp47Code:

Public Member Functions

 toBcp47Code ()
 
 isSameCodeAs (Bcp47Code $other)
 Compare two Bcp47Code objects.
 

Detailed Description

This interface defines an opaque object representing a language.

The language can return a standardized IETF BCP 47 language tag representing itself.

It is recommended that the internal language class in your code implement the Bcp47Code interface, and that you provide a mechanism that will accept a Bcp47Code and return an appropriate instance of your internal language code.

For example:

use Wikimedia\Bcp47Code\Bcp47Code;

class MyLanguage implements Bcp47Code {
   public function toBcp47Code(): string {
     return $this->code;
   }
   public static function fromBcp47(Bcp47Code $code): MyLanguage {
     if ($code instanceof MyLanguage) {
        return $code;
     }
     return new MyLanguage($code->toBcp47Code());
   }
   public function isSameCodeAs( Bcp47Code $other ): bool {
     if ( $other instanceof MyLanguage ) {
        // implement optimized MyLanguage-specific comparison
     }
     return strcasecmp( $this->toBcp47Code(), $other->toBcp47Code() ) === 0;
   }
}

Member Function Documentation

◆ isSameCodeAs()

Wikimedia\Bcp47Code\Bcp47Code::isSameCodeAs ( Bcp47Code $other)

Compare two Bcp47Code objects.

Note that BCP 47 codes are case insensitive, so if this comparison is going to use ::toBcp47Code() ensure the comparison is case insensitive.

Parameters
Bcp47Code$otherThe language tag to compare to
Returns
bool true if this language tag is the same as the given one

Implemented in Wikimedia\Bcp47Code\Bcp47CodeValue.

◆ toBcp47Code()

Wikimedia\Bcp47Code\Bcp47Code::toBcp47Code ( )
Returns
string a standardized IETF BCP 47 language tag

Implemented in Wikimedia\Bcp47Code\Bcp47CodeValue.


The documentation for this interface was generated from the following file: