Skip to content

Nepovinný parametr #4

Description

@meridius

Ahoj,

řeším problém s nepovinným parametrem a zjištěním jeho stavu.
Mějme např. následující strukturu, kde otherData může být pole hodnot, ale může být i null nebo prázdné pole.

$data = [
	'id' => 42,
	'name' => 'Tomas Marny',
	'otherData' => [
		'data1' => 'foo',
		'data2' => 'bar',
	],
];

$data = [
	'id' => 42,
	'name' => 'Tomas Marny',
	'otherData' => [],
];

Jak v tomto případě zjistím stav "prázdnosti", resp. že otherData nemá žádné property, na které by se dalo přistupovat?
Pokud totiž entitu nadefinuji následovně, tak typ property otherData bude vždy OtherData, nehledě na vstup.

/**
 *
 * @property-read int id
 * @property-read string name
 * @property-read OtherData otherData
 */
class Data extends Entry
{
    protected static $associations = [
        'otherData' => OtherData::class,
    ];
}

/**
 *
 * @property-read string data1
 * @property-read string data2
 */
class OtherData extends Entry
{
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions