Skip to content

Commit 4a66dbe

Browse files
committed
Merge branch 'collection'
2 parents 2ca0e29 + 12be89a commit 4a66dbe

34 files changed

+1121
-577
lines changed

src/Concept/LooselyTypedCollection.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Concept/TypedCollection.php

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,17 @@
44

55
use Lkrms\Concern\TCollection;
66
use Lkrms\Contract\ICollection;
7-
use Lkrms\Contract\IImmutable;
8-
use LogicException;
97

108
/**
11-
* Base class for collections of objects of a strictly enforced type
12-
*
13-
* Extend {@see LooselyTypedCollection} instead if performance concerns outweigh
14-
* type safety.
9+
* Base class for collections of items of a given type
1510
*
1611
* @template TKey of array-key
17-
* @template TValue of object
12+
* @template TValue
1813
*
1914
* @implements ICollection<TKey,TValue>
2015
*/
21-
abstract class TypedCollection implements ICollection, IImmutable
16+
abstract class TypedCollection implements ICollection
2217
{
23-
/**
24-
* @var class-string<TValue>
25-
*/
26-
protected const ITEM_CLASS = \stdClass::class;
27-
2818
/** @use TCollection<TKey,TValue> */
29-
use TCollection {
30-
push as private _push;
31-
unshift as private _unshift;
32-
offsetSet as private _offsetSet;
33-
}
34-
35-
/**
36-
* @param TValue[] $items
37-
*/
38-
public function __construct($items = [])
39-
{
40-
foreach ($items as $item) {
41-
if (!is_object($item) || !is_a($item, static::ITEM_CLASS)) {
42-
$this->throwItemTypeException($item);
43-
}
44-
}
45-
$this->Items = $items;
46-
}
47-
48-
public function push(...$item)
49-
{
50-
foreach ($item as $_item) {
51-
if (!is_object($_item) || !is_a($_item, static::ITEM_CLASS)) {
52-
$this->throwItemTypeException($_item);
53-
}
54-
}
55-
return $this->_push(...$item);
56-
}
57-
58-
public function unshift(...$item)
59-
{
60-
foreach ($item as $_item) {
61-
if (!is_object($_item) || !is_a($_item, static::ITEM_CLASS)) {
62-
$this->throwItemTypeException($_item);
63-
}
64-
}
65-
return $this->_unshift(...$item);
66-
}
67-
68-
public function offsetSet($offset, $value): void
69-
{
70-
if (!is_object($value) || !is_a($value, static::ITEM_CLASS)) {
71-
$this->throwItemTypeException($value);
72-
}
73-
$this->_offsetSet($offset, $value);
74-
}
75-
76-
/**
77-
* @param mixed $item
78-
* @return never
79-
*/
80-
private function throwItemTypeException($item)
81-
{
82-
throw new LogicException(sprintf(
83-
'Not a subclass of %s: %s',
84-
static::ITEM_CLASS,
85-
is_object($item) ? get_class($item) : gettype($item)
86-
));
87-
}
19+
use TCollection;
8820
}

src/Concept/TypedList.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lkrms\Concept;
4+
5+
use Lkrms\Concern\TList;
6+
use Lkrms\Contract\IList;
7+
8+
/**
9+
* Base class for lists of items of a given type
10+
*
11+
* @template TValue
12+
*
13+
* @implements IList<TValue>
14+
*/
15+
abstract class TypedList implements IList
16+
{
17+
/** @use TList<TValue> */
18+
use TList;
19+
}

src/Concern/HasMutator.php

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/Concern/Immutable.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lkrms\Concern;
4+
5+
trait Immutable
6+
{
7+
/**
8+
* Clone the object
9+
*
10+
* @return static
11+
*/
12+
protected function clone()
13+
{
14+
$clone = clone $this;
15+
return $clone;
16+
}
17+
18+
/**
19+
* Apply a value to a clone of the object if the current value differs
20+
*
21+
* @param mixed $value
22+
* @return static
23+
*/
24+
protected function withPropertyValue(string $property, $value)
25+
{
26+
if (isset($this->$property) && $value === $this->$property) {
27+
return $this;
28+
}
29+
30+
$clone = $this->clone();
31+
$clone->$property = $value;
32+
return $clone;
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lkrms\Concern;
4+
5+
use LogicException;
6+
7+
/**
8+
* @template TKey of array-key
9+
* @template TValue
10+
*/
11+
trait ImmutableArrayAccess
12+
{
13+
/**
14+
* @param TKey|null $offset
15+
* @param TValue $value
16+
*/
17+
public function offsetSet($offset, $value): void
18+
{
19+
throw new LogicException(sprintf('%s is immutable', static::class));
20+
}
21+
22+
/**
23+
* @param TKey $offset
24+
*/
25+
public function offsetUnset($offset): void
26+
{
27+
throw new LogicException(sprintf('%s is immutable', static::class));
28+
}
29+
}

0 commit comments

Comments
 (0)