Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/Schematic/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ class Entry
const INDEX_ENTRYCLASS = 0;
const INDEX_MULTIPLICITY = 1;
const INDEX_EMBEDDING = 2;
const INDEX_NULLABLE = 3;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Konstanta INDEX_NULLABLE by měla být v doc pro self::$parsedAssociations.


/**
* @var array
*/
protected static $associations = [];

/**
* @var array entryClass => [INDEX_ENTRYCLASS => relatedEntryClass, INDEX_MULTIPLICITY => multiplicity, INDEX_EMBEDDING => embedding]
* @var array entryClass => [INDEX_ENTRYCLASS => relatedEntryClass, INDEX_MULTIPLICITY => multiplicity, INDEX_EMBEDDING => embedding, INDEX_NULLABLE => null value allowed]
*/
private static $parsedAssociations = [];

Expand Down Expand Up @@ -64,18 +65,19 @@ private static function parseAssociations($class)

foreach (static::$associations as $association => $entryClass) {
$matches = [];
$result = preg_match('#^([^.[\]]+)(\.[^.[\]]*)?(\[\])?$#', $association, $matches);
$result = preg_match('#^(\?)?([^.[\]]+)(\.[^.[\]]*)?(\[\])?$#', $association, $matches);

if ($result === 0 || (!empty($matches[2]) && !empty($matches[3]))) {
if ($result === 0 || (!empty($matches[3]) && !empty($matches[4]))) {
throw new InvalidArgumentException('Invalid association definition given: ' . $association);
}

self::$parsedAssociations[$class][$matches[1]] = [
self::$parsedAssociations[$class][$matches[2]] = [
self::INDEX_ENTRYCLASS => $entryClass,
self::INDEX_MULTIPLICITY => !empty($matches[3]),
self::INDEX_EMBEDDING => !empty($matches[2]) ?
($matches[2] === '.' ? $matches[1] . '_' : substr($matches[2], 1)) :
self::INDEX_MULTIPLICITY => !empty($matches[4]),
self::INDEX_EMBEDDING => !empty($matches[3]) ?
($matches[3] === '.' ? $matches[2] . '_' : substr($matches[3], 1)) :
FALSE,
self::INDEX_NULLABLE => !empty($matches[1]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady bych možná empty() nahradil výsledkem volání protected funkce isEmpty() (nebo tak nějak), aby si uživatel měl možnost nastavit, jaké hodnoty považuje za ty správně "empty". Dovedu si představit, že asi ne všem se trefíme do use-case.

];
}
}
Expand All @@ -101,7 +103,7 @@ public function __get($name)
$this->readEmbeddedEntry($association[self::INDEX_EMBEDDING]) :
$this->readData($name);

if ($data === NULL) {
if ($data === NULL || ($association[self::INDEX_NULLABLE] && static::isEmpty($data))) {
return $this->data[$name] = NULL;
}

Expand Down Expand Up @@ -130,6 +132,16 @@ public function __isset($name)
}


/**
* @param mixed $value
* @return bool
*/
protected static function isEmpty($value)
{
return empty($value);
}


/**
* @param string $prefix
* @return array|NULL
Expand Down
Loading