commerceguys / zone Goto Github PK
View Code? Open in Web Editor NEWA PHP 5.5+ zone management library.
License: MIT License
A PHP 5.5+ zone management library.
License: MIT License
This package requires the dev-master
version of commerceguys/addressing
- I therefore cannot install version 0.5
of that package:
Can the composer.json
requirement be changed to ~0.5
(for stable releases) or *
(any version)?
See also:
RoyalMail defines several zones, with "World zone 1" defined as "anything which isn't in any of the other zones.".
The data can be seen here: http://cgit.drupalcode.org/rmzone/tree/rmzone.inc
We need a way to represent this.
We should have a ZoneMember that can take one or more zones, and returns TRUE if the referenced zone(s) don't match.
Question How to name this ZoneMember? Maybe expand the existing ZoneMemberZone to support multiple zones & a negate option?
If you try:
PostalCodeHelper::buildList('5:7,10:12')
you'll get something like:
[6,7,10,11,12]
and "5" will be missing because of the mix of foreach
, array_merge
and unset($postalCodeList[$index])
.
$postalCodeList = explode(',', $postalCodes);
foreach ($postalCodeList as $index => &$postalCodeItem) {
$postalCodeItem = trim($postalCodeItem);
if (strpos($postalCodeItem, ':') !== false) {
$postalCodeRange = explode(':', $postalCodeItem);
if (is_numeric($postalCodeRange[0]) && is_numeric($postalCodeRange[1])) {
$postalCodeRange = range($postalCodeRange[0], $postalCodeRange[1]);
$postalCodeList = array_merge($postalCodeList, $postalCodeRange);
}
unset($postalCodeList[$index]);
}
}
Here is my solution:
/**
* Builds a list of postal codes from the provided string.
*
* Expands any ranges into full values (e.g. "1:3" becomes "1, 2, 3").
*
* @param string $postalCodes The postal codes.
*
* @return array The list of postal codes.
*/
protected static function buildList($postalCodes)
{
$resultList = [];
$postalCodeList = explode(',', $postalCodes);
foreach ($postalCodeList as $index => $postalCodeItem) {
$postalCodeItem = trim($postalCodeItem);
if (strpos($postalCodeItem, ':') !== false) {
$postalCodeRange = explode(':', $postalCodeItem);
if (is_numeric($postalCodeRange[0]) && is_numeric($postalCodeRange[1])) {
$postalCodeRange = range($postalCodeRange[0], $postalCodeRange[1]);
$resultList = array_merge($resultList, $postalCodeRange);
}
continue;
}
$resultList[] = $postalCodeItem;
}
return $resultList;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.