Multidimensional array with polymorphic relation (tree structure) - php

To understand what I want to do, here a simple expain : I want to create templates.
One "Template" has a "Panel"
This "Panel" has some "Item" and another "Panel"
We have to do this step by step until the last "Item"
I want something like this :
- Template
- Panel "Section"
- Panel "Slider"
- Panel "Slide 1"
- Item "Text 1"
- Item "Text 2"
- Item "Button"
- Panel "Slide 2"
- Panel "Buttons"
- Item "Button 1"
- Item "Button 2"
- Item "Text"
With the polymorphic relations from Laravel, I have 3 Models which are "Panel", "Item" and "PanelContent" (this one has some attributesn "parent_id", "contentable_id and "contentable_type" usefull to know which Panel is the parent, and which Item or Panel will be the content.
Obvisouly, my Models contains some function usefull to get contents and parent.
What I did
Currently, I use a recursive function to get Panel's children to build step by step an array with the structure I shown at the beginning.
private function getChildren(PanelContent $panelContent, string $parentName, string $key): void
{
$this->result['template']['global_panel'][$parentName][$key] = $panelContent->contentable->toArray();
if ($panelContent->contentable_type === 'panel') {
foreach($panelContent->contentable->contents as $key => $container) {
$this->getChildren($container, $container->contentable->name, $key);
}
}
}
What I want
I want to have something like this, like a tree structure.
$this->result = [
'template' => [
'id' => 1,
'name' => 'MySlider'
'contents' => [
'id' => 745,
'name' => 'slider',
'type' => 'panel',
'slides' => [
0 => [
'id' => 500,
'name' => 'slide',
'type' => 'panel'
'buttons' => [
0 => [
'id' => 877,
'name' => 'button',
'type' => 'item'
]
]
],
1 => [
'id' => 500,
'name' => 'slide',
'type' => 'panel'
'buttons' => [
0 => [
'id' => 877,
'name' => 'button',
'type' => 'item'
]
]
]
]
]
]
];
What I have for the moment
Currently, I have something like this :
$this->result = [
'template' => [
'id' => 1,
'name' => 'MySlider'
'contents' => [
'id' => 745,
'name' => 'slider',
'type' => 'panel'
],
'slides' => [
0 => [
'id' => 500,
'name' => 'slide',
'type' => 'panel'
],
1 => [
'id' => 500,
'name' => 'slide',
'type' => 'panel'
]
],
'buttons' => [
0 => [
'id' => 877,
'name' => 'button',
'type' => 'item'
],
1 => [
'id' => 877,
'name' => 'button',
'type' => 'item'
]
]
]
];
With this code, the result is "normal", because the key at this line is wrong :
$this->result['template']['global_panel'][$parentName][$key] = $panelContent->contentable->toArray();
But I really don't know how to do what I want with my data.
I hope my explains are fine. Do you have an idea about this ? How can I build properly my array (or json of course) ?
Thank you and have a nice day !

I've found a solution. Laravel has many helpers and there is one for array.
private function formatTemplateData(PanelContent $panelContent, string $path): void
{
Arr::set(
$this->result,
'template.data.' . $path,
"Je suis une data"
);
if ($panelContent->contentable_type === 'panel') {
foreach($panelContent->contentable->contents as $container) {
$newPath = $path . '.' . $container->contentable->name . $container->contentable->order;
$this->formatTemplateData($container, $newPath);
}
}
}
With the "set" function, I can use string to create dynamic path for my array.
For example :
"template.data.slider.slide.0.test"
Will be :
$array['template']['data']['slider']['slide'][0]['test']
Thanks for your help.

Related

Notice: Undefined index: content in onboarding.php on line 43

I'm getting this error and not sure why. Here's my code :
$genesis_sample_shared_content = genesis_get_config( 'onboarding-shared' );
return [
'starter_packs' => [
'black-white' => [
'title' => __( 'Black & White', 'genesis-sample' ),
'description' => __( 'A pack with a homepage designed with black and white images.', 'genesis-sample' ),
'thumbnail' => get_stylesheet_directory_uri() . '/config/import/images/thumbnails/home-black-white.jpg',
'demo_url' => 'https://demo.studiopress.com/genesis-sample/',
'config' => [
'dependencies' => [
'plugins' => $genesis_sample_shared_content['plugins'],
],
'content' => array_merge(
[
'homepage' => [
'post_title' => 'Homepage',
'post_content' => require dirname( __FILE__ ) . '/import/content/home-black-white.php',
'post_type' => 'page',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => [
'_genesis_layout' => 'full-width-content',
'_genesis_hide_title' => true,
'_genesis_hide_breadcrumbs' => true,
'_genesis_hide_singular_image' => true,
],
],
],
$genesis_sample_shared_content['content']
),
'navigation_menus' => $genesis_sample_shared_content['navigation_menus'],
'widgets' => $genesis_sample_shared_content['widgets'],
],
],
],
];
I have modified the onboarding-shared.php file and remove the code for the content Maybe this is the problem?
Update : Now the menu items are created on theme setup. Here's the modified code :
return [
'plugins' => [
[
'name' => __( 'Genesis Blocks', 'genesis-sample' ),
'slug' => 'genesis-blocks/genesis-blocks.php',
'public_url' => 'https://wordpress.org/plugins/genesis-blocks/',
],
],
'navigation_menus' => [
'primary' => [
'get-started' => [
'title' => 'Get Started',
],
'testimonials' => [
'title' => 'Testimonials',
],
'contact' => [
'title' => 'Contact Us',
],
],
],
'widgets' => [
'footer-1' => [
[
'type' => 'text',
'args' => [
'title' => 'Design',
'text' => '<p>With an emphasis on typography, white space, and mobile-optimized design, your website will look absolutely breathtaking.</p><p>Learn more about design.</p>',
'filter' => 1,
'visual' => 1,
],
],
],
'footer-2' => [
[
'type' => 'text',
'args' => [
'title' => 'Content',
'text' => '<p>Our team will teach you the art of writing audience-focused content that will help you achieve the success you truly deserve.</p><p>Learn more about content.</p>',
'filter' => 1,
'visual' => 1,
],
],
],
'footer-3' => [
[
'type' => 'text',
'args' => [
'title' => 'Strategy',
'text' => '<p>We help creative entrepreneurs build their digital business by focusing on three key elements of a successful online platform.</p><p>Learn more about strategy.</p>',
'filter' => 1,
'visual' => 1,
],
],
],
],
];
The menu items aren't for pages, they're linked to scroll points on the homepage. Not sure how to code that using the above PHP?
I removed $genesis_sample_shared_content['content'] and it now works except the menu items aren't created on theme setup maybe due to this error
Notice: Trying to get property 'name' of non-object in wp-admin/nav-menus.php on line 1098

How to insert PAGE_NUMBER and PAGE_COUNT in footer

I'm looking for exemple of paragraph and element paragraph in footer.
I'd tried but i only can insert text in footer:
$requests = array (new \Google_Service_Docs_Request(array(
'insertText' => array(
'location' => array(
'segmentId' => $segmentId,
'index' => 0
),
"text" => 'My Text'
)
)),
I tried to implement new footers like this :
$footers = array(
new \Google_Service_Docs_Footer(array(
'footerId' => $segmentId,
'content' => [
'paragraph' => [
'element' => [
'textRun' => [
'content' => 'My Text - '
]
]
]
]
)),
new \Google_Service_Docs_Footer(array(
'footerId' => $segmentId,
'content' => [
'paragraph' => [
'element' => [
'AutoText' => [
'type' => 'PAGE_NUMBER'
]
]
]
]
)),
new \Google_Service_Docs_Footer(array(
'footerId' => $segmentId,
'content' => [
'paragraph' => [
'element' => [
'AutoText' => [
'type' => 'PAGE_COUNT'
]
]
]
]
))
);
$document->setFooters($footers);
Can we manage paragraph of Footer ?
I don't think you can insert AutoText through the API. There is no valid request from batchUpdate that can be used for that (text inserted via InsertTextRequest has to be a string). As a result of this, PAGE_NUMBER and PAGE_COUNT can only be set through the UI: if you're using the API, you can only retrieve them.
I'd suggest you to file a Feature Request in this Issue Tracker component regarding this issue.
Reference:
AutoText
documents.batchUpdate
InsertTextRequest
Issue in progress https://issuetracker.google.com/issues/149275293
i keep you in touch when i have a feedback

PHP MongoDB\Client updateOne add new field to existing document array

I'm struggling to update a document and add new elements (fields) into it's existing array without losing the array elements on update.
This is my code which inserts a new document into the collection if it doesn't already exist (upsert):
$updateResult = $collection->findOneAndUpdate(
[
'recording-id' => $out['recording']->id
],
['$set' => [
'release' => [
'id' => $out['release']->id,
'title' => $out['release']->title,
'date' => $out['release']->date,
'country' => $out['release']->country
],
'artist' => [
'id' => $out['artist']->id,
'name' => $out['artist']->name,
],
'recording' => [
'id' => $out['recording']->id,
'title' => $out['recording']->title,
'score' => $out['recording']->score,
'length' => $out['recording']->length,
'release-count' => count($out['recording']->releases),
],
'release-group' => [
'id' => $out['release-group']['id'],
'title' => $out['release-group']['title'],
'first-release-date'=>$out['release-group']['first-release-date'],
'primary-type' => $out['release-group']['primary-type'],
'musicbrainz' => $out['release-group']['musicbrainz'],
'url-rels' => $out['release-group']['url-rels'],
'coverart' => $out['release-group']['coverart']
],
'execution' => [
'firstfind' => $out['execution']->time
]
]
],
['upsert' => true,
'projection' =>
[
'_id' => 0,
'release' => 1,
'artist' => 1,
'recording' => 1,
'release-group' => 1,
'execution' => 1
],
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
]
);
So now I have an existing document in a collection:
{
"_id" : ObjectId("5d1a6aaf5ecc8001ee858f6c"),
"recording-id" : "d0d439f9-5324-4728-8706-2da39adb89c5",
"artist" : {
"id" : "9d97b077-b28d-4ba8-a3d9-c71926e3b2b6",
"name" : "Gordon Lightfoot"
},
"recording" : {
"id" : "d0d439f9-5324-4728-8706-2da39adb89c5",
"title" : "Sundown",
"score" : 100,
"length" : 184000,
"release-count" : 2
},
"release" : {
"id" : "0c008d76-2bc9-44a3-854b-0a08cde89337",
"title" : "All Live",
"date" : "2012-04-24",
"country" : "CA"
},
"release-group" : {
"id" : "0a5d5f33-8e9d-4fa4-b622-a95e4218a3c4",
"title" : "All Live",
"first-release-date" : "2012-04-24",
"primary-type" : "Album",
"musicbrainz" : "https://musicbrainz.org/release-group/0a5d5f33-8e9d-4fa4-b622-a95e4218a3c4",
"url-rels" : "https://musicbrainz.org/ws/2/release-group/0a5d5f33-8e9d-4fa4-b622-a95e4218a3c4?inc=url-rels&fmt=json",
"coverart" : null
}
}
Now, I would like to update this document, and add new fields into the arrays. The new fields are to be added to certain fields.
Here is the code doing that:
$collection = (new MongoDB\Client)->stream->musicbrainz;
$updateResult = $collection->updateOne(
[
'recording-id' => $out['recording']['id']
],
['$addToSet' => [
'artist' => [
'wikiQiD' => $out['artist']['qid'],
'wiki-extract' => $out['artist']['wiki-extract'],
'wiki-pageid' => $out['artist']['pageid'],
],
'release-group' => [
'wikiQiD' => $out['release-group']['qid'],
'wiki-extract' => $out['release-group']['wiki-extract']
]
]
],
[
'upsert' => true,
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
]
);
I've noticed there's "$addToSet" and "$push" commands, and could use assistance with what the difference is between these two commands.
If the field is absent in the document to update, $push adds the array
field with the value as its element.
The $addToSet operator adds a value to an array unless the value is
already present, in which case $addToSet does nothing to that array.
I did some googling, and reading of the MongoDB/Client UpdateOne function, but can't seem to find a way to append these fields to the existing arrays.
The error I'm getting is:
Fatal error: Uncaught MongoDB\Driver\Exception\BulkWriteException: The field 'artist' must be an array but is of type object in document {_id: ObjectId('5d1a6aaf5ecc8001ee858f6c')} in ...
I know the following:
It could be my document, as it's not a proper array that Fatal error is complaining about.
It could be my `findOneAndUpdate' formatting, and I'm not doing that correctly.
It could be both and I have it all wrong from the very start.
Any insight or constructive criticism is appreciated, just refrain from flames, pls.
Here's the working code I finally use to get it do what I want it to.
It's a simple matter of just setting your field names and values and mongo will update the found record replacing it with the array sent to it. No need to push or anything.
function firstFindMongoUpdate($out) {
$collection = (new MongoDB\Client)->stream->musicbrainz;
$updateResult = $collection->findOneAndUpdate(
[
'recording-id' => $out['recording']->id
],
['$set' => [
'query' => $out['query'],
'release' => [
'id' => $out['release']->id,
'title' => $out['release']->title,
'date' => $out['release']->date,
'country' => $out['release']->country,
'label' => $out['release']->label,
],
'artist' => [
'id' => $out['artist']->id,
'name' => $out['artist']->name,
'wiki' => $out['artist']->wiki,
],
'recording' => [
'id' => $out['recording']->id,
'title' => $out['recording']->title, // sometimes contains apostophe ie; Bill‘s Love (option ] key)
'score' => $out['recording']->score,
'length' => $out['recording']->length,
'release-count' => count($out['recording']->releases)
],
'release-group' => [
'id' => $out['release-group']['id'],
'title' => $out['release-group']['title'],
'first-release-date'=>$out['release-group']['first-release-date'],
'primary-type' => $out['release-group']['primary-type'],
'musicbrainz' => $out['release-group']['musicbrainz'],
'url-rels' => $out['release-group']['url-rels'],
'coverart' => $out['release-group']['coverart'],
'wiki' => $out['release-group']['wiki']
],
'execution' => [
'artistQuery' => $out['execution']->artistQuery,
'recordingQuery'=> $out['execution']->recordingQuery,
'time' => $out['execution']->time
]
]
],
['upsert' => true,
'projection' =>
[
'_id' => 1,
'query' => 1,
'release' => 1,
'artist' => 1,
'recording' => 1,
'release-group' => 1,
'execution' => 1
],
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
]
);

PHP push a value to an array during recursive search

$menus = [
0 => [
'id' => 'home',
'title' => 'Home',
'url' => '/display/home',
'children' => [],
'parent' => null
],
1 => [
'id' => 'nodes',
'title' => 'Nodes',
'url' => 'nodes/index',
'children' => [
0 => [
'id' => 'addNode',
'title' => 'Add Node',
'url' => '/nodes/add',
'children' => [],
'parent' => "nodes"
],
1 => [
'id' => 'editNode',
'title' => 'Edit Node',
'url' => '/nodes/edit',
'children' => [],
'parent' => 'nodes'
],
2 => [
'id' => 'deleteNode',
'title' => 'Delete Node',
'url' => '/nodes/delete',
'children' => [
0 => [
'id' => 'deleteMultipleNodes',
'title' => 'Delete Multiple Nodes',
'url' => '/nodes/deleteall',
'children' => [
0 => [
'id' => 'deleteMultipleSelectedNodes',
'title' => 'Delete Multiple Selected Nodes',
'url' => '/nodes/deleteallselected',
'children' => [],
'parent' => 'deleteMultipleNodes'
]
],
'parent' => 'deleteNode'
]
],
'parent' => 'nodes'
]
],
'parent' => null
]
];
Assuming I have this array. What i want is to recursively search this array for an "id" and if found push a new children to the children array of that element.
I've tried it via different ways, I've also tried to use RecursiveArrayIterator to traverse the array, but the problem is how can i push value to that index of the array when found while traversing.
For Example here is a code from one of my tries:
private function traverseArray($array)
{
$child = [
'id' => 'deleteMultipleNotSelectedNodes',
'title' => 'Delete Multiple Not Selected Nodes',
'url' => '/nodes/deletenotselected',
'children' => [],
'parent' => 'deleteMultipleNodes'
];
foreach($array as $key=>$value)
{
if(is_array($value))
{
$this->traverseArray($value);
}
if($key == "id" && $value == "deleteMultipleNodes")
{
array_push($array["children"], $child); // This part is confusing me, How to add the child on this index where the id is found.
}
}
}
Any help on how to do such thing in an efficient way would save my days.
Here it's how it would work without using $this and fixing bugs in comparing $value instead assigning anything to value.
Please note the difference with &$array and &$value, which are references, so it would change the original data instead of copying it into new variables.
<?php
$menus = [
0 => [
'id' => 'home',
'title' => 'Home',
'url' => '/display/home',
'children' => [],
'parent' => null
],
1 => [
'id' => 'nodes',
'title' => 'Nodes',
'url' => 'nodes/index',
'children' => [
0 => [
'id' => 'addNode',
'title' => 'Add Node',
'url' => '/nodes/add',
'children' => [],
'parent' => "nodes"
],
1 => [
'id' => 'editNode',
'title' => 'Edit Node',
'url' => '/nodes/edit',
'children' => [],
'parent' => 'nodes'
],
2 => [
'id' => 'deleteNode',
'title' => 'Delete Node',
'url' => '/nodes/delete',
'children' => [
0 => [
'id' => 'deleteMultipleNodes',
'title' => 'Delete Multiple Nodes',
'url' => '/nodes/deleteall',
'children' => [
0 => [
'id' => 'deleteMultipleSelectedNodes',
'title' => 'Delete Multiple Selected Nodes',
'url' => '/nodes/deleteallselected',
'children' => [],
'parent' => 'deleteMultipleNodes'
]
],
'parent' => 'deleteNode'
]
],
'parent' => 'nodes'
]
],
'parent' => null
]
];
function traverseArray(&$array)
{
$child = [
'id' => 'deleteMultipleNotSelectedNodes',
'title' => 'Delete Multiple Not Selected Nodes',
'url' => '/nodes/deletenotselected',
'children' => [],
'parent' => 'deleteMultipleNodes'
];
foreach($array as $key=>&$value)
{
if(is_array($value))
{
traverseArray($value);
}
if($key == "id" && $value == "deleteMultipleNodes")
{
array_push($array["children"], $child);
}
}
}
echo "=== before \n";
var_export($menus);
echo "\n\n";
traverseArray($menus);
echo "=== after \n";
var_export($menus);

How do you define a Cassandra CollectionMap nested in a UDT with duoshuo's PHP client library?

I have a CollectionSet<UDT> where UDT contains a CollectionMap<int,boolean>. I have not been able to find any documentation or example of how to define this when creating a new Cassandra\Type\CollectionSet for inserting into the table. There is a great example with a CollectionList (found here) which is like this:
// CollectionSet<UDT>, where UDT contains: Int, Text, Boolean,
// CollectionList<Text>, CollectionList<UDT>
new Cassandra\Type\CollectionSet([
[
'id' => 1,
'name' => 'string',
'active' => true,
'friends' => ['string1', 'string2', 'string3'],
'drinks' => [['qty' => 5, 'brand' => 'Pepsi'], ['qty' => 3, 'brand' => 'Coke']]
],[
'id' => 2,
'name' => 'string',
'active' => false,
'friends' => ['string4', 'string5', 'string6'],
'drinks' => []
]
], [
[
'type' => Cassandra\Type\Base::UDT,
'definition' => [
'id' => Cassandra\Type\Base::INT,
'name' => Cassandra\Type\Base::VARCHAR,
'active' => Cassandra\Type\Base::BOOLEAN,
'friends' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => Cassandra\Type\Base::VARCHAR
],
'drinks' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => [
'type' => Cassandra\Type\Base::UDT,
'typeMap' => [
'qty' => Cassandra\Type\Base::INT,
'brand' => Cassandra\Type\Base::VARCHAR
]
]
]
]
]
]);
I've tried using the above example with several variations to accommodate the CollectionMap but nothing is working. My last attempt was this
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'value' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])
which gives the error Caught exception: Since v0.7, collection types should have \"definition\" directive. I've also tried using 'definition' instead of 'value'. I'm running out of ideas, any help would be greatly appreciated.
Use "definition" instead of "value". I tried this before but apparently I was doing something else wrong because this worked.
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'definition' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])

Categories