Skip to content

Instantly share code, notes, and snippets.

@chrisdlangton
Last active June 21, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdlangton/add6e1d5769b26b687548ea2776d78fe to your computer and use it in GitHub Desktop.
Save chrisdlangton/add6e1d5769b26b687548ea2776d78fe to your computer and use it in GitHub Desktop.
from repo php-functional-programming
<?php
class Dictionary {
private $data = [];
public function __construct(array $data) {
$this->data = $data;
}
public function validate(array $data): void {
if (array_keys($data) === array_keys(array_values($data))) {
throw new Exception(
'Dictionary::validate Dictionary must be indexed by keys'
);
}
}
public function hasValue(string $key): bool {
return array_key_exists($key, $this->data);
}
public function getValue(string $key, $default = NULL) {
if ($this->hasValue($key)) {
return $this->data[$key];
}
return $default;
}
public function withValue(string $key, $value): Dictionary {
if (!is_string($key)) {
throw new Exception(
'Dictionary::validate Dictionary $key must be a string'
);
}
$this->validate([$key => $value]);
$copy = clone $this;
$copy->data[$key] = $value;
return $copy;
}
public function withoutValue($key): Dictionary {
$copy = clone $this;
unset($copy->data[$key]);
return $copy;
}
}
<?php
class OrderedList {
private $sorter = 'sort';
private $data = [];
public function __construct(array $data) {
$this->data = $data;
}
public function validate(array $data): void {
if (array_values($data) !== $data) {
throw new Exception(
'OrderedList::validate List structures cannot be indexed by keys'
);
}
}
public function withSorter(callable $sorter): OrderedList {
if ($this->sorter === $sorter) {
return $this;
}
$copy = clone $this;
$copy->sorter = $sorter;
return $copy;
}
public function getData(): array {
call_user_func_array($this->sorter, [&$this->data]);
return $this->data;
}
}
<?php
class Set {
private $data = [];
public function __construct(array $data) {
$dataClean = array_unique($data, SORT_REGULAR);
$this->validate($dataClean);
$this->data = $dataClean;
}
public function validate(array $data): void {
if (array_values($data) !== $data) {
throw new Exception(
'Set::validate Set structures cannot be indexed by keys'
);
}
}
public function replaceData(array $data): void {
$dataClean = array_unique($data, SORT_REGULAR);
$this->validate($dataClean);
$this->data = $dataClean;
}
public function hasValue($value): bool {
return in_array($value, $this->data);
}
public function withValue($value): Set {
if ($this->hasValue($value)) {
return $this;
}
$copy = clone $this;
array_push($copy->data, $value);
return $copy;
}
public function withValueAfter($value, $search) {
if ($this->hasValue($value)) {
return $this;
}
$this->validate([$value]);
$copy = clone $this;
$key = array_search($search, $this->data);
if (FALSE === $key) {
array_push($copy->data, $value);
} else {
array_splice($copy->data, $key + 1, 0, $value);
}
return $copy;
}
public function withValueBefore($value, $search) {
if ($this->hasValue($value)) {
return $this;
}
$this->validate([$value]);
$copy = clone $this;
$key = array_search($search, $this->data);
if (FALSE === $key) {
array_unshift($copy->data, $value);
} else {
array_splice($copy->data, $key, 0, $value);
}
return $copy;
}
}
<?php
class UnorderedList {
private $data = [];
public function __construct(array $data) {
$this->data = $data;
}
public function validate(array $data) {
if (array_values($data) !== $data) {
throw new Exception(
'UnorderedList::validate List structures cannot be indexed by keys'
);
}
}
public function getData(): array {
return $this->data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment