Created
July 5, 2019 04:47
-
-
Save zonuexe/fe1950f1c2d3c77d76a6c9e3535ee238 to your computer and use it in GitHub Desktop.
シリアライズできる任意の値をキーにできる配列っぽいオブジェクト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright 2019 USAMI Kenta <[email protected]> | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* シリアライズできる任意の値をキーにできる配列っぽいオブジェクト | |
* | |
* @copylight 2019 USAMI Kenta <[email protected]> | |
* @license MIT | |
*/ | |
class MyArray implements IteratorAggregate, ArrayAccess, Serializable, Countable | |
{ | |
private $a = []; | |
public function __construct(array $input = []) | |
{ | |
foreach ($input as $k => $v) { | |
$this[$k] = $v; | |
} | |
} | |
public function getIterator() | |
{ | |
return (function () { | |
foreach ($this->a as $k => $v) { | |
yield unserialize($k) => $v; | |
} | |
})(); | |
} | |
public function offsetExists($offset) | |
{ | |
return isset($this->a[self::key($offset)]); | |
} | |
public function offsetGet($offset) | |
{ | |
return $this->a[self::key($offset)]; | |
} | |
public function offsetSet($offset, $value) | |
{ | |
$this->a[self::key($offset)] = $value; | |
} | |
public function offsetUnset($offset) | |
{ | |
unset($this->a[self::key($offset)]); | |
} | |
public function count() | |
{ | |
return count($this->a); | |
} | |
public function serialize() | |
{ | |
return serialize($this->a); | |
} | |
public function unserialize($serialized) | |
{ | |
$this->a = unserialize($serialized); | |
} | |
private function key($offset) | |
{ | |
// 配列キーのキャストを模倣する | |
if (is_string($offset) || is_string($offset)) { | |
$offset = array_keys([$offset => null])[0]; | |
} | |
return serialize($offset); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment