Created
February 1, 2015 16:44
-
-
Save JeroenDeDauw/f5d3ce3e184f31f0b776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Identifiable { | |
/** | |
* @return Id | |
*/ | |
public function getId(); | |
/** | |
* Needs to be an id of the correct type. | |
* If the type is wrong, an InvalidArgumentException is thrown. | |
* | |
* @param Id $id | |
*/ | |
public function setId( $id ); | |
} | |
class Kitten implements Identifiable { | |
/** | |
* @param KittenId $id | |
*/ | |
public function setId( $id ) { | |
if ( !( $id instanceof KittenId ) ) { | |
throw new InvalidArgumentException( 'Needs to be a KittenId' ); | |
} | |
} | |
} | |
class Pony implements Identifiable { | |
/** | |
* @param PonyId $id | |
*/ | |
public function setId( $id ) { | |
if ( !( $id instanceof PonyId ) ) { | |
throw new InvalidArgumentException( 'Needs to be a PonyId' ); | |
} | |
} | |
} | |
abstract class Id { | |
public abstract function toString(); | |
} | |
class KittenId extends Id { | |
// Makes sure all Kitten ids start with a "k" | |
} | |
class PonyId extends Id { | |
// Makes sure all pony ids start with a "p" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment