├── .editorconfig ├── .gitignore ├── .pullapprove.yml ├── .scrutinizer.yml ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── Dictionary.php ├── DictionaryInterface.php ├── ImmutableException.php ├── ListInterface.php ├── OrderedList.php ├── Set.php ├── SetInterface.php ├── SortedDictionary.php ├── StructureInterface.php ├── Traits │ ├── CanAccess.php │ ├── CanArray.php │ ├── CanCompare.php │ ├── CanCount.php │ ├── CanIterate.php │ ├── CanSerialize.php │ ├── CanSerializeJson.php │ ├── CanStructure.php │ └── CanValidate.php ├── UnorderedList.php └── ValidationException.php └── tests ├── ArrayRecursionTest.php ├── DictionaryTest.php ├── OrderedListTest.php ├── SetTest.php ├── SimilarTest.php ├── SortedDictionaryTest.php ├── StructureTest.php └── UnorderedListTest.php /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | 11 | # PHP/JS Formatting 12 | [*.{php,js,json}] 13 | charset = utf-8 14 | trim_trailing_whitespace = true 15 | indent_style = space 16 | indent_size = 4 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | composer.lock 3 | vendor/ 4 | build/ 5 | -------------------------------------------------------------------------------- /.pullapprove.yml: -------------------------------------------------------------------------------- 1 | author_approval: ignored 2 | approve_by_comment: true 3 | reset_on_push: true 4 | approve_regex: '^:\+1:' 5 | reject_regex: '^:\-1:' 6 | reviewers: 7 | - 8 | name: stack 9 | required: 1 10 | members: 11 | - ameech 12 | - dolfelt 13 | - shadowhand 14 | - elazar 15 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | excluded_paths: [tests/*] 3 | 4 | checks: 5 | php: 6 | remove_extra_empty_lines: true 7 | remove_php_closing_tag: true 8 | remove_trailing_whitespace: true 9 | fix_use_statements: 10 | remove_unused: true 11 | preserve_multiple: false 12 | preserve_blanklines: true 13 | order_alphabetically: true 14 | fix_php_opening_tag: true 15 | fix_linefeed: true 16 | fix_line_ending: true 17 | fix_identation_4spaces: true 18 | fix_doc_comments: true 19 | 20 | tools: 21 | external_code_coverage: 22 | timeout: 600 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.5 5 | - 5.6 6 | - 7.0 7 | - hhvm 8 | 9 | matrix: 10 | include: 11 | - php: 5.5 12 | env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"' 13 | 14 | before_script: 15 | - travis_retry composer self-update 16 | - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source 17 | 18 | script: 19 | - vendor/bin/phpunit 20 | 21 | after_script: 22 | - bash -c '[[ -f "build/logs/clover.xml" ]] && wget https://scrutinizer-ci.com/ocular.phar' 23 | - bash -c '[[ -f "build/logs/clover.xml" ]] && php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml' 24 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | This project adheres to [Semantic Versioning](http://semver.org/). 5 | 6 | **1.1.0** 7 | 8 | - Make `toArray` operate recursively to flatten nested structures. PR#1 9 | 10 | **1.0.0** 11 | 12 | - Initial release 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Woody Gilk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Equip Structure 2 | 3 | [![Latest Stable Version](https://img.shields.io/packagist/v/equip/structure.svg)](https://packagist.org/packages/equip/structure) 4 | [![License](https://img.shields.io/packagist/l/equip/structure.svg)](https://github.com/equip/structure/blob/master/LICENSE) 5 | [![Build Status](https://travis-ci.org/equip/structure.svg)](https://travis-ci.org/equip/structure) 6 | [![Code Coverage](https://scrutinizer-ci.com/g/equip/structure/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/equip/structure/?branch=master) 7 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/equip/structure/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/equip/structure/?branch=master) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/destrukt/destrukt.svg)](https://packagist.org/packages/destrukt/destrukt) 9 | 10 | Provides a number of common data structures in [Equip](http://equip.github.io/) 11 | that are not natively supported by PHP. Each structure is represented by an 12 | immutable object that can be counted and serialized to JSON. All of the structures 13 | can be used as [iterators][php-iterator] and [arrays][php-arrayaccess], but cannot 14 | be modified using array functions. 15 | 16 | [php-iterator]: http://php.net/manual/en/class.iterator.php 17 | [php-arrayaccess]: http://php.net/manual/en/class.arrayaccess.php 18 | 19 | For more information, see [the documentation][docs]. 20 | 21 | [docs]: http://equipframework.readthedocs.org/en/latest/structure 22 | 23 | This package is compliant with [PSR-1][], [PSR-2][], and [PSR-4][]. If you notice 24 | compliance oversights, please send a patch via pull request. 25 | 26 | [PSR-1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md 27 | [PSR-2]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md 28 | [PSR-4]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md 29 | 30 | ## Structures 31 | 32 | **`Dictionary`** is an implementation of a [associative array][wiki-dict] that 33 | stores values identified by a key. Only associative arrays can be used to 34 | initialize the structure. Any value can be defined by a string key. 35 | 36 | **`SortedDictionary`** is an implementation of a [associative array][wiki-dict] 37 | that also sorts the array. When the dictionary is modified it will be sorted. 38 | By default the [`asort`][php-asort] function is used. 39 | 40 | **`OrderedList`** is an implementation of a [list][wiki-list] that stores ordered 41 | values. Only an indexed array can be used to initialize the structure. Any value 42 | can be added. When the list is modified it will be sorted. By default the 43 | [`sort`][php-sort] function will be used. 44 | 45 | **`UnorderedList`** is an implementation of a [list][wiki-list] that stores 46 | unordered values. The same value may appear more than once. Only an indexed array 47 | can be used to initialize the structure. Any value can be added. 48 | 49 | **`Set`** is an implementation of a [set][wiki-set] that stores a unique values. 50 | The same value will *not* appear more than once. Only an indexed array can be used 51 | to initialize the structure. Adding an existing value to the set will have no effect. 52 | A set also also be added to before or after an existing element. 53 | 54 | [wiki-dict]: https://en.wikipedia.org/wiki/Associative_array 55 | [wiki-list]: https://en.wikipedia.org/wiki/List_(abstract_data_type) 56 | [wiki-set]: https://en.wikipedia.org/wiki/Set_(abstract_data_type) 57 | 58 | [php-sort]: http://php.net/sort 59 | [php-asort]: http://php.net/asort 60 | 61 | ## Requirements 62 | 63 | The following versions of PHP are supported. 64 | 65 | * PHP 5.5 66 | * PHP 5.6 67 | * PHP 7.0 68 | * HHVM 69 | 70 | ## Install 71 | 72 | Via Composer 73 | 74 | ```bash 75 | $ composer require equip/structure 76 | ``` 77 | 78 | ## License 79 | 80 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 81 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "equip/structure", 3 | "description": "Simple, immutable data structures", 4 | "keywords": [ 5 | "data structure", 6 | "immutable", 7 | "hash map", 8 | "hash table", 9 | "dictionary", 10 | "ordered list", 11 | "unordered list", 12 | "list", 13 | "set" 14 | ], 15 | "license": "MIT", 16 | "authors": [ 17 | { 18 | "name": "Equip Contributors", 19 | "homepage": "https://github.com/equip" 20 | } 21 | ], 22 | "require": { 23 | "php": ">=5.5" 24 | }, 25 | "require-dev": { 26 | "phpunit/phpunit": "^4.8|^5.0" 27 | }, 28 | "replace": { 29 | "shadowhand/destrukt": "self.version", 30 | "destruktphp/destrukt": "self.version" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "Equip\\Structure\\": "src" 35 | } 36 | }, 37 | "autoload-dev": { 38 | "psr-4": { 39 | "Equip\\Structure\\": "tests" 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/Dictionary.php: -------------------------------------------------------------------------------- 1 | hasValue($key)) { 14 | return $this->values[$key]; 15 | } 16 | 17 | return $default; 18 | } 19 | 20 | public function hasValue($key) 21 | { 22 | return array_key_exists($key, $this->values); 23 | } 24 | 25 | public function withValues(array $values) 26 | { 27 | if ($this->values === $values) { 28 | return $this; 29 | } 30 | 31 | $this->assertValid($values); 32 | 33 | $copy = clone $this; 34 | $copy->values = $values; 35 | 36 | return $copy; 37 | } 38 | 39 | public function withValue($key, $value) 40 | { 41 | if ($this->hasValue($key) && $this->getValue($key) === $value) { 42 | return $this; 43 | } 44 | 45 | $this->assertValid([$key => $value]); 46 | 47 | $copy = clone $this; 48 | $copy->values[$key] = $value; 49 | 50 | return $copy; 51 | } 52 | 53 | public function withoutValue($key) 54 | { 55 | if (!$this->hasValue($key)) { 56 | return $this; 57 | } 58 | 59 | $copy = clone $this; 60 | unset($copy->values[$key]); 61 | 62 | return $copy; 63 | } 64 | 65 | protected function assertValid(array $values) 66 | { 67 | if (empty($values)) { 68 | return; 69 | } 70 | 71 | $keys = array_keys($values); 72 | $vals = array_values($values); 73 | 74 | if ($keys === array_keys($vals)) { 75 | throw ValidationException::invalid( 76 | 'Dictionary values must have distinct keys' 77 | ); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/DictionaryInterface.php: -------------------------------------------------------------------------------- 1 | values, true); 14 | } 15 | 16 | public function withValues(array $values) 17 | { 18 | if ($this->values === $values) { 19 | return $this; 20 | } 21 | 22 | $this->assertValid($values); 23 | 24 | $copy = clone $this; 25 | $copy->values = $values; 26 | $copy->sortValues(); 27 | 28 | return $copy; 29 | } 30 | 31 | public function withValue($value) 32 | { 33 | $this->assertValid([$value]); 34 | 35 | $copy = clone $this; 36 | $copy->values[] = $value; 37 | $copy->sortValues(); 38 | 39 | return $copy; 40 | } 41 | 42 | public function withoutValue($value) 43 | { 44 | $key = array_search($value, $this->values, true); 45 | 46 | if ($key === false) { 47 | return $this; 48 | } 49 | 50 | $copy = clone $this; 51 | unset($copy->values[$key]); 52 | $copy->sortValues(); 53 | 54 | return $copy; 55 | } 56 | 57 | protected function sortValues() 58 | { 59 | sort($this->values, SORT_REGULAR); 60 | } 61 | 62 | protected function assertValid(array $values) 63 | { 64 | if (empty($values)) { 65 | return; 66 | } 67 | 68 | if ($values !== array_values($values)) { 69 | throw ValidationException::invalid( 70 | 'List structures cannot have distinct keys' 71 | ); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Set.php: -------------------------------------------------------------------------------- 1 | values, true); 14 | } 15 | 16 | public function withValues(array $values) 17 | { 18 | $this->assertValid($values); 19 | 20 | $copy = clone $this; 21 | $copy->values = array_unique($values, SORT_REGULAR); 22 | 23 | return $copy; 24 | } 25 | 26 | public function withValue($value) 27 | { 28 | if ($this->hasValue($value)) { 29 | return $this; 30 | } 31 | 32 | $this->assertValid([$value]); 33 | 34 | $copy = clone $this; 35 | $copy->values[] = $value; 36 | 37 | return $copy; 38 | } 39 | 40 | public function withoutValue($value) 41 | { 42 | $key = array_search($value, $this->values, true); 43 | 44 | if ($key === false) { 45 | return $this; 46 | } 47 | 48 | $copy = clone $this; 49 | unset($copy->values[$key]); 50 | 51 | return $copy; 52 | } 53 | 54 | public function withValueAfter($value, $search) 55 | { 56 | if ($this->hasValue($value)) { 57 | return $this; 58 | } 59 | 60 | $this->assertValid([$value]); 61 | 62 | $copy = clone $this; 63 | 64 | $key = array_search($search, $this->values); 65 | if ($key === false) { 66 | array_push($copy->values, $value); 67 | } else { 68 | array_splice($copy->values, $key + 1, 0, $value); 69 | } 70 | 71 | return $copy; 72 | } 73 | 74 | public function withValueBefore($value, $search) 75 | { 76 | if ($this->hasValue($value)) { 77 | return $this; 78 | } 79 | 80 | $this->assertValid([$value]); 81 | 82 | $copy = clone $this; 83 | 84 | $key = array_search($search, $this->values); 85 | if ($key === false) { 86 | array_unshift($copy->values, $value); 87 | } else { 88 | array_splice($copy->values, $key, 0, $value); 89 | } 90 | 91 | return $copy; 92 | } 93 | 94 | protected function assertValid(array $values) 95 | { 96 | if (empty($values)) { 97 | return; 98 | } 99 | 100 | if ($values !== array_values($values)) { 101 | throw ValidationException::invalid( 102 | 'Set structures cannot have distinct keys' 103 | ); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/SetInterface.php: -------------------------------------------------------------------------------- 1 | sortChanged( 10 | parent::withValues($values) 11 | ); 12 | } 13 | 14 | public function withValue($key, $value) 15 | { 16 | return $this->sortChanged( 17 | parent::withValue($key, $value) 18 | ); 19 | } 20 | 21 | public function withoutValue($key) 22 | { 23 | return $this->sortChanged( 24 | parent::withoutValue($key) 25 | ); 26 | } 27 | 28 | /** 29 | * Sorts values, respecting keys. 30 | * 31 | * @return void 32 | */ 33 | protected function sortValues() 34 | { 35 | asort($this->values); 36 | } 37 | 38 | /** 39 | * Sorts the dictionary if it is not the same. 40 | * 41 | * @param SortedDictionary $copy 42 | * 43 | * @return SortedDictionary 44 | */ 45 | private function sortChanged(SortedDictionary $copy) 46 | { 47 | if ($copy !== $this) { 48 | $copy->sortValues(); 49 | } 50 | 51 | return $copy; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/StructureInterface.php: -------------------------------------------------------------------------------- 1 | values[$offset]); 12 | } 13 | 14 | public function offsetGet($offset) 15 | { 16 | return $this->values[$offset]; 17 | } 18 | 19 | public function offsetSet($offset, $value) 20 | { 21 | throw ImmutableException::cannotModify(get_class($this)); 22 | } 23 | 24 | public function offsetUnset($offset) 25 | { 26 | throw ImmutableException::cannotModify(get_class($this)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Traits/CanArray.php: -------------------------------------------------------------------------------- 1 | toArray(); 14 | } 15 | return $value; 16 | }, $this->values); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Traits/CanCompare.php: -------------------------------------------------------------------------------- 1 | values); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Traits/CanIterate.php: -------------------------------------------------------------------------------- 1 | values); 10 | } 11 | 12 | public function key() 13 | { 14 | return key($this->values); 15 | } 16 | 17 | public function next() 18 | { 19 | next($this->values); 20 | } 21 | 22 | public function rewind() 23 | { 24 | reset($this->values); 25 | } 26 | 27 | public function valid() 28 | { 29 | $key = $this->key(); 30 | return isset($this->values[$key]); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Traits/CanSerialize.php: -------------------------------------------------------------------------------- 1 | values); 12 | } 13 | 14 | public function unserialize($values) 15 | { 16 | $values = unserialize($values); 17 | $this->assertValid($values); 18 | $this->values = $values; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Traits/CanSerializeJson.php: -------------------------------------------------------------------------------- 1 | toArray(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Traits/CanStructure.php: -------------------------------------------------------------------------------- 1 | assertValid($values); 24 | $this->values = $values; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Traits/CanValidate.php: -------------------------------------------------------------------------------- 1 | values, true); 14 | } 15 | 16 | public function withValues(array $values) 17 | { 18 | $this->assertValid($values); 19 | 20 | if ($this->values === $values) { 21 | return $this; 22 | } 23 | 24 | $copy = clone $this; 25 | $copy->values = $values; 26 | 27 | return $copy; 28 | } 29 | 30 | public function withValue($value) 31 | { 32 | $this->assertValid([$value]); 33 | 34 | $copy = clone $this; 35 | $copy->values[] = $value; 36 | 37 | return $copy; 38 | } 39 | 40 | public function withoutValue($value) 41 | { 42 | $key = array_search($value, $this->values, true); 43 | 44 | if ($key === false) { 45 | return $this; 46 | } 47 | 48 | $copy = clone $this; 49 | unset($copy->values[$key]); 50 | 51 | return $copy; 52 | } 53 | 54 | protected function assertValid(array $values) 55 | { 56 | if (empty($values)) { 57 | return; 58 | } 59 | 60 | if ($values !== array_values($values)) { 61 | throw ValidationException::invalid( 62 | 'List structures cannot have distinct keys' 63 | ); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/ValidationException.php: -------------------------------------------------------------------------------- 1 | new OrderedList($books_by_lee), 23 | 'Laura Ingles Wilder' => new OrderedList($books_by_wilder), 24 | ]); 25 | 26 | $expect = [ 27 | 'Harper Lee' => $books_by_lee, 28 | 'Laura Ingles Wilder' => $books_by_wilder, 29 | ]; 30 | 31 | $this->assertSame($expect, $books->toArray()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/DictionaryTest.php: -------------------------------------------------------------------------------- 1 | struct = new Dictionary([ 17 | 'one' => 1, 18 | 'two' => 2, 19 | 'three' => 3, 20 | 'four' => 4, 21 | ]); 22 | } 23 | 24 | public function testGetValue() 25 | { 26 | $this->assertSame(1, $this->struct->getValue('one')); 27 | $this->assertSame(2, $this->struct->getValue('two')); 28 | 29 | // Default value is null 30 | $this->assertNull($this->struct->getValue('fuzzy')); 31 | 32 | // Can also be specified 33 | $this->assertSame(false, $this->struct->getValue('fuzzy', false)); 34 | } 35 | 36 | public function testHasValue() 37 | { 38 | $this->assertTrue($this->struct->hasValue('one')); 39 | $this->assertTrue($this->struct->hasValue('four')); 40 | $this->assertFalse($this->struct->hasValue('fuzzy')); 41 | 42 | // Null values can be checked 43 | $copy = $this->struct->withValue('wuzzy', null); 44 | $this->assertTrue($copy->hasValue('wuzzy')); 45 | } 46 | 47 | public function testWithValues() 48 | { 49 | $values = [ 50 | 'zero' => '0', 51 | 'pi' => '3.1416', 52 | ]; 53 | 54 | $copy = $this->struct->withValues($values); 55 | 56 | $this->assertNotSame($this->struct, $copy); 57 | $this->assertSame($values, $copy->toArray()); 58 | 59 | // Setting the same values should not copy 60 | $another = $copy->withValues($values); 61 | $this->assertSame($copy, $another); 62 | } 63 | 64 | public function testWithValuesInvalid() 65 | { 66 | $this->setExpectedException(ValidationException::class); 67 | 68 | $copy = $this->struct->withValues([1, 2, 3]); 69 | } 70 | 71 | public function testWithValue() 72 | { 73 | $this->assertArrayNotHasKey('five', $this->struct); 74 | 75 | $copy = $this->struct->withValue('five', 5); 76 | 77 | $this->assertNotSame($this->struct, $copy); 78 | $this->assertArrayHasKey('five', $copy); 79 | 80 | // Setting the same values should not copy 81 | $another = $copy->withValue('five', 5); 82 | $this->assertSame($copy, $another); 83 | } 84 | 85 | public function testWithoutValue() 86 | { 87 | $this->assertArrayHasKey('one', $this->struct); 88 | 89 | $copy = $this->struct->withoutValue('one'); 90 | 91 | $this->assertNotSame($this->struct, $copy); 92 | $this->assertArrayNotHasKey('one', $copy); 93 | 94 | // Removing the a non-exisistant value should not copy 95 | $another = $copy->withoutValue('one'); 96 | $this->assertSame($copy, $another); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /tests/OrderedListTest.php: -------------------------------------------------------------------------------- 1 | struct = new OrderedList([ 17 | 'red', 18 | 'green', 19 | 'blue', 20 | 'black', 21 | 'white', 22 | 'green', // duplicate 23 | ]); 24 | } 25 | 26 | public function testHasValue() 27 | { 28 | $this->assertTrue($this->struct->hasValue('red')); 29 | $this->assertTrue($this->struct->hasValue('black')); 30 | $this->assertFalse($this->struct->hasValue('yellow')); 31 | } 32 | 33 | public function testWithValues() 34 | { 35 | $values = [ 36 | 'ant', 37 | 'fly', 38 | ]; 39 | 40 | $copy = $this->struct->withValues($values); 41 | 42 | $this->assertNotSame($this->struct, $copy); 43 | $this->assertSame($values, $copy->toArray()); 44 | 45 | // If the values are exactly the same, nothing changes 46 | $another = $copy->withValues($values); 47 | $this->assertSame($copy, $another); 48 | } 49 | 50 | public function testWithValuesNotUnique() 51 | { 52 | $values = [ 53 | 'ant', 54 | 'bee', 55 | 'bee', 56 | ]; 57 | 58 | $copy = $this->struct->withValues($values); 59 | 60 | $this->assertSame($values, $copy->toArray()); 61 | $this->assertNotSame(array_unique($values), $copy->toArray()); 62 | } 63 | 64 | public function testWithValuesSort() 65 | { 66 | $values = [ 67 | 'orange', 68 | 'apple', 69 | 'banana', 70 | ]; 71 | 72 | $copy = $this->struct->withValues($values); 73 | 74 | $this->assertNotSame($values, $copy->toArray()); 75 | 76 | sort($values); 77 | $this->assertSame($values, $copy->toArray()); 78 | } 79 | 80 | public function testWithValuesInvalid() 81 | { 82 | $this->setExpectedException(ValidationException::class); 83 | 84 | $copy = $this->struct->withValues([ 85 | 'color' => 'magenta', 86 | ]); 87 | } 88 | 89 | public function testWithValue() 90 | { 91 | $copy = $this->struct->withValue('butterfly'); 92 | 93 | $this->assertNotSame($this->struct, $copy); 94 | $this->assertFalse($this->struct->hasValue('butterfly')); 95 | $this->assertTrue($copy->hasValue('butterfly')); 96 | 97 | // If the value already exists, it is still appended 98 | $another = $copy->withValue('butterfly'); 99 | $this->assertNotSame($copy, $another); 100 | } 101 | 102 | public function testWithoutValue() 103 | { 104 | $copy = $this->struct->withoutValue('blue'); 105 | 106 | $this->assertNotSame($this->struct, $copy); 107 | $this->assertTrue($this->struct->hasValue('blue')); 108 | $this->assertFalse($copy->hasValue('blue')); 109 | 110 | // If the value does not exist, nothing changes 111 | $same = $copy->withoutValue('blue'); 112 | $this->assertSame($copy, $same); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /tests/SetTest.php: -------------------------------------------------------------------------------- 1 | struct = new Set([ 17 | 'red', 18 | 'green', 19 | 'blue', 20 | 'black', 21 | 'white', 22 | ]); 23 | } 24 | 25 | public function testHasValue() 26 | { 27 | $this->assertTrue($this->struct->hasValue('red')); 28 | $this->assertTrue($this->struct->hasValue('black')); 29 | $this->assertFalse($this->struct->hasValue('yellow')); 30 | } 31 | 32 | public function testWithValues() 33 | { 34 | $values = [ 35 | 'ant', 36 | 'fly', 37 | ]; 38 | 39 | $copy = $this->struct->withValues($values); 40 | 41 | $this->assertNotSame($this->struct, $copy); 42 | $this->assertSame($values, $copy->toArray()); 43 | } 44 | 45 | public function testWithValuesUnique() 46 | { 47 | $values = [ 48 | 'bee', 49 | 'wasp', 50 | 'hornet', 51 | 'bee', 52 | ]; 53 | 54 | $copy = $this->struct->withValues($values); 55 | 56 | $this->assertNotSame($this->struct, $copy); 57 | $this->assertSame(array_unique($values), $copy->toArray()); 58 | } 59 | 60 | public function testWithValuesInvalid() 61 | { 62 | $this->setExpectedException(ValidationException::class); 63 | 64 | $copy = $this->struct->withValues([ 65 | 'color' => 'magenta', 66 | ]); 67 | } 68 | 69 | public function testWithValue() 70 | { 71 | $copy = $this->struct->withValue('butterfly'); 72 | 73 | $this->assertNotSame($this->struct, $copy); 74 | $this->assertFalse($this->struct->hasValue('butterfly')); 75 | $this->assertTrue($copy->hasValue('butterfly')); 76 | 77 | // If the value already exists, nothing changes 78 | $same = $copy->withValue('butterfly'); 79 | $this->assertSame($copy, $same); 80 | } 81 | 82 | public function testWithoutValue() 83 | { 84 | $copy = $this->struct->withoutValue('blue'); 85 | 86 | $this->assertNotSame($this->struct, $copy); 87 | $this->assertTrue($this->struct->hasValue('blue')); 88 | $this->assertFalse($copy->hasValue('blue')); 89 | 90 | // If the value does not exist, nothing changes 91 | $same = $copy->withoutValue('blue'); 92 | $this->assertSame($copy, $same); 93 | } 94 | 95 | public function testWithValueAfter() 96 | { 97 | $set = new Set; 98 | 99 | $s1 = 1; 100 | $s2 = 2; 101 | $s3 = 3; 102 | $s4 = 4; 103 | 104 | $set = $set->withValue($s1); 105 | $this->assertSame([$s1], $set->toArray()); 106 | 107 | $set = $set->withValueAfter($s2, $s1); 108 | $this->assertSame([$s1, $s2], $set->toArray()); 109 | 110 | $set = $set->withValueAfter($s3, $s1); 111 | $this->assertSame([$s1, $s3, $s2], $set->toArray()); 112 | 113 | // Nothing should change, s1 is already in the set 114 | $set = $set->withValueAfter($s1, $s3); 115 | $this->assertSame([$s1, $s3, $s2], $set->toArray()); 116 | 117 | // Value does not exist, it should append 118 | $set = $set->withValueAfter($s4, 0); 119 | $this->assertSame([$s1, $s3, $s2, $s4], $set->toArray()); 120 | } 121 | 122 | public function testWithValueBefore() 123 | { 124 | $set = new Set; 125 | 126 | $s1 = 1; 127 | $s2 = 2; 128 | $s3 = 3; 129 | $s4 = 4; 130 | 131 | $set = $set->withValue($s1); 132 | $this->assertSame([$s1], $set->toArray()); 133 | 134 | $set = $set->withValueBefore($s2, $s1); 135 | $this->assertSame([$s2, $s1], $set->toArray()); 136 | 137 | $set = $set->withValueBefore($s3, $s1); 138 | $this->assertSame([$s2, $s3, $s1], $set->toArray()); 139 | 140 | // Nothing should change, s1 is already in the set 141 | $set = $set->withValueBefore($s1, $s3); 142 | $this->assertSame([$s2, $s3, $s1], $set->toArray()); 143 | 144 | // Value does not exist, it should prepend 145 | $set = $set->withValueBefore($s4, 0); 146 | $this->assertSame([$s4, $s2, $s3, $s1], $set->toArray()); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /tests/SimilarTest.php: -------------------------------------------------------------------------------- 1 | assertSame($similar, $primary->isSimilar($secondary)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/SortedDictionaryTest.php: -------------------------------------------------------------------------------- 1 | struct = new SortedDictionary([ 10 | 'one' => 1, 11 | 'two' => 2, 12 | 'three' => 3, 13 | 'four' => 4, 14 | ]); 15 | } 16 | 17 | public function testSorting() 18 | { 19 | $values = $this->struct->toArray(); 20 | 21 | asort($values); 22 | 23 | $this->assertSame($values, $this->struct->toArray()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/StructureTest.php: -------------------------------------------------------------------------------- 1 | 'thing', 23 | 'bar' => 'widget', 24 | ], 25 | 2, 26 | ], 27 | [ 28 | Set::class, 29 | [ 30 | 'red', 31 | 'green', 32 | 'blue', 33 | ], 34 | 3, 35 | ], 36 | [ 37 | OrderedList::class, 38 | [ 39 | 'ant', 40 | 'bee', 41 | 'butterfly', 42 | 'caterpiller', 43 | 'fly', 44 | ], 45 | 5, 46 | ], 47 | [ 48 | UnorderedList::class, 49 | [ 50 | 'ant', 51 | 'caterpiller', 52 | 'butterfly', 53 | 'bee', 54 | 'fly', 55 | ], 56 | 5, 57 | ], 58 | ]; 59 | } 60 | 61 | /** 62 | * @dataProvider dataStructures 63 | */ 64 | public function testStructure($class, $values, $count) 65 | { 66 | $struct = new $class($values); 67 | 68 | $this->assertStructure($struct); 69 | $this->assertArrayAccess($struct, $values); 70 | $this->assertArrayConversion($struct, $values); 71 | $this->assertComparison($struct, $class); 72 | $this->assertCount($count, $struct); 73 | $this->assertCountEmpty($struct); 74 | $this->assertIteration($struct); 75 | $this->assertSerialization($struct); 76 | $this->assertJsonSerialization($struct); 77 | } 78 | 79 | private function assertStructure($struct) 80 | { 81 | $this->assertInstanceOf(StructureInterface::class, $struct); 82 | } 83 | 84 | private function assertArrayAccess($struct, array $values) 85 | { 86 | $this->assertInstanceOf(ArrayAccess::class, $struct); 87 | 88 | $key = key($values); 89 | $this->assertTrue(isset($struct[$key])); 90 | $this->assertSame($struct[$key], $values[$key]); 91 | 92 | // We cannot use setExpectedException() for the following tests because 93 | // the thrown exception aborts the rest of the assertions. 94 | 95 | try { 96 | $struct[$values] = false; 97 | $this->fail('ImmutableException was not thrown by set'); 98 | } catch (Exception $e) { 99 | $this->assertInstanceOf(ImmutableException::class, $e); 100 | } 101 | 102 | try { 103 | unset($struct[$key]); 104 | $this->fail('ImmutableException was not thrown by unset'); 105 | } catch (Exception $e) { 106 | $this->assertInstanceOf(ImmutableException::class, $e); 107 | } 108 | } 109 | 110 | private function assertArrayConversion($struct, $values) 111 | { 112 | $this->assertSame($values, $struct->toArray()); 113 | } 114 | 115 | private function assertComparison($struct, $class) 116 | { 117 | $this->assertTrue($struct->isSimilar(new $class)); 118 | $this->assertFalse($struct->isSimilar($this->getMock(StructureInterface::class))); 119 | } 120 | 121 | private function assertCountEmpty($struct) 122 | { 123 | $struct = $struct->withValues([]); 124 | $this->assertCount(0, $struct); 125 | } 126 | 127 | private function assertIteration($struct) 128 | { 129 | $this->assertInstanceOf(Iterator::class, $struct); 130 | 131 | $values = \iterator_to_array($struct); 132 | $this->assertSame($values, $struct->toArray()); 133 | } 134 | 135 | private function assertSerialization($struct) 136 | { 137 | $this->assertInstanceOf(Serializable::class, $struct); 138 | 139 | $copy = unserialize(serialize($struct)); 140 | $this->assertSame($struct->toArray(), $copy->toArray()); 141 | } 142 | 143 | private function assertJsonSerialization($struct) 144 | { 145 | $this->assertInstanceOf(JsonSerializable::class, $struct); 146 | 147 | $json = json_encode($struct); 148 | $this->assertJson($json); 149 | 150 | $copy = json_decode($json, true); 151 | $this->assertSame($copy, $struct->toArray()); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /tests/UnorderedListTest.php: -------------------------------------------------------------------------------- 1 | struct = new UnorderedList([ 17 | 'red', 18 | 'green', 19 | 'blue', 20 | 'black', 21 | 'white', 22 | 'green', // duplicate 23 | ]); 24 | } 25 | 26 | public function testHasValue() 27 | { 28 | $this->assertTrue($this->struct->hasValue('red')); 29 | $this->assertTrue($this->struct->hasValue('black')); 30 | $this->assertFalse($this->struct->hasValue('yellow')); 31 | } 32 | 33 | public function testWithValues() 34 | { 35 | $values = [ 36 | 'ant', 37 | 'fly', 38 | ]; 39 | 40 | $copy = $this->struct->withValues($values); 41 | 42 | $this->assertNotSame($this->struct, $copy); 43 | $this->assertSame($values, $copy->toArray()); 44 | 45 | // If the values are exactly the same, nothing changes 46 | $another = $copy->withValues($values); 47 | $this->assertSame($copy, $another); 48 | } 49 | 50 | public function testWithValuesNotUnique() 51 | { 52 | $values = [ 53 | 'ant', 54 | 'bee', 55 | 'ant', 56 | ]; 57 | 58 | $copy = $this->struct->withValues($values); 59 | 60 | $this->assertSame($values, $copy->toArray()); 61 | $this->assertNotSame(array_unique($values), $copy->toArray()); 62 | } 63 | 64 | public function testWithValuesInvalid() 65 | { 66 | $this->setExpectedException(ValidationException::class); 67 | 68 | $copy = $this->struct->withValues([ 69 | 'color' => 'magenta', 70 | ]); 71 | } 72 | 73 | public function testWithValue() 74 | { 75 | $copy = $this->struct->withValue('butterfly'); 76 | 77 | $this->assertNotSame($this->struct, $copy); 78 | $this->assertFalse($this->struct->hasValue('butterfly')); 79 | $this->assertTrue($copy->hasValue('butterfly')); 80 | 81 | // If the value already exists, it is still appended 82 | $another = $copy->withValue('butterfly'); 83 | $this->assertNotSame($copy, $another); 84 | } 85 | 86 | public function testWithoutValue() 87 | { 88 | $copy = $this->struct->withoutValue('blue'); 89 | 90 | $this->assertNotSame($this->struct, $copy); 91 | $this->assertTrue($this->struct->hasValue('blue')); 92 | $this->assertFalse($copy->hasValue('blue')); 93 | 94 | // If the value does not exist, nothing changes 95 | $same = $copy->withoutValue('blue'); 96 | $this->assertSame($copy, $same); 97 | } 98 | } 99 | --------------------------------------------------------------------------------