示例#1
0
 /**
  * Initializes a new instance of that class.
  *
  * @param IEnumerable $sequence The base sequence.
  * @param callable $selector The selector for the sort values to use.
  * @param bool $preventKeys Prevent keys or not.
  */
 public function __construct(IEnumerable $sequence, callable $selector, callable $comparer)
 {
     $this->_comparer = $comparer;
     $this->_selector = $selector;
     $this->_sequence = $sequence;
     parent::__construct($sequence);
     $this->resetMe(false);
 }
示例#2
0
 /**
  * Initializes a new instance of that class.
  *
  * @param IEnumerable $grps The sequence of groupings.
  *
  * @throws \System\ArgumentException
  */
 public function __construct($grps)
 {
     $grps = static::asIterator($grps, true);
     $dict = new Dictionary(null, null, null, \sprintf('$x => $x instanceof %s', IGrouping::class));
     while ($grps->valid()) {
         /* @var IGrouping $curGrouping */
         $curGrouping = $grps->current();
         $dict->add($curGrouping->key(), $curGrouping);
         $grps->next();
     }
     parent::__construct($dict);
 }
示例#3
0
 public function test1Iterator()
 {
     $seq = Enumerable::create(new ArrayIterator([1, 2, 3]));
     $items1 = static::sequenceToArray($seq);
     $items2 = static::sequenceToArray($seq->reset());
     $this->assertEquals(count($items1), count($items2));
     foreach ($items1 as $key => $value) {
         $this->assertTrue(isset($items2[$key]));
         $this->assertTrue($items2[$key] === $value);
         $this->assertTrue($items2[$key] === $items1[$key]);
     }
 }
示例#4
0
 public function testRange3()
 {
     foreach ($this->createRangeIncreaseByFuncs() as $increaseByFunc) {
         $seq = Enumerable::range(2, 3, $increaseByFunc);
         $items = static::sequenceToArray($seq);
         $this->assertEquals(3, count($items));
         $this->assertTrue(isset($items[0]));
         $this->assertSame(2, $items[0]);
         $this->assertTrue(isset($items[1]));
         $this->assertSame(2.5, $items[1]);
         $this->assertTrue(isset($items[2]));
         $this->assertSame(3.0, $items[2]);
     }
 }
示例#5
0
 public function testIterator()
 {
     $this->executeForPersonSequence(function (array $persons) {
         return Enumerable::create(new ArrayIterator($persons));
     });
 }
示例#6
0
 /**
  * Creates a list of sequences from an array.
  *
  * @param array $arr The array with the data for each sequence.
  *
  * @return IEnumerable[] The list of sequences.
  */
 protected static function sequenceListFromArray(array $arr = []) : array
 {
     return [static::sequenceFromArray($arr), Enumerable::create(new \ArrayIterator($arr)), Enumerable::create(static::generatorFromArray($arr))];
 }
示例#7
0
 public function testPrependFormatArray()
 {
     $createGenerator = function () {
         (yield 'ys');
         (yield 'Mk');
         (yield null);
     };
     $str1 = $this->createInstance('xyz');
     $str2 = $this->checkTransformMethod(function (IString $str) {
         return $str->prependFormatArray('{2}{0}{3}{1}', [1, 2.0, '3.0', '4']);
     }, '3.0142xyz', $str1);
     $str3 = $this->checkTransformMethod(function (IString $str) use($createGenerator) {
         return $str->prependFormatArray('   {0}{1}{2}  {4}{3}{5} ', $createGenerator(), Enumerable::create($createGenerator())->select('$x => \\strtoupper($x)')->reverse());
     }, '   ysMk  MKYS 3.0142xyz', $str2);
 }
示例#8
0
 /**
  * {@inheritDoc}
  */
 public final function enumerateFiles() : IEnumerable
 {
     $path = (string) $this->_fullName;
     return Enumerable::create(\scandir($path))->select(function ($x) use($path) {
         if (ClrString::isNullOrWhitespace($x)) {
             return null;
         }
         if ('.' === \trim($x)) {
             return null;
         }
         if ('..' === \trim($x)) {
             return null;
         }
         $fullPath = \realpath($path . \DIRECTORY_SEPARATOR . $x);
         if (false === $fullPath) {
             return null;
         }
         $scopePath = $fullPath;
         if (@\is_link($scopePath)) {
             $link = @\readlink($scopePath);
             if (false !== $link) {
                 $scopePath = $link;
             }
         }
         if (!\is_file($scopePath)) {
             return null;
         }
         return $fullPath;
     })->ofType('string')->select('$x => new \\System\\IO\\FileInfo($x)');
 }
