PHP code example of gowork / values
1. Go to this page and download the library: Download gowork/values library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gowork / values example snippets
use GW\Value\Wrap;
$arrayValue = Wrap::array(['a', 'b', 'c', 'a', 'd', 'f'])
->map(function (string $value): string {
return strtoupper($value)
})
->map('strtolower')
->filter(function (string $value): bool {
return $value !== 'd';
})
->sort(function (string $a, string $b): int {
return $a <=> $b;
})
->shuffle()
->reverse()
->unique()
->diff(Wrap::array(['d', 'f']))
->intersect(Wrap::array(['a', 'b', 'c']))
->join(Wrap::array(['g', 'h', 'i']))
->unshift('j')
->shift($j)
->push('l')
->pop($l)
->slice(0, 6)
->each(function (string $value): void {
echo $value;
});
$count = $arrayValue->count();
$reduced = $arrayValue->reduce(
function (string $reduced, string $value): string {
return $reduced . $value;
},
''
);
$stringValue = $arrayValue->implode(', ');
if (isset($arrayValue[0])) {
$first = $arrayValue[0];
}
$first = $arrayValue->first();
foreach ($arrayValue as $item) {
echo $item;
}
use \GW\Value\Wrap;
$assocValue = Wrap::assocArray(['a' => 1, 'b' => 2, 'c' => 3, 'x' => 0])
->with('d', 4)
->without('a', 'b')
->withoutElement(0)
->merge(Wrap::assocArray(['e' => 5, 'f' => 6]));
$keys = $assocValue->keys();
$withMappedKeys = $assocValue->mapKeys(function (string $key): string {
return strtoupper($key);
});
$aValue = $assocValue->get('a', $default = 1);
$hasA = $assocValue->has('a');
$associativeArray = $assocValue->toAssocArray();
$indexedArray = $assocValue->toArray();
use GW\Value\Wrap;
$stringValue = Wrap::string('just example string')
->trim()
->trimRight()
->trimLeft()
->lower()
->upper()
->lowerFirst()
->upperFirst()
->upperWords()
->padLeft(50, '-')
->padRight(100, '-')
->padBoth(200, '-')
->replace('no', 'yes')
->replacePattern('/\s/', '-')
->replacePatternCallback('/[\-]+/', function (array $match): string {
return '-';
})
->truncate(140)
->substring(0, 100)
->stripTags();
$hasExample = $stringValue->contains('example');
$firstA = $stringValue->position('a');
$lastA = $stringValue->positionLast('a');
$stringLength = $stringValue->length();
$primitiveString = $stringValue->toString();
$onlyLetters = $stringValue->isMatching('/^[a-z]+$/');
$stringsArray = $stringValue->explode(' ');
use \GW\Value\Wrap;
use \GW\Value\StringValue;
$stringsArray = Wrap::stringsArray(['one', ' two ', '<b>three</b>'])
// StringValue
->trim()
->stripTags()
->padLeft(16)
// ArrayValue
->unique()
->each(function (StringValue $value): void {
echo $value->toString();
});
use \GW\Value\Wrap;
$range = function (int $start, int $end) {
for ($i = $start; $i <= $end; $i++) {
yield $i;
}
};
$stringsArray = Wrap::iterable($range(0, 10))
->join(Wrap::iterable($range(400, 440000)))
->slice(10, 20)
->filter(function (int $value): bool {
return $value % 2 === 0;
})
->map(function (int $value): int {
return $value + 2;
})
->each(function (int $value): void {
echo $value . "\n";
});