makeValuePairs() 공개 정적인 메소드

Make a simple array consisting of key=>value pairs, that can be used in select-boxes in forms.
public static makeValuePairs ( array $array, string $key, string $value ) : array
$array array
$key string
$value string
리턴 array
예제 #1
0
파일: ArrTest.php 프로젝트: aaleksu/bolt_cm
 public function testMakeValuePairs()
 {
     $test = array(array('id' => 1, 'value' => 1), array('id' => 2, 'value' => 2));
     $this->assertEquals(array(1 => 1, 2 => 2), Arr::makeValuePairs($test, 'id', 'value'));
     $this->assertEquals(array(0 => 1, 1 => 2), Arr::makeValuePairs($test, '', 'value'));
 }
예제 #2
0
 /**
  * Update / insert taxonomy for a given content-unit.
  *
  * @param Content $content
  * @param integer $contentId
  * @param array   $taxonomy
  */
 protected function updateTaxonomy($content, $contentId, $taxonomy)
 {
     $contenttype = $content->contenttype;
     $tablename = $this->getTablename('taxonomy');
     $configTaxonomies = $this->app['config']->get('taxonomy');
     // Make sure $contenttypeslug is a 'slug'
     if (is_array($contenttype)) {
         $contenttypeslug = $contenttype['slug'];
     } else {
         $contenttypeslug = $contenttype;
     }
     // If our contenttype has no taxonomies, there's nothing for us to do here.
     if (!isset($contenttype['taxonomy'])) {
         return;
     }
     foreach ($contenttype['taxonomy'] as $taxonomytype) {
         // Set 'newvalues to 'empty array' if not defined
         if (!empty($taxonomy[$taxonomytype])) {
             $newslugs = $this->getIndexedTaxonomy($content, $taxonomy[$taxonomytype]);
         } else {
             $newslugs = array();
         }
         // Get the current values from the DB.
         $query = sprintf("SELECT id, slug, sortorder FROM %s WHERE content_id=? AND contenttype=? AND taxonomytype=?", $tablename);
         $currentvalues = $this->app['db']->executeQuery($query, array($contentId, $contenttypeslug, $taxonomytype), array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_STR))->fetchAll();
         if (!empty($currentvalues)) {
             $currentsortorder = $currentvalues[0]['sortorder'];
             $currentvalues = Arr::makeValuePairs($currentvalues, 'id', 'slug');
         } else {
             $currentsortorder = 0;
             $currentvalues = array();
         }
         // Add the ones not yet present.
         foreach ($newslugs as $slug) {
             // If it's like 'desktop#10', split it into value and sortorder.
             list($slug, $sortorder) = explode('#', $slug . "#");
             // @todo clean up and/or refactor
             // If you save this content via anything other than the Bolt
             // backend (see Content->setFromPost), then taxonomies that
             // behave like groupings, will have their sortorders reset to 0.
             if ($configTaxonomies[$taxonomytype]['behaves_like'] == 'grouping' && empty($sortorder) && $sortorder !== '0') {
                 $sortorder = $currentsortorder;
             }
             if (empty($sortorder)) {
                 $sortorder = 0;
             }
             // Make sure we have a 'name'.
             if (isset($configTaxonomies[$taxonomytype]['options'][$slug])) {
                 $name = $configTaxonomies[$taxonomytype]['options'][$slug];
             } else {
                 $name = $slug;
             }
             // Make sure the slug is also set correctly
             if (!isset($configTaxonomies[$taxonomytype]['options'][$slug])) {
                 // Assume we passed a value, instead of a slug. Turn it back into a proper slug
                 if (isset($configTaxonomies[$taxonomytype]['options']) && is_array($configTaxonomies[$taxonomytype]['options']) && array_search($slug, $configTaxonomies[$taxonomytype]['options'])) {
                     $slug = array_search($slug, $configTaxonomies[$taxonomytype]['options']);
                 } else {
                     // make sure it's at least a slug-like value.
                     $slug = $this->app['slugify']->slugify($slug);
                 }
             }
             if ((!in_array($slug, $currentvalues) || $currentsortorder != $sortorder) && !empty($slug)) {
                 // Insert it!
                 $row = array('content_id' => $contentId, 'contenttype' => $contenttypeslug, 'taxonomytype' => $taxonomytype, 'slug' => $slug, 'name' => $name, 'sortorder' => (int) $sortorder);
                 $this->app['db']->insert($tablename, $row);
             }
         }
         // Convert new slugs to lowercase to compare in the delete process
         $newSlugsNormalised = array();
         foreach ($newslugs as $slug) {
             // If it's like 'desktop#10', split it into value and sortorder.
             list($slug, $sortorder) = explode('#', $slug . "#");
             $slug = $this->app['slugify']->slugify($slug);
             if (!empty($sortorder)) {
                 $slug = $slug . '#' . $sortorder;
             }
             $newSlugsNormalised[] = $slug;
         }
         // Delete the ones that have been removed.
         foreach ($currentvalues as $id => $slug) {
             // Make it look like 'desktop#10'
             $valuewithorder = $slug . "#" . $currentsortorder;
             $slugkey = '/' . $configTaxonomies[$taxonomytype]['slug'] . '/' . $slug;
             if (!in_array($slug, $newSlugsNormalised) && !in_array($valuewithorder, $newSlugsNormalised) && !array_key_exists($slugkey, $newSlugsNormalised)) {
                 $this->app['db']->delete($tablename, array('id' => $id));
             }
         }
     }
 }
예제 #3
0
 public function testMakeValuePairs()
 {
     $test = [['id' => 1, 'value' => 1], ['id' => 2, 'value' => 2]];
     $this->assertEquals([1 => 1, 2 => 2], Arr::makeValuePairs($test, 'id', 'value'));
     $this->assertEquals([0 => 1, 1 => 2], Arr::makeValuePairs($test, '', 'value'));
 }