/** * @param array $array * @param boolean $save_indexes = true * @return SplFixedArray, DataStructures\SerializableFixedArray */ public static function fromArray(array $array, $save_indexes = true) { if (self::$_useSpl === null) { self::_checkEnvironment(); } return self::$_useSpl ? \SplFixedArray::fromArray($array, $save_indexes) : new self(count($array), $save_indexes ? $array : array_values($array)); }
public function testMapTraversable() { $expected = [1, 2, 3, 4]; $arr = \SplFixedArray::fromArray($expected); $actual = t::into([], t::map(t::value()), $arr); $this->assertEquals($expected, $actual); }
public function testFromArrayDisordered() { $array = array(1 => 'foo', 3 => 'bar', 2 => 'baz', 0 => 'qux'); $the_DS = DynamicArray::fromArray($array); $SPL_DS = \SplFixedArray::fromArray($array); $this->assertMethodsEqual($SPL_DS, $the_DS); }
/** * Create a new instance. * * @param array $matchRegexps The regexes to match files. * * @param AbstractFilter[] $filters The filters to apply. */ public function __construct(array $matchRegexps, $filters) { foreach ($matchRegexps as $pattern) { $this->matchRegexps[] = $this->toRegex($pattern); } $this->filters = \SplFixedArray::fromArray($filters); }
/** * Create a new ImmutableVector from the given traversable. * @param array|Traversable $traversable * @param bool $preserveKeys * @return ImmutableVector */ public static function copyOf($traversable, $preserveKeys = true) { if (is_array($traversable)) { return new self(\SplFixedArray::fromArray($traversable, $preserveKeys)); } else { return new self(\SplFixedArray::fromArray(iterator_to_array($traversable), $preserveKeys)); } }
public function valuesDataProvider() { $splFixedArrayIn = \SplFixedArray::fromArray([2, 154, 2342, 1001, 7651, 4523, 1343, 756, 6324, 1]); $expected = [1, 2, 154, 756, 1001, 1343, 2342, 4523, 6324, 7651]; $splFixedArrayIn2 = clone $splFixedArrayIn; $splFixedArrayIn2[9] = 8000; $expected2 = [2, 154, 756, 1001, 1343, 2342, 4523, 6324, 7651, 8000]; return [[$splFixedArrayIn, $expected], [$splFixedArrayIn2, $expected2]]; }
public function __construct(array $list = array()) { array_map(function ($item) { if (!$item instanceof FFMpegThumbnailer) { throw new \InvalidArgumentException('Excpecting list of FFMpegThumbnailer\\FFMpegThumbnailer objects only'); } }, $list); $this->attaches = \SplFixedArray::fromArray($list, false); }
/** * @param int|string $start * @param int|string $length * @return $this */ public function slice($start, $length) { $end = $this->set->getSize(); if ($start > $end || $length > $end) { throw new \RuntimeException('Invalid start or length'); } $this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length)); return $this; }
/** * Imports a PHP array in a FixedArray instance. * * This method needs to be reimplemented as SplFixedArray does not return `new static`. * @see https://bugs.php.net/bug.php?id=55128 * * Subclasses of FixedArray do not need to reimplement this method. * * @param array $array * @param boolean $saveIndexes * * @return FixedArray * * @throws \InvalidArgumentException If the array contains non-numeric or negative indexes. */ public static function fromArray($array, $saveIndexes = true) { $splFixedArray = \SplFixedArray::fromArray($array, $saveIndexes); $result = new static($splFixedArray->count()); $source = $saveIndexes ? $array : $splFixedArray; foreach ($source as $key => $value) { $result[$key] = $value; } return $result; }
/** * * @param PointInterface[] $points * @param LineFactoryInterface $lineFactory */ public function __construct(array $points, LineFactoryInterface $lineFactory) { $items = []; for ($i = 0; $i < count($points) - 1;) { $items[] = $lineFactory->createLineSegment($points[$i], $points[++$i]); } // and add last segment $items[] = $lineFactory->createLineSegment($points[$i], $points[0]); $this->items = \SplFixedArray::fromArray($items); }
public function testItReceivesAResultWhenBodyFollowsResponse() { $command = $this->createCommandInstance(); $response = $this->getMockBuilder('Rvdv\\Nntp\\Response\\MultiLineResponse')->disableOriginalConstructor()->getMock(); $lines = \SplFixedArray::fromArray(['Lorem ipsum dolor sit amet, ', 'consectetur adipiscing elit. ', 'Sed volutpat sit amet leo sit amet sagittis.']); $response->expects($this->once())->method('getLines')->will($this->returnValue($lines)); $command->onBodyFollows($response); $result = $command->getResult(); $this->assertEquals(implode("\r\n", $lines->toArray()), $result); }
public function testWhenThereIsAFailedReport() { $report_mock = \Mockery::mock('\\SimpleHealth\\EndpointReport'); $report_mock->pass = false; $report_mock->message = StringLiteral::fromNative(''); $reports = new Collection(\SplFixedArray::fromArray([$report_mock])); $subject = new NodeValidator(); $report = $subject->isValid($reports); $this->assertEquals($report->pass, false); }
/** * This list only accepts an array of integers, or throw an exception with invalid type. * * @param int[] $ids * @throws \InvalidArgumentException */ public function __construct(array $ids) { foreach ($ids as $key => $id) { $type = gettype($id); if ('integer' !== $type) { throw new \InvalidArgumentException(sprintf('The array of IDs can only contain integers. Item of type %s given at the offset %s', $type, $key)); } } $this->list = \SplFixedArray::fromArray($ids, false); }
/** * Hand constructor. * @param Card[] ...$cards */ public function __construct(Card ...$cards) { if (count($cards) > 5) { throw new \BadMethodCallException('max 5 cards. Found: ' . count($cards)); } usort($cards, function (Card $a, Card $b) { $faceValueDifference = $b->getFaceValue() - $a->getFaceValue(); return $faceValueDifference === 0 ? $b->getSuit()->getSuit() - $a->getSuit()->getSuit() : $faceValueDifference; }); $this->cards = \SplFixedArray::fromArray($cards, false); }
public function build() { $endpoints = \SplFixedArray::fromArray($this->endpoints); for ($i = 0, $len = count($endpoints); $i < $len; $i++) { $endpoints[$i] = Url::fromNative($endpoints[$i]); } $endpoints = new Collection($endpoints); $node_healthcheck_factory = new NodeHealthCheckFactory(); $node_healthcheck = $node_healthcheck_factory->make($endpoints); return new SimpleHealth($endpoints, $node_healthcheck); }
/** * insert * * @param array/string $data * @access public * @return void */ public function insert($data) { if (is_array($data)) { $data = \SplFixedArray::fromArray($data); } if (!$data instanceof \Iterator) { $data = \SplFixedArray::fromArray([$data]); } $transformedList = $this->transformer->transform($data); return $this->writer->insert($transformedList); }
/** * @return SplFixedArray */ public function parse($fileId) { $fileId = new MongoId($fileId); $file = $this->Gridfs->readFile('csv', $fileId); $Data = str_getcsv($file, "\n", "\""); foreach ($Data as &$Row) { $Row = str_getcsv($Row, ","); } //parse the items in rows $spl = SplFixedArray::fromArray($Data); return $spl; }
public function testCacheGet() { $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); $cacheMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface(); $cacheMock->getMockController()->has = true; $cacheMock->getMockController()->get = $this->getSerializedResponse(new Response(200, [], "The answer is 42", '1.1', 'OK')); $cacheMock->getMockController()->ttl = 256; $this->if($testedClass = new TestedClass(['handle_factory' => $curlFactoryMock]))->and($testedClass->setCache($cacheMock, 500, false))->and($testedClass->setDebug(true))->and($request = new Request('GET', 'http://httpbin.org'))->then->object($response = $testedClass($request, [])->wait())->isInstanceOf('GuzzleHttp\\Psr7\\Response')->integer($response->getStatuscode())->isEqualTo(200)->string($response->getReasonPhrase())->isEqualTo('OK')->string($response->getBody()->__toString())->isEqualTo('The answer is 42')->boolean($response->cached)->isTrue()->integer($response->cacheTtl)->isEqualTo(256)->mock($cacheMock)->call('get')->withArguments(md5($request->getMethod() . $request->getUri()))->once()->call('ttl')->withArguments(md5($request->getMethod() . $request->getUri()))->once()->mock($curlFactoryMock)->call('release')->never(); // Test unserialize issue $cacheMock->getMockController()->get = serialize(\SplFixedArray::fromArray([null, null, null, null, null])); $this->object($response = $testedClass($request, [])->wait())->isInstanceOf('GuzzleHttp\\Psr7\\Response')->integer($response->getStatuscode())->isEqualTo(200)->boolean(isset($response->cached) ? $response->cached : false)->isFalse()->mock($cacheMock)->call('get')->withArguments(md5($request->getMethod() . $request->getUri()))->twice()->call('ttl')->withArguments(md5($request->getMethod() . $request->getUri()))->once()->mock($curlFactoryMock)->call('release')->once(); }
public function testSameValueAs() { $array = \SplFixedArray::fromArray(array(new StringLiteral('one'), new StringLiteral('two'), new Integer(3))); $collection2 = new Collection($array); $array = \SplFixedArray::fromArray(array('one', 'two', array(1, 2))); $collection3 = Collection::fromNative($array); $this->assertTrue($this->collection->sameValueAs($collection2)); $this->assertTrue($collection2->sameValueAs($this->collection)); $this->assertFalse($this->collection->sameValueAs($collection3)); $mock = $this->getMock('EmbarkNow\\ValueObjects\\ValueObjectInterface'); $this->assertFalse($this->collection->sameValueAs($mock)); }
/** * Returns a new Collection object * * @param \SplFixedArray $array * @return self */ public static function fromNative() { $array = \func_get_arg(0); $items = array(); foreach ($array as $item) { if ($item instanceof \Traversable || \is_array($item)) { $items[] = static::fromNative($item); } else { $items[] = new StringLiteral(\strval($item)); } } $fixedArray = \SplFixedArray::fromArray($items); return new static($fixedArray); }
/** * Note: angle will be construct from given points anticlockwise on Cartesian 2D system! * Geometry library, by default, use full positive circle for present angle value: from 0 to 360 degrees. * For some cases, angle can be greater than 180, not negative! * * For example: if points presents as [A, B, C] (middle point is vertex), angle size is 270 deg for this case * B>____C * | * | 270 * | * ^A * * For create rigth (90) angle you must allow this order: * * A * | * | 90 * | * B>_____C * * Or this: * * * * * @param PointInterface[] $points * @param AngleFactoryInterface $angleFactory */ public function __construct(array $points, AngleFactoryInterface $angleFactory) { if (count($points) < 3) { throw new Exceptions\DomainException("For build set of Angles need more that 3 points"); } $items = []; // and proceed edge cases $items[] = $angleFactory->createAngleByPoints($points[1], $points[0], $points[count($points) - 1]); for ($i = 0; $i < count($points) - 2; $i++) { $items[] = $angleFactory->createAngleByPoints($points[$i + 2], $points[$i + 1], $points[$i]); } $items[] = $angleFactory->createAngleByPoints($points[0], $points[count($points) - 1], $points[count($points) - 2]); $this->items = \SplFixedArray::fromArray($items); }
public function test() { $validator_mock = \Mockery::mock('\\SimpleHealth\\ValidatorInterface'); $heatlhcheck_factory_mock = \Mockery::mock('\\SimpleHealth\\EndpointHealthCheckFactory'); $healthcheck_mock = \Mockery::mock('\\SimpleHealth\\EndpointHealthCheck'); $endpoint_report_mock = \Mockery::mock('\\SimpleHealth\\EndpointReport'); $node_valid_mock = \Mockery::mock('\\SimpleHealth\\ValidatorResult'); $node_valid_mock->pass = true; $validator_mock->shouldReceive('isValid')->andReturn($node_valid_mock); $heatlhcheck_factory_mock->shouldReceive('make')->andReturn($healthcheck_mock); $healthcheck_mock->shouldReceive('check')->andReturn($endpoint_report_mock); $endpoints = new Collection(SplFixedArray::fromArray([Url::fromNative('http://www.php.net/')])); $subject = new NodeHealthCheck($endpoints, $validator_mock, $heatlhcheck_factory_mock); $node_report = $subject->check(); $this->assertEquals($node_report->pass, true); }
public static function copyOf($array, $newLength) { if (is_array($array)) { return self::copyOfFixedArray(\SplFixedArray::fromArray($array), $newLength)->toArray(); } if ($array instanceof \SplFixedArray) { return self::copyOfFixedArray($array, $newLength); } if ($array instanceof String) { return self::copyOfString($array, $newLength); } if (is_string($array)) { return (string) self::copyOfString(new String($array), $newLength); } return null; }
/** * Returns a new Dictionary object. * * @param array $array * * @return self */ public static function fromNative() { $array = \func_get_arg(0); $keyValuePairs = array(); foreach ($array as $arrayKey => $arrayValue) { $key = new StringLiteral(\strval($arrayKey)); if ($arrayValue instanceof \Traversable || \is_array($arrayValue)) { $value = Collection::fromNative($arrayValue); } else { $value = new StringLiteral(\strval($arrayValue)); } $keyValuePairs[] = new KeyValuePair($key, $value); } $fixedArray = \SplFixedArray::fromArray($keyValuePairs); return new self($fixedArray); }
<?php $array = SplFixedArray::fromArray(array(1 => 1, 2 => '2', 3 => false)); var_dump($array);
/** * @return SplFixedArray */ public function parse($fileId = null) { if (!is_null($fileId)) { $this->__construct($fileId); } $Data = str_getcsv($this->file, "\n", "\""); foreach ($Data as &$Row) { $Row = str_getcsv($Row, ","); } //parse the items in rows $spl = SplFixedArray::fromArray($Data); return $spl; }
public function __clone() { $this->set = \SplFixedArray::fromArray(array_map(function (TransactionInterface $tx) { return clone $tx; }, $this->set->toArray())); }
public function shuffle() { $tmpPopulation = $this->population->toArray(); shuffle($tmpPopulation); $this->population = SplFixedArray::fromArray($tmpPopulation); }
<?php /** * Not efficient enough Timeout * */ $handle = fopen("php://stdin", "r"); while ('0 0' != ($what = trim(fgets($handle)))) { $nk = $what; list($n, $k) = explode(' ', $nk); $middle = floor($n / 2); $pumpkins1 = trim(fgets($handle)); $pumpkins = explode(' ', $pumpkins1); $pumpkinsSpl = SplFixedArray::fromArray($pumpkins); unset($pumpkins); for ($watering = 1; $watering <= $k; $watering++) { $st = trim(fgets($handle)); list($s, $t) = explode(' ', $st); for ($wateredPumpkin = $s - 1; $wateredPumpkin <= $t - 1; $wateredPumpkin++) { $pumpkinsSpl[$wateredPumpkin] += 1; } $sortedArray = $pumpkinsSpl->toArray(); sort($sortedArray); echo $sortedArray[$middle] . "\n"; } }
public static function init() { if (!self::$list instanceof \SplFixedArray) { //self::$list = new \SplFixedArray(65536); self::$list = \SplFixedArray::fromArray(self::$list, true); } for ($i = 0; $i < 256; ++$i) { if (Block::$list[$i] !== null) { self::$list[$i] = Block::$list[$i]; } } self::initCreativeItems(); }