示例#9
0
 public function syntax(Language $lang = null, $linePrefix = '')
 {
     $doc = $this->docBlock();
     $visibility = null;
     if (!$this->reflector()->getDeclaringClass()->isInterface()) {
         if ($this->reflector()->isPublic()) {
             $visibility = 'public ';
         } else {
             if ($this->reflector()->isProtected()) {
                 $visibility = 'protected ';
             } else {
                 if ($this->reflector()->isPrivate()) {
                     $visibility = 'private ';
                 }
             }
         }
     }
     $isStatic = null;
     if ($this->reflector()->isStatic()) {
         $isStatic = 'static ';
     }
     $isAbstract = null;
     if (!$this->reflector()->getDeclaringClass()->isInterface()) {
         if ($this->reflector()->isAbstract()) {
             $isAbstract = 'abstract ';
         }
     }
     $isFinal = null;
     if ($this->reflector()->isFinal()) {
         $isFinal = 'final ';
     }
     $syntax = \sprintf('%s%s%s%s%sfunction %s(', $linePrefix, $isAbstract, $visibility, $isStatic, $isFinal, $this->reflector()->getName());
     /* @var ParamTag[] $paramTags */
     $paramTags = $doc->getTagsByName('param');
     $findParamTag = function (\ReflectionParameter $rp) use($paramTags) {
         $paramName = '$' . $rp->getName();
         $result = null;
         foreach ($paramTags as $pt) {
             if (\trim($pt->getVariableName()) === $paramName) {
                 $result = $pt;
             }
         }
         return $result;
     };
     $parameters = Enumerable::create($this->reflector()->getParameters())->select(function (\ReflectionParameter $x, IIndexedItemContext $ctx) use($findParamTag, $linePrefix, $syntax) {
         $result = !$ctx->isFirst() ? "\n" . \str_repeat(' ', \strlen($syntax)) : '';
         $tag = $findParamTag($x);
         if ($tag instanceof ParamTag) {
             $types = Enumerable::create($tag->getTypes())->select('$x => \\trim($x)')->distinct('($x, $y) => \\strtolower($x) === \\strtolower($y)')->toArray();
             if (!empty($types)) {
                 $result .= \implode(' | ', $types) . ' ';
             }
         }
         $result .= '$' . $x->getName();
         if ($x->isOptional()) {
             $result .= ' = ' . \var_export($x->getDefaultValue(), true);
         }
         return $result;
     })->joinToString(',');
     $syntax .= \sprintf('%s)', $parameters);
     $syntax .= ';';
     return $syntax;
 }
示例#10
0
 public function testKeyValidators()
 {
     foreach ($this->createKeyValidators() as $keyValidator) {
         unset($dict1);
         unset($dict2);
         unset($thrownEx1);
         unset($thrownEx2);
         try {
             $dict1 = Enumerable::create(['a' => 111, 'b' => 2.7, 'C' => '33three'])->toDictionary(null, $keyValidator);
         } catch (\Exception $ex) {
             $thrownEx1 = $ex;
         }
         $this->assertTrue(isset($dict1));
         $this->assertFalse(isset($thrownEx1));
         $this->assertInstanceOf(IDictionary::class, $dict1);
         $this->assertEquals(3, count($dict1));
         $this->checkForExpectedValues($dict1->keys()->asResettable(), ['a', 'b', 'C']);
         $this->checkForExpectedValues($dict1->values()->asResettable(), [111, 2.7, '33three']);
         try {
             $dict2 = Enumerable::create(['a' => 111, 2 => 3, 'C' => '33three'])->toDictionary(null, $keyValidator);
         } catch (\Exception $ex) {
             $thrownEx2 = $ex;
         }
         $this->assertFalse(isset($dict2));
         $this->assertTrue(isset($thrownEx2));
         $this->assertInstanceOf(InvalidKeyException::class, $thrownEx2);
     }
 }
示例#11
0
 public function syntax(Language $lang = null, $linePrefix = '')
 {
     $classType = $this->classType($lang);
     $syntax = $linePrefix . $classType . ' ' . $this->reflector()->getShortName() . " {\n";
     Enumerable::create($this->methods())->each(function (Method_ $x, IEachItemContext $ctx) use($lang, &$syntax) {
         if (!$ctx->isFirst()) {
             $syntax .= "\n";
         }
         $syntax .= $x->syntax($lang, '    ') . "\n";
     });
     $syntax .= "}";
     return $syntax;
 }
示例#12
0
 /**
  * {@inheritDoc}
  */
 public final function split($delimiter, $limit = null) : IEnumerable
 {
     $delimiter = static::valueToString($delimiter, false);
     $str = $this->_wrappedValue;
     if (\func_num_args() < 2) {
         $result = Enumerable::create(\explode($delimiter, $str));
     } else {
         $result = Enumerable::create(\explode($delimiter, $str, $limit));
     }
     return $result->select(static::format('$x => new \\{0}($x)', static::class));
 }
示例#13
0
 /**
  * @return static[]
  */
 public static function fromNameArray($names, Language $lang = null, Project $proj = null)
 {
     $rc = new \ReflectionClass(static::class);
     $fn = $rc->getMethod('fromName')->getClosure(null);
     return Enumerable::create($names)->select('$x => \\trim($x)')->where('$x => "" !== $x')->distinct('($x) => \\strtolower($x)')->select(function ($x) use($fn, $lang, $proj) {
         return $fn($x, $lang, $proj);
     })->ofType($rc)->toArray();
 }