/en/core-utility-libraries/set.rst

https://github.com/kriskhaira/cakephp-docs · ReStructuredText · 1564 lines · 1359 code · 205 blank · 0 comment · 0 complexity · cec880fc715393af578c8a345a7ddbc7 MD5 · raw file

  1. Set
  2. ###
  3. .. php:class:: Set
  4. Array management, if done right, can be a very powerful and useful
  5. tool for building smarter, more optimized code. CakePHP offers a
  6. very useful set of static utilities in the Set class that allow you
  7. to do just that.
  8. CakePHP's Set class can be called from any model or controller in
  9. the same way Inflector is called. Example: Set::combine().
  10. Set-compatible Path syntax
  11. ==========================
  12. The Path syntax is used by (for example) sort, and is used to
  13. define a path.
  14. Usage example (using :php:func:`Set::sort()`)::
  15. <?php
  16. $a = array(
  17. 0 => array('Person' => array('name' => 'Jeff')),
  18. 1 => array('Shirt' => array('color' => 'black'))
  19. );
  20. $result = Set::sort($a, '{n}.Person.name', 'asc');
  21. /* $result now looks like:
  22. Array
  23. (
  24. [0] => Array
  25. (
  26. [Shirt] => Array
  27. (
  28. [color] => black
  29. )
  30. )
  31. [1] => Array
  32. (
  33. [Person] => Array
  34. (
  35. [name] => Jeff
  36. )
  37. )
  38. )
  39. */
  40. As you can see in the example above, some things are wrapped in
  41. {}'s, others not. In the table below, you can see which options are
  42. available.
  43. Expression
  44. Definition
  45. {n}
  46. Represents a numeric key
  47. {s}
  48. Represents a string
  49. Foo
  50. Any string (without enclosing brackets) is treated like a string
  51. literal.
  52. {[a-z]+}
  53. Any string enclosed in brackets (besides {n} and {s}) is
  54. interpreted as a regular expression.
  55. This section needs to be expanded.
  56. .. php:staticmethod:: Set::insert ($list, $path, $data = null)
  57. :rtype: array
  58. Inserts $data into an array as defined by $path.::
  59. <?php
  60. $a = array(
  61. 'pages' => array('name' => 'page')
  62. );
  63. $result = Set::insert($a, 'files', array('name' => 'files'));
  64. /* $result now looks like:
  65. Array
  66. (
  67. [pages] => Array
  68. (
  69. [name] => page
  70. )
  71. [files] => Array
  72. (
  73. [name] => files
  74. )
  75. )
  76. */
  77. $a = array(
  78. 'pages' => array('name' => 'page')
  79. );
  80. $result = Set::insert($a, 'pages.name', array());
  81. /* $result now looks like:
  82. Array
  83. (
  84. [pages] => Array
  85. (
  86. [name] => Array
  87. (
  88. )
  89. )
  90. )
  91. */
  92. $a = array(
  93. 'pages' => array(
  94. 0 => array('name' => 'main'),
  95. 1 => array('name' => 'about')
  96. )
  97. );
  98. $result = Set::insert($a, 'pages.1.vars', array('title' => 'page title'));
  99. /* $result now looks like:
  100. Array
  101. (
  102. [pages] => Array
  103. (
  104. [0] => Array
  105. (
  106. [name] => main
  107. )
  108. [1] => Array
  109. (
  110. [name] => about
  111. [vars] => Array
  112. (
  113. [title] => page title
  114. )
  115. )
  116. )
  117. )
  118. */
  119. .. php:staticmethod:: sort($data, $path, $dir)
  120. :rtype: array
  121. Sorts an array by any value, determined by a Set-compatible path.::
  122. $a = array(
  123. 0 => array('Person' => array('name' => 'Jeff')),
  124. 1 => array('Shirt' => array('color' => 'black'))
  125. );
  126. $result = Set::sort($a, '{n}.Person.name', 'asc');
  127. /* $result now looks like:
  128. Array
  129. (
  130. [0] => Array
  131. (
  132. [Shirt] => Array
  133. (
  134. [color] => black
  135. )
  136. )
  137. [1] => Array
  138. (
  139. [Person] => Array
  140. (
  141. [name] => Jeff
  142. )
  143. )
  144. )
  145. */
  146. $result = Set::sort($a, '{n}.Shirt', 'asc');
  147. /* $result now looks like:
  148. Array
  149. (
  150. [0] => Array
  151. (
  152. [Person] => Array
  153. (
  154. [name] => Jeff
  155. )
  156. )
  157. [1] => Array
  158. (
  159. [Shirt] => Array
  160. (
  161. [color] => black
  162. )
  163. )
  164. )
  165. */
  166. $result = Set::sort($a, '{n}', 'desc');
  167. /* $result now looks like:
  168. Array
  169. (
  170. [0] => Array
  171. (
  172. [Shirt] => Array
  173. (
  174. [color] => black
  175. )
  176. )
  177. [1] => Array
  178. (
  179. [Person] => Array
  180. (
  181. [name] => Jeff
  182. )
  183. )
  184. )
  185. */
  186. $a = array(
  187. array(7,6,4),
  188. array(3,4,5),
  189. array(3,2,1),
  190. );
  191. $result = Set::sort($a, '{n}.{n}', 'asc');
  192. /* $result now looks like:
  193. Array
  194. (
  195. [0] => Array
  196. (
  197. [0] => 3
  198. [1] => 2
  199. [2] => 1
  200. )
  201. [1] => Array
  202. (
  203. [0] => 3
  204. [1] => 4
  205. [2] => 5
  206. )
  207. [2] => Array
  208. (
  209. [0] => 7
  210. [1] => 6
  211. [2] => 4
  212. )
  213. )
  214. */
  215. .. php:staticmethod:: reverse($object)
  216. :rtype: array
  217. Set::reverse is basically the opposite of :php:func:`Set::map`. It converts an
  218. object into an array. If $object is not an object, reverse will
  219. simply return $object.::
  220. <?php
  221. $result = Set::reverse(null);
  222. // Null
  223. $result = Set::reverse(false);
  224. // false
  225. $a = array(
  226. 'Post' => array('id'=> 1, 'title' => 'First Post'),
  227. 'Comment' => array(
  228. array('id'=> 1, 'title' => 'First Comment'),
  229. array('id'=> 2, 'title' => 'Second Comment')
  230. ),
  231. 'Tag' => array(
  232. array('id'=> 1, 'title' => 'First Tag'),
  233. array('id'=> 2, 'title' => 'Second Tag')
  234. ),
  235. );
  236. $map = Set::map($a); // Turn $a into a class object
  237. /* $map now looks like:
  238. stdClass Object
  239. (
  240. [_name_] => Post
  241. [id] => 1
  242. [title] => First Post
  243. [Comment] => Array
  244. (
  245. [0] => stdClass Object
  246. (
  247. [id] => 1
  248. [title] => First Comment
  249. )
  250. [1] => stdClass Object
  251. (
  252. [id] => 2
  253. [title] => Second Comment
  254. )
  255. )
  256. [Tag] => Array
  257. (
  258. [0] => stdClass Object
  259. (
  260. [id] => 1
  261. [title] => First Tag
  262. )
  263. [1] => stdClass Object
  264. (
  265. [id] => 2
  266. [title] => Second Tag
  267. )
  268. )
  269. )
  270. */
  271. $result = Set::reverse($map);
  272. /* $result now looks like:
  273. Array
  274. (
  275. [Post] => Array
  276. (
  277. [id] => 1
  278. [title] => First Post
  279. [Comment] => Array
  280. (
  281. [0] => Array
  282. (
  283. [id] => 1
  284. [title] => First Comment
  285. )
  286. [1] => Array
  287. (
  288. [id] => 2
  289. [title] => Second Comment
  290. )
  291. )
  292. [Tag] => Array
  293. (
  294. [0] => Array
  295. (
  296. [id] => 1
  297. [title] => First Tag
  298. )
  299. [1] => Array
  300. (
  301. [id] => 2
  302. [title] => Second Tag
  303. )
  304. )
  305. )
  306. )
  307. */
  308. $result = Set::reverse($a['Post']); // Just return the array
  309. /* $result now looks like:
  310. Array
  311. (
  312. [id] => 1
  313. [title] => First Post
  314. )
  315. */
  316. .. php:staticmethod:: combine($data, $path1 = null, $path2 = null, $groupPath = null)
  317. :rtype: array
  318. Creates an associative array using a $path1 as the path to build
  319. its keys, and optionally $path2 as path to get the values. If
  320. $path2 is not specified, all values will be initialized to null
  321. (useful for Set::merge). You can optionally group the values by
  322. what is obtained when following the path specified in $groupPath.::
  323. <?php
  324. $result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
  325. // $result == array();
  326. $result = Set::combine('', '{n}.User.id', '{n}.User.Data');
  327. // $result == array();
  328. $a = array(
  329. array(
  330. 'User' => array(
  331. 'id' => 2,
  332. 'group_id' => 1,
  333. 'Data' => array(
  334. 'user' => 'mariano.iglesias',
  335. 'name' => 'Mariano Iglesias'
  336. )
  337. )
  338. ),
  339. array(
  340. 'User' => array(
  341. 'id' => 14,
  342. 'group_id' => 2,
  343. 'Data' => array(
  344. 'user' => 'phpnut',
  345. 'name' => 'Larry E. Masters'
  346. )
  347. )
  348. ),
  349. array(
  350. 'User' => array(
  351. 'id' => 25,
  352. 'group_id' => 1,
  353. 'Data' => array(
  354. 'user' => 'gwoo',
  355. 'name' => 'The Gwoo'
  356. )
  357. )
  358. )
  359. );
  360. $result = Set::combine($a, '{n}.User.id');
  361. /* $result now looks like:
  362. Array
  363. (
  364. [2] =>
  365. [14] =>
  366. [25] =>
  367. )
  368. */
  369. $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
  370. /* $result now looks like:
  371. Array
  372. (
  373. [2] =>
  374. [14] =>
  375. [25] =>
  376. )
  377. */
  378. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  379. /* $result now looks like:
  380. Array
  381. (
  382. [2] => Array
  383. (
  384. [user] => mariano.iglesias
  385. [name] => Mariano Iglesias
  386. )
  387. [14] => Array
  388. (
  389. [user] => phpnut
  390. [name] => Larry E. Masters
  391. )
  392. [25] => Array
  393. (
  394. [user] => gwoo
  395. [name] => The Gwoo
  396. )
  397. )
  398. */
  399. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  400. /* $result now looks like:
  401. Array
  402. (
  403. [2] => Mariano Iglesias
  404. [14] => Larry E. Masters
  405. [25] => The Gwoo
  406. )
  407. */
  408. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  409. /* $result now looks like:
  410. Array
  411. (
  412. [1] => Array
  413. (
  414. [2] => Array
  415. (
  416. [user] => mariano.iglesias
  417. [name] => Mariano Iglesias
  418. )
  419. [25] => Array
  420. (
  421. [user] => gwoo
  422. [name] => The Gwoo
  423. )
  424. )
  425. [2] => Array
  426. (
  427. [14] => Array
  428. (
  429. [user] => phpnut
  430. [name] => Larry E. Masters
  431. )
  432. )
  433. )
  434. */
  435. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  436. /* $result now looks like:
  437. Array
  438. (
  439. [1] => Array
  440. (
  441. [2] => Mariano Iglesias
  442. [25] => The Gwoo
  443. )
  444. [2] => Array
  445. (
  446. [14] => Larry E. Masters
  447. )
  448. )
  449. */
  450. $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
  451. /* $result now looks like:
  452. Array
  453. (
  454. [1] => Array
  455. (
  456. [2] => mariano.iglesias: Mariano Iglesias
  457. [25] => gwoo: The Gwoo
  458. )
  459. [2] => Array
  460. (
  461. [14] => phpnut: Larry E. Masters
  462. )
  463. )
  464. */
  465. $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  466. /* $result now looks like:
  467. Array
  468. (
  469. [mariano.iglesias: Mariano Iglesias] => 2
  470. [phpnut: Larry E. Masters] => 14
  471. [gwoo: The Gwoo] => 25
  472. )
  473. */
  474. $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  475. /* $result now looks like:
  476. Array
  477. (
  478. [Mariano Iglesias: mariano.iglesias] => 2
  479. [Larry E. Masters: phpnut] => 14
  480. [The Gwoo: gwoo] => 25
  481. )
  482. */
  483. $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  484. /* $result now looks like:
  485. Array
  486. (
  487. [mariano.iglesias: 2] => Mariano Iglesias
  488. [phpnut: 14] => Larry E. Masters
  489. [gwoo: 25] => The Gwoo
  490. )
  491. */
  492. $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  493. /* $result now looks like:
  494. Array
  495. (
  496. [2: mariano.iglesias] => Mariano Iglesias
  497. [14: phpnut] => Larry E. Masters
  498. [25: gwoo] => The Gwoo
  499. )
  500. */
  501. .. php:staticmethod:: normalize($list, $assoc = true, $sep = ',', $trim = true)
  502. :rtype: array
  503. Normalizes a string or array list.::
  504. <?php
  505. $a = array('Tree', 'CounterCache',
  506. 'Upload' => array(
  507. 'folder' => 'products',
  508. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')));
  509. $b = array('Cacheable' => array('enabled' => false),
  510. 'Limit',
  511. 'Bindable',
  512. 'Validator',
  513. 'Transactional');
  514. $result = Set::normalize($a);
  515. /* $result now looks like:
  516. Array
  517. (
  518. [Tree] =>
  519. [CounterCache] =>
  520. [Upload] => Array
  521. (
  522. [folder] => products
  523. [fields] => Array
  524. (
  525. [0] => image_1_id
  526. [1] => image_2_id
  527. [2] => image_3_id
  528. [3] => image_4_id
  529. [4] => image_5_id
  530. )
  531. )
  532. )
  533. */
  534. $result = Set::normalize($b);
  535. /* $result now looks like:
  536. Array
  537. (
  538. [Cacheable] => Array
  539. (
  540. [enabled] =>
  541. )
  542. [Limit] =>
  543. [Bindable] =>
  544. [Validator] =>
  545. [Transactional] =>
  546. )
  547. */
  548. $result = Set::merge($a, $b); // Now merge the two and normalize
  549. /* $result now looks like:
  550. Array
  551. (
  552. [0] => Tree
  553. [1] => CounterCache
  554. [Upload] => Array
  555. (
  556. [folder] => products
  557. [fields] => Array
  558. (
  559. [0] => image_1_id
  560. [1] => image_2_id
  561. [2] => image_3_id
  562. [3] => image_4_id
  563. [4] => image_5_id
  564. )
  565. )
  566. [Cacheable] => Array
  567. (
  568. [enabled] =>
  569. )
  570. [2] => Limit
  571. [3] => Bindable
  572. [4] => Validator
  573. [5] => Transactional
  574. )
  575. */
  576. $result = Set::normalize(Set::merge($a, $b));
  577. /* $result now looks like:
  578. Array
  579. (
  580. [Tree] =>
  581. [CounterCache] =>
  582. [Upload] => Array
  583. (
  584. [folder] => products
  585. [fields] => Array
  586. (
  587. [0] => image_1_id
  588. [1] => image_2_id
  589. [2] => image_3_id
  590. [3] => image_4_id
  591. [4] => image_5_id
  592. )
  593. )
  594. [Cacheable] => Array
  595. (
  596. [enabled] =>
  597. )
  598. [Limit] =>
  599. [Bindable] =>
  600. [Validator] =>
  601. [Transactional] =>
  602. )
  603. */
  604. .. php:staticmethod:: countDim ($array = null, $all = false, $count = 0)
  605. :rtype: integer
  606. Counts the dimensions of an array. If $all is set to false (which
  607. is the default) it will only consider the dimension of the first
  608. element in the array::
  609. <?php
  610. $data = array('one', '2', 'three');
  611. $result = Set::countDim($data);
  612. // $result == 1
  613. $data = array('1' => '1.1', '2', '3');
  614. $result = Set::countDim($data);
  615. // $result == 1
  616. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  617. $result = Set::countDim($data);
  618. // $result == 2
  619. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  620. $result = Set::countDim($data);
  621. // $result == 1
  622. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  623. $result = Set::countDim($data, true);
  624. // $result == 2
  625. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  626. $result = Set::countDim($data);
  627. // $result == 2
  628. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  629. $result = Set::countDim($data, true);
  630. // $result == 3
  631. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  632. $result = Set::countDim($data, true);
  633. // $result == 4
  634. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  635. $result = Set::countDim($data, true);
  636. // $result == 5
  637. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  638. $result = Set::countDim($data, true);
  639. // $result == 5
  640. $set = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  641. $result = Set::countDim($set, false, 0);
  642. // $result == 2
  643. $result = Set::countDim($set, true);
  644. // $result == 5
  645. .. php:staticmethod:: diff($val1, $val2 = null)
  646. :rtype: array
  647. Computes the difference between a Set and an array, two Sets, or
  648. two arrays::
  649. <?php
  650. $a = array(
  651. 0 => array('name' => 'main'),
  652. 1 => array('name' => 'about')
  653. );
  654. $b = array(
  655. 0 => array('name' => 'main'),
  656. 1 => array('name' => 'about'),
  657. 2 => array('name' => 'contact')
  658. );
  659. $result = Set::diff($a, $b);
  660. /* $result now looks like:
  661. Array
  662. (
  663. [2] => Array
  664. (
  665. [name] => contact
  666. )
  667. )
  668. */
  669. $result = Set::diff($a, array());
  670. /* $result now looks like:
  671. Array
  672. (
  673. [0] => Array
  674. (
  675. [name] => main
  676. )
  677. [1] => Array
  678. (
  679. [name] => about
  680. )
  681. )
  682. */
  683. $result = Set::diff(array(), $b);
  684. /* $result now looks like:
  685. Array
  686. (
  687. [0] => Array
  688. (
  689. [name] => main
  690. )
  691. [1] => Array
  692. (
  693. [name] => about
  694. )
  695. [2] => Array
  696. (
  697. [name] => contact
  698. )
  699. )
  700. */
  701. $b = array(
  702. 0 => array('name' => 'me'),
  703. 1 => array('name' => 'about')
  704. );
  705. $result = Set::diff($a, $b);
  706. /* $result now looks like:
  707. Array
  708. (
  709. [0] => Array
  710. (
  711. [name] => main
  712. )
  713. )
  714. */
  715. .. php:staticmethod:: check($data, $path = null)
  716. :rtype: boolean/array
  717. Checks if a particular path is set in an array. If $path is empty,
  718. $data will be returned instead of a boolean value::
  719. <?php
  720. $set = array(
  721. 'My Index 1' => array('First' => 'The first item')
  722. );
  723. $result = Set::check($set, 'My Index 1.First');
  724. // $result == True
  725. $result = Set::check($set, 'My Index 1');
  726. // $result == True
  727. $result = Set::check($set, array());
  728. // $result == array('My Index 1' => array('First' => 'The first item'))
  729. $set = array(
  730. 'My Index 1' => array('First' =>
  731. array('Second' =>
  732. array('Third' =>
  733. array('Fourth' => 'Heavy. Nesting.'))))
  734. );
  735. $result = Set::check($set, 'My Index 1.First.Second');
  736. // $result == True
  737. $result = Set::check($set, 'My Index 1.First.Second.Third');
  738. // $result == True
  739. $result = Set::check($set, 'My Index 1.First.Second.Third.Fourth');
  740. // $result == True
  741. $result = Set::check($set, 'My Index 1.First.Seconds.Third.Fourth');
  742. // $result == False
  743. .. php:staticmethod:: remove($list, $path = null)
  744. :rtype: array
  745. Removes an element from a Set or array as defined by $path::
  746. <?php
  747. $a = array(
  748. 'pages' => array('name' => 'page'),
  749. 'files' => array('name' => 'files')
  750. );
  751. $result = Set::remove($a, 'files');
  752. /* $result now looks like:
  753. Array
  754. (
  755. [pages] => Array
  756. (
  757. [name] => page
  758. )
  759. )
  760. */
  761. .. php:staticmethod:: classicExtract($data, $path = null)
  762. :rtype: array
  763. Gets a value from an array or object that is contained in a given
  764. path using an array path syntax, i.e.:
  765. - "{n}.Person.{[a-z]+}" - Where "{n}" represents a numeric key,
  766. "Person" represents a string literal
  767. - "{[a-z]+}" (i.e. any string literal enclosed in brackets besides
  768. {n} and {s}) is interpreted as a regular expression.
  769. **Example 1**
  770. ::
  771. <?php
  772. $a = array(
  773. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  774. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  775. array('Article' => array('id' => 3, 'title' => 'Article 3')));
  776. $result = Set::classicExtract($a, '{n}.Article.id');
  777. /* $result now looks like:
  778. Array
  779. (
  780. [0] => 1
  781. [1] => 2
  782. [2] => 3
  783. )
  784. */
  785. $result = Set::classicExtract($a, '{n}.Article.title');
  786. /* $result now looks like:
  787. Array
  788. (
  789. [0] => Article 1
  790. [1] => Article 2
  791. [2] => Article 3
  792. )
  793. */
  794. $result = Set::classicExtract($a, '1.Article.title');
  795. // $result == "Article 2"
  796. $result = Set::classicExtract($a, '3.Article.title');
  797. // $result == null
  798. **Example 2**
  799. ::
  800. <?php
  801. $a = array(
  802. 0 => array('pages' => array('name' => 'page')),
  803. 1 => array('fruites'=> array('name' => 'fruit')),
  804. 'test' => array(array('name' => 'jippi')),
  805. 'dot.test' => array(array('name' => 'jippi'))
  806. );
  807. $result = Set::classicExtract($a, '{n}.{s}.name');
  808. /* $result now looks like:
  809. Array
  810. (
  811. [0] => Array
  812. (
  813. [0] => page
  814. )
  815. [1] => Array
  816. (
  817. [0] => fruit
  818. )
  819. )
  820. */
  821. $result = Set::classicExtract($a, '{s}.{n}.name');
  822. /* $result now looks like:
  823. Array
  824. (
  825. [0] => Array
  826. (
  827. [0] => jippi
  828. )
  829. [1] => Array
  830. (
  831. [0] => jippi
  832. )
  833. )
  834. */
  835. $result = Set::classicExtract($a,'{\w+}.{\w+}.name');
  836. /* $result now looks like:
  837. Array
  838. (
  839. [0] => Array
  840. (
  841. [pages] => page
  842. )
  843. [1] => Array
  844. (
  845. [fruites] => fruit
  846. )
  847. [test] => Array
  848. (
  849. [0] => jippi
  850. )
  851. [dot.test] => Array
  852. (
  853. [0] => jippi
  854. )
  855. )
  856. */
  857. $result = Set::classicExtract($a,'{\d+}.{\w+}.name');
  858. /* $result now looks like:
  859. Array
  860. (
  861. [0] => Array
  862. (
  863. [pages] => page
  864. )
  865. [1] => Array
  866. (
  867. [fruites] => fruit
  868. )
  869. )
  870. */
  871. $result = Set::classicExtract($a,'{n}.{\w+}.name');
  872. /* $result now looks like:
  873. Array
  874. (
  875. [0] => Array
  876. (
  877. [pages] => page
  878. )
  879. [1] => Array
  880. (
  881. [fruites] => fruit
  882. )
  883. )
  884. */
  885. $result = Set::classicExtract($a,'{s}.{\d+}.name');
  886. /* $result now looks like:
  887. Array
  888. (
  889. [0] => Array
  890. (
  891. [0] => jippi
  892. )
  893. [1] => Array
  894. (
  895. [0] => jippi
  896. )
  897. )
  898. */
  899. $result = Set::classicExtract($a,'{s}');
  900. /* $result now looks like:
  901. Array
  902. (
  903. [0] => Array
  904. (
  905. [0] => Array
  906. (
  907. [name] => jippi
  908. )
  909. )
  910. [1] => Array
  911. (
  912. [0] => Array
  913. (
  914. [name] => jippi
  915. )
  916. )
  917. )
  918. */
  919. $result = Set::classicExtract($a,'{[a-z]}');
  920. /* $result now looks like:
  921. Array
  922. (
  923. [test] => Array
  924. (
  925. [0] => Array
  926. (
  927. [name] => jippi
  928. )
  929. )
  930. [dot.test] => Array
  931. (
  932. [0] => Array
  933. (
  934. [name] => jippi
  935. )
  936. )
  937. )
  938. */
  939. $result = Set::classicExtract($a, '{dot\.test}.{n}');
  940. /* $result now looks like:
  941. Array
  942. (
  943. [dot.test] => Array
  944. (
  945. [0] => Array
  946. (
  947. [name] => jippi
  948. )
  949. )
  950. )
  951. */
  952. .. php:staticmethod:: matches($conditions, $data=array(), $i = null, $length=null)
  953. :rtype: boolean
  954. Set::matches can be used to see if a single item or a given xpath
  955. match certain conditions.::
  956. <?php
  957. $a = array(
  958. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  959. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  960. array('Article' => array('id' => 3, 'title' => 'Article 3')));
  961. $res=Set::matches(array('id>2'), $a[1]['Article']);
  962. // returns false
  963. $res=Set::matches(array('id>=2'), $a[1]['Article']);
  964. // returns true
  965. $res=Set::matches(array('id>=3'), $a[1]['Article']);
  966. // returns false
  967. $res=Set::matches(array('id<=2'), $a[1]['Article']);
  968. // returns true
  969. $res=Set::matches(array('id<2'), $a[1]['Article']);
  970. // returns false
  971. $res=Set::matches(array('id>1'), $a[1]['Article']);
  972. // returns true
  973. $res=Set::matches(array('id>1', 'id<3', 'id!=0'), $a[1]['Article']);
  974. // returns true
  975. $res=Set::matches(array('3'), null, 3);
  976. // returns true
  977. $res=Set::matches(array('5'), null, 5);
  978. // returns true
  979. $res=Set::matches(array('id'), $a[1]['Article']);
  980. // returns true
  981. $res=Set::matches(array('id', 'title'), $a[1]['Article']);
  982. // returns true
  983. $res=Set::matches(array('non-existant'), $a[1]['Article']);
  984. // returns false
  985. $res=Set::matches('/Article[id=2]', $a);
  986. // returns true
  987. $res=Set::matches('/Article[id=4]', $a);
  988. // returns false
  989. $res=Set::matches(array(), $a);
  990. // returns true
  991. .. php:staticmethod:: extract($path, $data=null, $options=array())
  992. :rtype: array
  993. Set::extract uses basic XPath 2.0 syntax to return subsets of your
  994. data from a find or a find all. This function allows you to
  995. retrieve your data quickly without having to loop through multi
  996. dimentional arrays or traverse through tree structures.
  997. .. note::
  998. If $path is an array or $data is empty it the call is delegated to
  999. Set::classicExtract.
  1000. ::
  1001. <?php
  1002. // Common Usage:
  1003. $users = $this->User->find("all");
  1004. $results = Set::extract('/User/id', $users);
  1005. // results returns:
  1006. // array(1,2,3,4,5,...);
  1007. Currently implemented selectors:
  1008. Selector
  1009. Note
  1010. /User/id
  1011. Similar to the classic {n}.User.id
  1012. /User[2]/name
  1013. Selects the name of the second User
  1014. /User[id<2]
  1015. Selects all Users with an id < 2
  1016. /User[id>2][<5]
  1017. Selects all Users with an id > 2 but
  1018. 5
  1019. /Post/Comment[author\_name=john]/../name
  1020. Selects the name of all Posts that have at least one Comment
  1021. written by john
  1022. /Posts[title]
  1023. Selects all Posts that have a 'title' key
  1024. /Comment/.[1]
  1025. Selects the contents of the first comment
  1026. /Comment/.[:last]
  1027. Selects the last comment
  1028. /Comment/.[:first]
  1029. Selects the first comment
  1030. /Comment[text=/cakephp/i]
  1031. Selects all comments that have a text matching the regex /cakephp/i
  1032. /Comment/@\*
  1033. Selects the key names of all comments
  1034. Currently only absolute paths starting with a single '/' are
  1035. supported. Please report any bugs as you find them. Suggestions for
  1036. additional features are welcome.
  1037. To learn more about Set::extract() refer to function testExtract()
  1038. in /cake/tests/cases/libs/set.test.php.
  1039. .. php:staticmethod:: format($data, $format, $keys)
  1040. :rtype: array
  1041. Returns a series of values extracted from an array, formatted in a
  1042. format string::
  1043. <?php
  1044. $data = array(
  1045. array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
  1046. array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')),
  1047. array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}')));
  1048. $res = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name'));
  1049. /*
  1050. Array
  1051. (
  1052. [0] => Abele, Nate
  1053. [1] => Masters, Larry
  1054. [2] => Woodworth, Garrett
  1055. )
  1056. */
  1057. $res = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state'));
  1058. /*
  1059. Array
  1060. (
  1061. [0] => Boston, MA
  1062. [1] => Boondock, TN
  1063. [2] => Venice Beach, CA
  1064. )
  1065. */
  1066. $res = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state'));
  1067. /*
  1068. Array
  1069. (
  1070. [0] => {Boston, MA}
  1071. [1] => {Boondock, TN}
  1072. [2] => {Venice Beach, CA}
  1073. )
  1074. */
  1075. $res = Set::format($data, '{%2$d, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
  1076. /*
  1077. Array
  1078. (
  1079. [0] => {42, 42}
  1080. [1] => {0, {0}}
  1081. [2] => {0, {1}}
  1082. )
  1083. */
  1084. $res = Set::format($data, '%2$d, %1$s', array('{n}.Person.first_name', '{n}.Person.something'));
  1085. /*
  1086. Array
  1087. (
  1088. [0] => 42, Nate
  1089. [1] => 0, Larry
  1090. [2] => 0, Garrett
  1091. )
  1092. */
  1093. $res = Set::format($data, '%1$s, %2$d', array('{n}.Person.first_name', '{n}.Person.something'));
  1094. /*
  1095. Array
  1096. (
  1097. [0] => Nate, 42
  1098. [1] => Larry, 0
  1099. [2] => Garrett, 0
  1100. )
  1101. */
  1102. .. php:staticmethod:: enum($select, $list=null)
  1103. :rtype: string
  1104. The enum method works well when using html select elements. It
  1105. returns a value from an array list if the key exists.
  1106. If a comma separated $list is passed arrays are numeric with the
  1107. key of the first being 0 $list = 'no, yes' would translate to $list
  1108. = array(0 => 'no', 1 => 'yes');
  1109. If an array is used, keys can be strings example: array('no' => 0,
  1110. 'yes' => 1);
  1111. $list defaults to 0 = no 1 = yes if param is not passed::
  1112. <?php
  1113. $res = Set::enum(1, 'one, two');
  1114. // $res is 'two'
  1115. $res = Set::enum('no', array('no' => 0, 'yes' => 1));
  1116. // $res is 0
  1117. $res = Set::enum('first', array('first' => 'one', 'second' => 'two'));
  1118. // $res is 'one'
  1119. .. php:staticmethod:: numeric($array=null)
  1120. :rtype: boolean
  1121. Checks to see if all the values in the array are numeric::
  1122. <?php
  1123. $data = array('one');
  1124. $res = Set::numeric(array_keys($data));
  1125. // $res is true
  1126. $data = array(1 => 'one');
  1127. $res = Set::numeric($data);
  1128. // $res is false
  1129. $data = array('one');
  1130. $res = Set::numeric($data);
  1131. // $res is false
  1132. $data = array('one' => 'two');
  1133. $res = Set::numeric($data);
  1134. // $res is false
  1135. $data = array('one' => 1);
  1136. $res = Set::numeric($data);
  1137. // $res is true
  1138. $data = array(0);
  1139. $res = Set::numeric($data);
  1140. // $res is true
  1141. $data = array('one', 'two', 'three', 'four', 'five');
  1142. $res = Set::numeric(array_keys($data));
  1143. // $res is true
  1144. $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  1145. $res = Set::numeric(array_keys($data));
  1146. // $res is true
  1147. $data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  1148. $res = Set::numeric(array_keys($data));
  1149. // $res is true
  1150. $data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
  1151. $res = Set::numeric(array_keys($data));
  1152. // $res is false
  1153. .. php:staticmethod:: map($class = 'stdClass', $tmp = 'stdClass')
  1154. :rtype: object
  1155. This method Maps the contents of the Set object to an object
  1156. hierarchy while maintaining numeric keys as arrays of objects.
  1157. Basically, the map function turns array items into initialized
  1158. class objects. By default it turns an array into a stdClass Object,
  1159. however you can map values into any type of class. Example:
  1160. Set::map($array\_of\_values, 'nameOfYourClass');::
  1161. <?php
  1162. $data = array(
  1163. array(
  1164. "IndexedPage" => array(
  1165. "id" => 1,
  1166. "url" => 'http://blah.com/',
  1167. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  1168. 'get_vars' => '',
  1169. 'redirect' => '',
  1170. 'created' => "1195055503",
  1171. 'updated' => "1195055503",
  1172. )
  1173. ),
  1174. array(
  1175. "IndexedPage" => array(
  1176. "id" => 2,
  1177. "url" => 'http://blah.com/',
  1178. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  1179. 'get_vars' => '',
  1180. 'redirect' => '',
  1181. 'created' => "1195055503",
  1182. 'updated' => "1195055503",
  1183. ),
  1184. )
  1185. );
  1186. $mapped = Set::map($data);
  1187. /* $mapped now looks like:
  1188. Array
  1189. (
  1190. [0] => stdClass Object
  1191. (
  1192. [_name_] => IndexedPage
  1193. [id] => 1
  1194. [url] => http://blah.com/
  1195. [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
  1196. [get_vars] =>
  1197. [redirect] =>
  1198. [created] => 1195055503
  1199. [updated] => 1195055503
  1200. )
  1201. [1] => stdClass Object
  1202. (
  1203. [_name_] => IndexedPage
  1204. [id] => 2
  1205. [url] => http://blah.com/
  1206. [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
  1207. [get_vars] =>
  1208. [redirect] =>
  1209. [created] => 1195055503
  1210. [updated] => 1195055503
  1211. )
  1212. )
  1213. */
  1214. Using Set::map() with a custom class for second parameter:
  1215. ::
  1216. class MyClass {
  1217. function sayHi() {
  1218. echo 'Hi!';
  1219. }
  1220. }
  1221. $mapped = Set::map($data, 'MyClass');
  1222. //Now you can access all the properties as in the example above,
  1223. //but also you can call MyClass's methods
  1224. $mapped->[0]->sayHi();
  1225. .. php:staticmethod:: pushDiff($array1, $array2)
  1226. :rtype: array
  1227. This function merges two arrays and pushes the differences in
  1228. array2 to the bottom of the resultant array.
  1229. **Example 1**
  1230. ::
  1231. <?php
  1232. $array1 = array('ModelOne' => array('id'=>1001, 'field_one'=>'a1.m1.f1', 'field_two'=>'a1.m1.f2'));
  1233. $array2 = array('ModelOne' => array('id'=>1003, 'field_one'=>'a3.m1.f1', 'field_two'=>'a3.m1.f2', 'field_three'=>'a3.m1.f3'));
  1234. $res = Set::pushDiff($array1, $array2);
  1235. /* $res now looks like:
  1236. Array
  1237. (
  1238. [ModelOne] => Array
  1239. (
  1240. [id] => 1001
  1241. [field_one] => a1.m1.f1
  1242. [field_two] => a1.m1.f2
  1243. [field_three] => a3.m1.f3
  1244. )
  1245. )
  1246. */
  1247. **Example 2**
  1248. ::
  1249. <?php
  1250. $array1 = array("a"=>"b", 1 => 20938, "c"=>"string");
  1251. $array2 = array("b"=>"b", 3 => 238, "c"=>"string", array("extra_field"));
  1252. $res = Set::pushDiff($array1, $array2);
  1253. /* $res now looks like:
  1254. Array
  1255. (
  1256. [a] => b
  1257. [1] => 20938
  1258. [c] => string
  1259. [b] => b
  1260. [3] => 238
  1261. [4] => Array
  1262. (
  1263. [0] => extra_field
  1264. )
  1265. )
  1266. */
  1267. .. php:staticmethod:: filter($var, $isArray=null)
  1268. :rtype: array
  1269. Filters empty elements out of a route array, excluding '0'::
  1270. <?php
  1271. $res = Set::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
  1272. /* $res now looks like:
  1273. Array (
  1274. [0] => 0
  1275. [2] => 1
  1276. [3] => 0
  1277. [4] => Array
  1278. (
  1279. [0] => one thing
  1280. [1] => I can tell you
  1281. [2] => is you got to be
  1282. [3] =>
  1283. )
  1284. )
  1285. */
  1286. .. php:staticmethod:: merge($arr1, $arr2=null)
  1287. :rtype: array
  1288. This function can be thought of as a hybrid between PHP's
  1289. array\_merge and array\_merge\_recursive. The difference to the two
  1290. is that if an array key contains another array then the function
  1291. behaves recursive (unlike array\_merge) but does not do if for keys
  1292. containing strings (unlike array\_merge\_recursive). See the unit
  1293. test for more information.
  1294. .. note::
  1295. This function will work with an unlimited amount of arguments and
  1296. typecasts non-array parameters into arrays.
  1297. ::
  1298. <?php
  1299. $arry1 = array(
  1300. array(
  1301. 'id' => '48c2570e-dfa8-4c32-a35e-0d71cbdd56cb',
  1302. 'name' => 'mysql raleigh-workshop-08 < 2008-09-05.sql ',
  1303. 'description' => 'Importing an sql dump'
  1304. ),
  1305. array(
  1306. 'id' => '48c257a8-cf7c-4af2-ac2f-114ecbdd56cb',
  1307. 'name' => 'pbpaste | grep -i Unpaid | pbcopy',
  1308. 'description' => 'Remove all lines that say "Unpaid".',
  1309. )
  1310. );
  1311. $arry2 = 4;
  1312. $arry3 = array(0=>"test array", "cats"=>"dogs", "people" => 1267);
  1313. $arry4 = array("cats"=>"felines", "dog"=>"angry");
  1314. $res = Set::merge($arry1, $arry2, $arry3, $arry4);
  1315. /* $res now looks like:
  1316. Array
  1317. (
  1318. [0] => Array
  1319. (
  1320. [id] => 48c2570e-dfa8-4c32-a35e-0d71cbdd56cb
  1321. [name] => mysql raleigh-workshop-08 < 2008-09-05.sql
  1322. [description] => Importing an sql dump
  1323. )
  1324. [1] => Array
  1325. (
  1326. [id] => 48c257a8-cf7c-4af2-ac2f-114ecbdd56cb
  1327. [name] => pbpaste | grep -i Unpaid | pbcopy
  1328. [description] => Remove all lines that say "Unpaid".
  1329. )
  1330. [2] => 4
  1331. [3] => test array
  1332. [cats] => felines
  1333. [people] => 1267
  1334. [dog] => angry
  1335. )
  1336. */
  1337. .. php:staticmethod:: contains($val1, $val2 = null)
  1338. :rtype: boolean
  1339. Determines if one Set or array contains the exact keys and values
  1340. of another::
  1341. <?php
  1342. $a = array(
  1343. 0 => array('name' => 'main'),
  1344. 1 => array('name' => 'about')
  1345. );
  1346. $b = array(
  1347. 0 => array('name' => 'main'),
  1348. 1 => array('name' => 'about'),
  1349. 2 => array('name' => 'contact'),
  1350. 'a' => 'b'
  1351. );
  1352. $result = Set::contains($a, $a);
  1353. // True
  1354. $result = Set::contains($a, $b);
  1355. // False
  1356. $result = Set::contains($b, $a);
  1357. // True
  1358. .. todo::
  1359. Missing info about apply().