key = $key !== null ? $key : (string) $this; if ($this->key === null) { throw new \InvalidArgumentException('Object cannot be created without assigning a key to it'); } } /** * @param string $key * @return $this */ public function setKey($key) { $this->key = $key; return $this; } /** * Implements JsonSerializable interface. * * @return array */ public function jsonSerialize() { return ['key' => $this->getKey(), 'objects' => $this->toArray()]; } }