- strtoupper: documentation ( source)
- strtolower: documentation ( source)
<?php
class BookMediator {
private $authorObject;
private $titleObject;
function __construct() {
$this->authorObject = new BookAuthorColleague($this);
$this->titleObject = new BookTitleColleague($this);
}
function getAuthor() {return $this->authorObject;}
function getTitle() {return $this->titleObject;}
// when title or author change case, this makes sure the other
// stays in sync
function change(BookColleague $changingClassIn) {
if ($changingClassIn instanceof BookAuthorColleague) {
if ('upper' == $changingClassIn->getState()) {
if ('upper' != $this->getTitle()->getState()) {
$this->getTitle()->setTitleUpperCase($this->getAuthor()->getAuthor());
$this->getTitle()->setTitleLowerCase($this->getAuthor()->getAuthor());
}
} elseif ('lower' == $changingClassIn->getState()) {
if ('lower' != $this->getTitle()->getState()) {
$this->getTitle()->setTitleLowerCase($this->getAuthor()->getAuthor());
$this->getTitle()->setTitleUpperCase($this->getAuthor()->getAuthor());
}
}
} elseif ($changingClassIn instanceof BookTitleColleague) {
if ('upper' == $changingClassIn->getState()) {
if ('upper' != $this->getAuthor()->getState()) {
$this->getAuthor()->setAuthorUpperCase($this->getTitle()->getTitle());
$this->getAuthor()->setAuthorLowerCase($this->getTitle()->getTitle());
}
} elseif ('lower' == $changingClassIn->getState()) {
if ('lower' != $this->getAuthor()->getState()) {
$this->getAuthor()->setAuthorLowerCase($this->getTitle()->getTitle());
$this->getAuthor()->setAuthorUpperCase($this->getTitle()->getTitle());
}
}
}
}
}
abstract class BookColleague {
private $mediator;
function __construct($mediator_in) {
$this->mediator = $mediator_in;
}
function getMediator() {return $this->mediator;}
function changed($changingClassIn) {
getMediator()->titleChanged($changingClassIn);
}
}
class BookAuthorColleague extends BookColleague {
private $author;
private $state;
function __construct($mediator_in) {
parent::__construct($mediator_in);
}
function getAuthor() {return $this->author;}
function setAuthor($author_in) {$this->author = $author_in;}
function getState() {return $this->state;}
function setState($state_in) {$this->state = $state_in;}
function setAuthorUpperCase($message) {
$this->setAuthor(strtoupper($message));
$this->setState('upper');
$this->getMediator()->change($this);
}
function setAuthorLowerCase($message) {
$this->setAuthor(strtolower($message));
$this->setState('lower');
$this->getMediator()->change($this);
}
}
class BookTitleColleague extends BookColleague {
private $title;
private $state;
function __construct($mediator_in) {
parent::__construct($mediator_in);
}
function getTitle() {return $this->title;}
function setTitle($title_in) {$this->title = $title_in;}
function getState() {return $this->state;}
function setState($state_in) {$this->state = $state_in;}
function setTitleUpperCase($message) {
$this->setTitle(strtoupper($message));
$this->setState('upper');
$this->getMediator()->change($this);
}
function setTitleLowerCase($message) {
$this->setTitle(strtolower($message));
$this->setState('lower');
$this->getMediator()->change($this);
}
}
$mediator = new BookMediator();
$author = $mediator->getAuthor();
$title = $mediator->getTitle();
$author->setAuthorLowerCase("hallo");
writeln($author->getAuthor());
writeln($title->getTitle());
$title->setTitleUpperCase("huhu");
writeln($author->getAuthor());
writeln($title->getTitle());
$author->setAuthorUpperCase("huhggu");
writeln($author->getAuthor());
writeln($title->getTitle());
function writeln($line_in) {
echo $line_in.'<br/>';
}