diff --git a/src/BaseTypes.php b/src/BaseTypes.php index 0cedf2990b7ec37869f1c37283fd4533b5e93d88..e050dc5e2c4eff058fdc9ba0f443a14ce119d33a 100644 --- a/src/BaseTypes.php +++ b/src/BaseTypes.php @@ -1,10 +1,5 @@ <?php -/** - * @file - * Enum for PHP Base types. - */ - namespace Drupal\data_structures; /** diff --git a/src/DataStructure/CallableImmutableSequence.php b/src/DataStructure/CallableImmutableSequence.php index 9b8f35d548d3f5b6081de35d08d6568b33568400..8bad378a6b0b06bf77f0fcf0007ecaacd3a2f02b 100644 --- a/src/DataStructure/CallableImmutableSequence.php +++ b/src/DataStructure/CallableImmutableSequence.php @@ -22,7 +22,7 @@ class CallableImmutableSequence implements \IteratorAggregate, \Countable, \Arra * @param iterable $values * An iterable data set of values. */ - public function __construct(iterable $values) { + final public function __construct(iterable $values) { $this->values = []; foreach ($values as $value) { if (!(is_callable($value))) { diff --git a/src/DataStructure/FloatImmutableSequence.php b/src/DataStructure/FloatImmutableSequence.php index 81570b5ebed7e085f5fc6ce7bfb6c302811cbfbd..624ad51f0c09d86244342c0ecb912dfedb1aa88b 100644 --- a/src/DataStructure/FloatImmutableSequence.php +++ b/src/DataStructure/FloatImmutableSequence.php @@ -25,7 +25,7 @@ class FloatImmutableSequence implements \IteratorAggregate, \Countable, \ArrayAc * Optional: callable with signature as: callback(string $a, string $b): int * or TRUE will sort using sort(). Defaults to FALSE: unsorted. */ - public function __construct(iterable $values, bool|callable $sort = FALSE) { + final public function __construct(iterable $values, bool|callable $sort = FALSE) { $this->values = []; foreach ($values as $value) { if (!(is_float($value))) { diff --git a/src/DataStructure/IntImmutableSequence.php b/src/DataStructure/IntImmutableSequence.php index f1091558c177f6fa106d4f169385e6a36dd6a4eb..717b9c9aa278ac7b58604f40992c93173beb7e6b 100644 --- a/src/DataStructure/IntImmutableSequence.php +++ b/src/DataStructure/IntImmutableSequence.php @@ -25,7 +25,7 @@ class IntImmutableSequence implements \IteratorAggregate, \Countable, \ArrayAcce * Optional: callable with signature as: callback(string $a, string $b): int * or TRUE will sort using sort(). Defaults to FALSE: unsorted. */ - public function __construct(iterable $values, bool|callable $sort = FALSE) { + final public function __construct(iterable $values, bool|callable $sort = FALSE) { $this->values = []; foreach ($values as $value) { if (!(is_int($value))) { diff --git a/src/DataStructure/ObjectImmutableSequence.php b/src/DataStructure/ObjectImmutableSequence.php index 6fbd7ec0f62995a6ea3aee373826277871dcd9bb..44adcf76c7f4fed211c98d6b2eaa0a39ebea93a4 100644 --- a/src/DataStructure/ObjectImmutableSequence.php +++ b/src/DataStructure/ObjectImmutableSequence.php @@ -25,7 +25,7 @@ class ObjectImmutableSequence implements \IteratorAggregate, \Countable, \ArrayA * Optional: callable with signature as: callback(string $a, string $b): int * or TRUE will sort using sort(). Defaults to FALSE: unsorted. */ - public function __construct(iterable $values, bool|callable $sort = FALSE) { + final public function __construct(iterable $values, bool|callable $sort = FALSE) { $this->values = []; foreach ($values as $value) { if (!(is_object($value))) { diff --git a/src/DataStructure/Set.php b/src/DataStructure/Set.php index 2a7b671a9627094694d9e2c1f175a049ce0249b7..6aa5d6a78b5d04fd848aad184f75ffa46e16491d 100644 --- a/src/DataStructure/Set.php +++ b/src/DataStructure/Set.php @@ -5,11 +5,9 @@ declare(strict_types = 1); namespace Drupal\data_structures\DataStructure; /** - * A data structure for sets. - * - * By definition, every element of a set is unique within the set. + * A general implementation for Set. */ -class Set implements \IteratorAggregate, \JsonSerializable, \Countable { +class Set implements SetInterface { /** * Internal storage for set members. @@ -24,17 +22,14 @@ class Set implements \IteratorAggregate, \JsonSerializable, \Countable { * @param iterable|null $values * Contains a variable number of values to add to the set. */ - public function __construct(?iterable $values) { + final public function __construct(?iterable $values) { if (!is_null($values)) { $this->add(...$values); } } /** - * Returns set data as an array. - * - * @return array - * The set values. + * {@inheritdoc} */ public function toArray(): array { return $this->members; @@ -59,34 +54,28 @@ class Set implements \IteratorAggregate, \JsonSerializable, \Countable { /** * {@inheritdoc} */ - public function jsonSerialize(): mixed { + public function jsonSerialize(): array { return $this->members; } /* ***** Maintaining the Set **** */ /** - * Empties the set. + * {@inheritdoc} */ public function empty(): void { $this->members = []; } /** - * Determines if the set is empty. - * - * @return bool - * True if the set is empty. + * {@inheritdoc} */ public function isEmpty(): bool { return empty($this->members); } /** - * Adds new values to the set if they are not already members. - * - * @param mixed ...$values - * A variable number of values to add to the set. + * {@inheritdoc} */ public function add(...$values): void { foreach ($values as $value) { @@ -97,10 +86,7 @@ class Set implements \IteratorAggregate, \JsonSerializable, \Countable { } /** - * Removes values from the set if they are members. - * - * @param mixed ...$values - * A variable number of values to remove from the set. + * {@inheritdoc} */ public function remove(mixed ...$values): void { $filterSet = new Set(NULL); @@ -112,13 +98,7 @@ class Set implements \IteratorAggregate, \JsonSerializable, \Countable { } /** - * Does the set have the value as a member?. - * - * @param mixed $value - * The value to check. - * - * @return bool - * True if the value is in the set + * {@inheritdoc} */ public function has(mixed $value):bool { $found = FALSE; @@ -132,27 +112,9 @@ class Set implements \IteratorAggregate, \JsonSerializable, \Countable { } /** - * Finds the union with another set. - * - * +----------+ - * |$this.....| - * |.....+-----------+ - * |.....|....|......| - * |.....|....|......| - * +-----|----+......| - * |...........| - * |......$set | - * +-----------+ - * - * Union combines the members of both sets. - * - * @param \Drupal\data_structures\DataStructure\Set $set - * The set to use in $this ∪ $set. - * - * @return \Drupal\data_structures\DataStructure\Set - * The intersection. + * {@inheritdoc} */ - public function union(Set $set): Set { + public function union(SetInterface $set): Set { $union = new \AppendIterator(); $union->append(new \IteratorIterator($this)); $union->append(new \IteratorIterator($set)); @@ -160,122 +122,46 @@ class Set implements \IteratorAggregate, \JsonSerializable, \Countable { } /** - * Finds the intersection with another set. - * - * +----------+ - * |$this | - * | +-----------+ - * | |....| | - * | |....| | - * +-----|----+ | - * | | - * | $set | - * +-----------+ - * - * Intersection finds the items that are members of both sets. - * - * @param \Drupal\data_structures\DataStructure\Set $set - * The set to use in $this ∩ $set. - * - * @return \Drupal\data_structures\DataStructure\Set - * The intersection. + * {@inheritdoc} */ - public function intersect(Set $set): Set { + public function intersect(SetInterface $set): Set { $filter = fn($value) => $set->has($value); $intersection = array_filter($this->members, $filter); return new Set($intersection); } /** - * Finds the difference with another set. - * - * +----------+ - * |$this.....| - * |.....+-----------+ - * |.....| | | - * |.....| | | - * +-----|----+ | - * | | - * | $set | - * +-----------+ - * - * Difference finds the items that are members of the first set but not the - * second. - * - * @param \Drupal\data_structures\DataStructure\Set $set - * The set to use in $this \ $set. - * - * @return \Drupal\data_structures\DataStructure\Set - * The intersection. + * {@inheritdoc} */ - public function difference(Set $set): Set { + public function difference(SetInterface $set): Set { $filter = fn($value) => !$set->has($value); $intersection = array_filter($this->members, $filter); return new Set($intersection); } /** - * Finds the symmetric difference with another set. - * - * +----------+ - * |$this.....| - * |.....+-----------+ - * |.....| |......| - * |.....| |......| - * +-----|----+......| - * |...........| - * |......$set | - * +-----------+ - * - * Symmetric difference finds the items that are members of both sets that - * are not also members of both sets. - * - * @param \Drupal\data_structures\DataStructure\Set $set - * The set to use in $this \ $set. - * - * @return \Drupal\data_structures\DataStructure\Set - * The intersection. + * {@inheritdoc} */ - public function symDifference(Set $set): Set { + public function symDifference(SetInterface $set): Set { return $this->difference($set)->union($set->difference($this)); } /** - * Applies a callable to re-map the set. - * - * @param callable $callable - * A callable that accepts a Set member and returns a new value. - * - * @return \Drupal\data_structures\DataStructure\Set - * The result of the mapping. + * {@inheritdoc} */ public function map(callable $callable): Set { return new static(array_map($callable, $this->members)); } /** - * Applies a callable to reduce the set to a single value. - * - * @param callable $callable - * The callable signature is: callback(mixed $carry, mixed $item): mixed. - * @param mixed $initial - * The initial value to pass as the "carry" value to the callable. - * - * @return mixed - * Returns the last return value from the callback. + * {@inheritdoc} */ public function reduce(callable $callable, mixed $initial = NULL): mixed { return array_reduce($this->members, $callable, $initial); } /** - * Applies a callable to filter the set into a new set. - * - * @param callable $callable - * The callable signature is: callback(mixed $item): bool. - * - * @return \Drupal\data_structures\DataStructure\Set - * The filtered sequence.. + * {@inheritdoc} */ public function filter(callable $callable): Set { return new static(array_filter($this->members, $callable)); diff --git a/src/DataStructure/SetInterface.php b/src/DataStructure/SetInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..07a175b96faded41bae0c8678ad07118c8c724b8 --- /dev/null +++ b/src/DataStructure/SetInterface.php @@ -0,0 +1,189 @@ +<?php + +namespace Drupal\data_structures\DataStructure; + +/** + * A data structure for sets. + * + * By definition, every element of a set is unique within the set. + */ +interface SetInterface extends \IteratorAggregate, \Countable, \JsonSerializable { + + /** + * Returns set data as an array. + * + * @return array + * The set values. + */ + public function toArray(): array; + + /** + * Empties the set. + */ + public function empty(): void; + + /** + * Determines if the set is empty. + * + * @return bool + * True if the set is empty. + */ + public function isEmpty(): bool; + + /** + * Adds new values to the set if they are not already members. + * + * @param mixed ...$values + * A variable number of values to add to the set. + */ + public function add(...$values): void; + + /** + * Removes values from the set if they are members. + * + * @param mixed ...$values + * A variable number of values to remove from the set. + */ + public function remove(mixed ...$values): void; + + /** + * Does the set have the value as a member?. + * + * @param mixed $value + * The value to check. + * + * @return bool + * True if the value is in the set + */ + public function has(mixed $value): bool; + + /** + * Finds the union with another set. + * + * +----------+ + * |$this.....| + * |.....+-----------+ + * |.....|....|......| + * |.....|....|......| + * +-----|----+......| + * |...........| + * |......$set | + * +-----------+ + * + * Union combines the members of both sets. + * + * @param \Drupal\data_structures\DataStructure\SetInterface $set + * The set to use in $this ∪ $set. + * + * @return \Drupal\data_structures\DataStructure\SetInterface + * The intersection. + */ + public function union(SetInterface $set): SetInterface; + + /** + * Finds the intersection with another set. + * + * +----------+ + * |$this | + * | +-----------+ + * | |....| | + * | |....| | + * +-----|----+ | + * | | + * | $set | + * +-----------+ + * + * Intersection finds the items that are members of both sets. + * + * @param \Drupal\data_structures\DataStructure\SetInterface $set + * The set to use in $this ∩ $set. + * + * @return \Drupal\data_structures\DataStructure\SetInterface + * The intersection. + */ + public function intersect(SetInterface $set): SetInterface; + + /** + * Finds the difference with another set. + * + * +----------+ + * |$this.....| + * |.....+-----------+ + * |.....| | | + * |.....| | | + * +-----|----+ | + * | | + * | $set | + * +-----------+ + * + * Difference finds the items that are members of the first set but not the + * second. + * + * @param \Drupal\data_structures\DataStructure\SetInterface $set + * The set to use in $this \ $set. + * + * @return \Drupal\data_structures\DataStructure\SetInterface + * The intersection. + */ + public function difference(SetInterface $set): SetInterface; + + /** + * Finds the symmetric difference with another set. + * + * +----------+ + * |$this.....| + * |.....+-----------+ + * |.....| |......| + * |.....| |......| + * +-----|----+......| + * |...........| + * |......$set | + * +-----------+ + * + * Symmetric difference finds the items that are members of both sets that + * are not also members of both sets. + * + * @param \Drupal\data_structures\DataStructure\SetInterface $set + * The set to use in $this \ $set. + * + * @return \Drupal\data_structures\DataStructure\SetInterface + * The intersection. + */ + public function symDifference(SetInterface $set): SetInterface; + + /** + * Applies a callable to re-map the set. + * + * @param callable $callable + * A callable that accepts a Set member and returns a new value. + * + * @return \Drupal\data_structures\DataStructure\SetInterface + * The result of the mapping. + */ + public function map(callable $callable): SetInterface; + + /** + * Applies a callable to reduce the set to a single value. + * + * @param callable $callable + * The callable signature is: callback(mixed $carry, mixed $item): mixed. + * @param mixed $initial + * The initial value to pass as the "carry" value to the callable. + * + * @return mixed + * Returns the last return value from the callback. + */ + public function reduce(callable $callable, mixed $initial = NULL): mixed; + + /** + * Applies a callable to filter the set into a new set. + * + * @param callable $callable + * The callable signature is: callback(mixed $item): bool. + * + * @return \Drupal\data_structures\DataStructure\SetInterface + * The filtered sequence.. + */ + public function filter(callable $callable): SetInterface; + +} diff --git a/src/DataStructure/StringImmutableSequence.php b/src/DataStructure/StringImmutableSequence.php index c56a4c7eb0dd10bb3d925696bb26ea97aa6c991f..07d0a191c10c93fcd122e70c30edfa1a023cdcc7 100644 --- a/src/DataStructure/StringImmutableSequence.php +++ b/src/DataStructure/StringImmutableSequence.php @@ -25,7 +25,7 @@ class StringImmutableSequence implements \IteratorAggregate, \Countable, \ArrayA * Optional: callable with signature as: callback(string $a, string $b): int * or TRUE will sort using natsort(). Defaults to FALSE: unsorted. */ - public function __construct(iterable $values, bool|callable $sort = FALSE) { + final public function __construct(iterable $values, bool|callable $sort = FALSE) { $this->values = []; foreach ($values as $value) { if (!(is_string($value))) { diff --git a/src/Drush/Generators/templates/map-immutable.php.twig b/src/Drush/Generators/templates/map-immutable.php.twig index d40182dfde5f2b46affce7b2bdcf877956aba988..78e6b6e4cf9f67cc82844cea24d086b25035080a 100644 --- a/src/Drush/Generators/templates/map-immutable.php.twig +++ b/src/Drush/Generators/templates/map-immutable.php.twig @@ -25,7 +25,7 @@ final class {{ class }} {{ '{' }} */ public function __construct( {% for property in properties %} - readonly {{ property.type}} ${{- property.name }}{{- not loop.last ? ',' }} + public readonly {{ property.type}} ${{- property.name }}{{- not loop.last ? ',' }} {% endfor %} ) {} diff --git a/src/Drush/Generators/templates/typed-sequence.php.twig b/src/Drush/Generators/templates/typed-sequence.php.twig index 1414b689a032422dff2a1ccd47f3a30a7a5fc896..d660be6398dc70effbfd7946337cfbef737e0925 100644 --- a/src/Drush/Generators/templates/typed-sequence.php.twig +++ b/src/Drush/Generators/templates/typed-sequence.php.twig @@ -29,7 +29,7 @@ class {{ class }} implements \IteratorAggregate, \Countable, \ArrayAccess, \Json * Optional: callable with signature as: callback(string $a, string $b): int * or TRUE will sort using sort(). Defaults to FALSE: unsorted. */ - public function __construct(iterable $values, bool|callable $sort = FALSE) { + final public function __construct(iterable $values, bool|callable $sort = FALSE) { $this->values = []; foreach ($values as $value) { {% if full is not empty %}