Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data types LanguageMapOfStrings and LanguageMapOfLists
- Loading branch information
Showing
12 changed files
with
376 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JSKOS; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Language map of strings or lists. | ||
*/ | ||
abstract class LanguageMap extends Container | ||
{ | ||
|
||
/** | ||
* Create a new Language Map | ||
* | ||
* @param String|Array|Object JSON data to copy | ||
*/ | ||
public function __construct($data=null, bool $strict=false) | ||
{ | ||
// FIXME: this is a verbose copy of DataType::__construct | ||
if (is_string($data)) { | ||
$data = json_decode($data); | ||
} | ||
if (is_array($data) or is_object($data)) { | ||
foreach ($data as $key => $value) { | ||
$this->setField($key, $value, $strict); | ||
} | ||
} elseif ($data !== null) { | ||
throw new InvalidArgumentException( | ||
get_called_class() . | ||
' constructor expects array, object, or JSON string' | ||
); | ||
} | ||
} | ||
|
||
protected function setField($key, $value, $strict) | ||
{ | ||
try { | ||
$this->offsetSet($key, $value); | ||
} catch (InvalidArgumentException $e) { | ||
if ($strict) { | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Language maps are always closed world, so this does nothing. | ||
* @param Boolean $closed | ||
*/ | ||
public function setClosed(bool $closed = true) | ||
{ | ||
} | ||
|
||
|
||
/** | ||
* Set an value at the given language or language range. | ||
* @param mixed $lang | ||
* @param mixed $value | ||
*/ | ||
public function offsetSet($lang, $value) | ||
{ | ||
if (DataType::isLanguageOrRange($lang)) { | ||
if (is_null($value)) { | ||
unset($this->members[$lang]); | ||
} else { | ||
$this->members[$lang] = static::checkMember($value); | ||
} | ||
} else { | ||
throw new InvalidArgumentException( | ||
'JSKOS\LanguageMap may only use language tags or ranges as index' | ||
); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Unset a value with given language or language range. | ||
* @param mixed $lang | ||
*/ | ||
public function offsetUnset($lang) | ||
{ | ||
unset($this->members[$lang]); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JSKOS; | ||
|
||
use InvalidArgumentException; | ||
use TypeError; | ||
|
||
/** | ||
* Language map of strings. | ||
*/ | ||
class LanguageMapOfLists extends LanguageMap | ||
{ | ||
protected static function checkMember($value) | ||
{ | ||
if (is_object($value) and $value instanceof Listing) { | ||
return $value; | ||
} else { | ||
try { | ||
return new Listing($value); | ||
} catch (TypeError $e) { | ||
throw new InvalidArgumentException( | ||
'JSKOS\LanguageMapOfLists may only contain JSKOS\Listing' | ||
); | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Return a data structure to serialize this container as JSON. | ||
*/ | ||
public function jsonSerializeRoot($context=self::DEFAULT_CONTEXT) | ||
{ | ||
$map = new \stdClass(); | ||
foreach ($this->members as $lang => $list) { | ||
$map->$lang = $list->jsonSerializeRoot(null); | ||
} | ||
return $map; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JSKOS; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Language map of strings. | ||
*/ | ||
class LanguageMapOfStrings extends LanguageMap | ||
{ | ||
protected static function checkMember($value) | ||
{ | ||
if (is_string($value)) { | ||
return $value; | ||
} else { | ||
throw new InvalidArgumentException( | ||
'JSKOS\LanguageMapOfStrings may only contain strings' | ||
); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Return a data structure to serialize this container as JSON. | ||
*/ | ||
public function jsonSerializeRoot($context=self::DEFAULT_CONTEXT) | ||
{ | ||
return (object)$this->members; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.